#include "12c5a.h"
#include "intrins.h"
#include "stdio.h"
#include <string.h>
#include "codetab.h"
#include "LQ12864.h"
#define uint unsigned int
#define uchar unsigned char
//sfr ADC_LOW2 = 0XBE;
#define ADC_POWER 0X80
#define ADC_FLAG 0X10
#define ADC_START 0X08
#define ADC_SPEEDLL 0X00
#define ADC_SPEEDL 0X20
#define ADC_SPEEDH 0X40
#define ADC_SPEEDHH 0X60
void adc_delay(uint n)
{
uint x;
while(n--)
{
x=5000;
while(x--);
}
}
unsigned int AD_get(unsigned char channel) //ad初始化
{
ADC_CONTR=ADC_POWER|ADC_SPEEDHH|ADC_START|channel;
_nop_(); _nop_(); _nop_(); _nop_();
while(!(ADC_CONTR&0x10));
ADC_CONTR&=0xe7;
return(ADC_RES*4+ADC_RESL);
}
float AD_work(unsigned char channel) //ad求平均值
{
float AD_val;
unsigned char i;
for(i=0;i<10;i++)
AD_val+=AD_get(channel);
AD_val/=10;
AD_val=AD_get(channel);
AD_val=(AD_val*5.0)/1024*100;
return AD_val;
//void UART_init()
//{
// TMOD = 0x20; //T1工作模式2 8位自动重装
// TH1 = 0xfd;
// TL1 = 0xfd; //比特率9600
// TR1 = 1; //启动T1定时器
// SM0 = 0;
// SM1 = 1; //串口工作方式1 10位异步
// REN = 1; //串口允许接收
// EA = 1; //开总中断
// ES = 1; //串口中断打开
//}
/*********************************************************
发送数据函数
*********************************************************/
//void senddata(float dat)
//{
// ES=0;
// TI=1;
// printf("输出1 %.3fV \n",dat);
// while(!TI);
// TI = 0;
// ES=1;
//}
void main()
{
OLED_Init(); //OLED初始化
// UART_init();
delay(8000);
OLED_CLS();
OLED_Asc_Str8x16(0,4," Waiting.......");
delay(5000);
OLED_CLS();
while(1)
{
int num=0;
num=AD_work();
OLED_Asc_Str8x16(28,0,number(num));
// senddata(AD_work(1));
// adc_delay(100);
}
}
|