#include "main.h"
#include "LCD1602.h"
unsigned int ADC_Buffer = 0;
//****************************************************
//主函数
//****************************************************
void main()
{
ADC_CONTR = ADC_360T | ADC_ON;
AUXR1 |= ADRJ; //ADRJ = 1; //10bitAD右对齐
Init_LCD1602(); //初始化LCD1602
LCD1602_write_com(0x80); //指针设置到
LCD1602_write_word("Welcome to use!"); //显示内容
LCD1602_write_com(0x01); //清屏
while(1)
{
LCD1602_write_com(0x80); //指针设置到第一行
LCD1602_write_word("Initialize OK!"); //显示内容
ADC_Buffer = 1023 - adc10_start(0); // P1.0 ADC
LCD1602_write_com(0x80+0x40); //指针设置
LCD1602_write_word("val = "); //显示内容
LCD1602_write_data(ADC_Buffer/1000+0x30); //显示内容
LCD1602_write_data(ADC_Buffer%1000/100+0x30);
LCD1602_write_data(ADC_Buffer%100/10+0x30);
LCD1602_write_data(ADC_Buffer%10+0x30);
if( Sensor_DO == 0 ) //检测比较器输出
{
LED1 = LED_ON;
}
else
{
LED1 = LED_OFF;
}
Delay_ms(100);
}
}
//****************************************************
//做一次ADC转换
//****************************************************
unsigned int adc10_start(unsigned char channel) //channel = 0~7
{
unsigned int adc;
unsigned char i;
P1ASF = (1 << channel); //12C5A60AD/S2系列模拟输入(AD)选择
ADC_RES = 0;
ADC_RESL = 0;
ADC_CONTR = (ADC_CONTR & 0xe0) | ADC_START | channel;
_nop_();
_nop_();
_nop_();
_nop_();
// for(i=0; i<250; i++) //13T/loop, 40*13=520T=23.5us @ 22.1184M
i = 250;
do{
if(ADC_CONTR & ADC_FLAG)
{
ADC_CONTR &= ~ADC_FLAG; //软件清零ADC_FLAG
_nop_();
_nop_();
_nop_();
_nop_();
adc = 0;
adc = (ADC_RES << 8) | ADC_RESL; //ADRJ_enable()
return adc;
}
}while(--i);
return 1024;
}
//****************************************************
//MS延时函数(12M晶振下测试)
//****************************************************
void Delay_ms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}
|