#include
#include
#define uchar unsigned char
#define uint unsigned int
uint k;
uchar ch ;
/*******************延时****************/
void Delay (uint j)
{
uint i;
while(j–)
{
i=111;while(i–);
}
}
void InitADC()
{
P1M0=0X01;
P1M1=0X01;
P1ASF=0XFF; //相应端口当ADC使用时,端口要置位
ADC_RES=0;
ADC_CONTR=0XE0; //1110 0000 开电源,最高速
Delay(50);
}
int GetADC(uchar ch)
{
int result;
ADC_CONTR|=ch; //挑选通道
_nop_(); _nop_();
_nop_(); _nop_();
ADC_CONTR|=0x08; //0000 0100 开端AD转化
while(!(ADC_CONTR&ADC_FLAG)); //查询ADC_FLOG是否置位1 转化完毕
ADC_CONTR &= ~ADC_FLAG; //Clear ADC interrupt flag
//result=ADC_RES;//1111 0111 完毕AD转化
result=ADC_RES*4+ADC_RESL;
return(result);
}
/////////////////////均匀滤波//////////////////////
#define N 100
float filter(uchar ch)
{
float value=0;
uchar count;
for(count=0;count
value+=GetADC(ch); //100次采样求均匀
}
value/=202.5;//单片机电压5.05V
//value/=205;
//value/=2048;
//value/=512;
//value/=100;
//value=value*5.0/1024;
return (value);
}
/****************1602液晶******************/
sbit RS = P2^4;
sbit RW = P2^5;
sbit E = P2^6;
/*******************查表0~f****************/
uchar code LCD_display_code[16]=
{
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
0x38,0x39,0x41,0x42,0x43,0x44,0x45,0x046
};
void lcd_Write_com(uchar com)
{
RS=0; //界说指令寄存器
RW=0; //写答应
P0=com; //写指令
Delay(5);E=1;//片选端上拉
Delay(5);E=0;//下降沿锁存
}
void lcd_write_date(uchar date)
{
RS=1;//界说数据寄存器
RW=0;//写答应
P0=date;//写数据
Delay(5);E=1;//片选端上拉
Delay(5);E=0;//下降沿锁存
}
void dis_lcd1602(uchar x,uchar y,uchar dat)
{
uchar add;
if(y==1) add=(0x80+x);
if(y==2) add=(0xc0+x);
lcd_Write_com(add);//写指令
lcd_write_date(dat);//写数据
}
void lcd1602_init()
{
lcd_Write_com(0x38);//设置8位格局,2行,5*7
lcd_Write_com(0x01);//清屏
lcd_Write_com(0x0c);//全体显现,关光标,不闪耀
lcd_Write_com(0x06);//设定输入方法,增量不移位
lcd_Write_com(0x80);//初始坐标
}
/*******************液晶显现汉字程序****************/
void hz_lcdDis(uchar x,uchar y,uchar *p) //汉字显现
{
uchar i=0,temp;
if(x)temp = 0x40; else temp = 0;
for(i=y;*p!=\0;i++,p++)
{
lcd_Write_com(i|0x80+temp);
lcd_write_date(*p);
Delay(5);
}
}
void num_lcdDis(uchar X,uchar Y,uint num,uchar n) //数字显现
{
uint i=0,temp,hang;
n +=1;
if(X)hang = 0x40;else hang = 0;
for(i=(n-1);i>0;i–)
{
lcd_Write_com((i+Y-1)|0x80+hang); //从个位开端显现
temp = num%10+0x30;
lcd_write_date(temp);
Delay(5);
num/=10;
}
}
void main()
{
lcd1602_init();
InitADC();
while(1)
{
num_lcdDis(1,0,filter(0),5);
}
}