#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit P23=P2^3;
sbit P35=P3^5;
sbit P36=P3^6;
sbit P37=P3^7;
sbit P20=P2^0;
sbit P21=P2^1;
sbit P22=P2^2;
sbit AD_EOC=P3^0;
sbit AD_START=P3^3;
sbit AD_OE=P3^4;
sbit LCD_RS=P3^1;
sbit LCD_EN=P3^2;
void Lcd_Delay1ms(uint c) ;
void delay(uint x);
void LcdWriteCom(uchar com);
void LcdWriteData(uchar dat);
void LCD_word(uchar addr,uchar *s);
void LCD_DATA(uchar addr,float dat);
void LcdInit();
void main()
{
float DATA,temp2;
char temp1;
LcdInit(); //液晶初始化
LCD_word(0x80,"voltage:");
while(1)
{
P2=0xF0;
temp1=P2;
temp1=temp1&0xf0;
switch(temp1)
{
case 0x80: P23=0,P35=1,P36=1,P37=1;P20=0,P21=0,P22=0;break;
case 0xc0: P23=1,P35=0,P36=1,P37=1,P20=1,P21=0,P22=0;break;
case 0xE0: P23=1,P35=1,P36=0,P37=1,P20=0,P21=1,P22=0;break;
case 0xF0: P23=1,P35=1,P36=1,P37=0,P20=1,P21=1,P22=0;break;
}
AD_START=0;
AD_OE=0;
AD_START=1;
_nop_();
AD_START=0;
while(!AD_EOC);
AD_OE=1;
temp2=P1;
AD_OE=0;
DATA=temp2/255.0*5.0;
temp1=P2;
temp1=temp1|0xf8;
switch(temp1)
{
case 0xf8:DATA=DATA*1;break; //2v
case 0xf9:DATA=DATA*10;break; //20
case 0xfa:DATA=DATA*100;break; //200
case 0xfb:DATA=DATA*1000;break; //500
}
LCD_DATA(0x89,DATA);
LCD_word(0x80+15,"V");
}
}
void Lcd_Delay1ms(uint c) //误差 0us
{
uchar a,b;
for (; c>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
void delay(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=10;b>0;b--);
}
void LcdWriteCom(uchar com) //写入命令
{
P0 = com; //放入命令
LCD_RS=0;
LCD_EN=0;
LCD_EN = 1; //写入时序
Lcd_Delay1ms(5); //保持时间
LCD_EN = 0;
}
void LcdWriteData(uchar dat) //写入数据
{
P0 = dat; //写入数据
LCD_RS=1;
LCD_EN = 0;
Lcd_Delay1ms(5); //保持时间
LCD_EN = 1; //写入时序
Lcd_Delay1ms(5); //保持时间
LCD_EN = 0;
}
void LCD_word(uchar addr,uchar *s)
{
LcdWriteCom(addr);
delay(20);
while(*s>0)
{
LcdWriteData(*s);s++;
}
}
void LCD_DATA(uchar addr,float dat)
{
int bai,shi,ge,shifen,baifen,qianfen;
uint num;
num=(uint)(dat*100);
bai=num/10000;
shi=num%10000/1000;
ge=num%10000%1000/100;
shifen=num%10000%1000%100/10;
baifen=num%10;
qianfen=num/1;
LcdWriteCom(addr);
LcdWriteData(bai+0x30);
LcdWriteData(shi+0x30);
LcdWriteData(ge+0x30);
LcdWriteData(0x2E);
LcdWriteData(shifen+0x30);
LcdWriteData(baifen+0x30);
}
void LcdInit() //LCD初始化子程序
{
LcdWriteCom(0x38); //开显示
Lcd_Delay1ms(10);
LcdWriteCom(0x0c); //开显示不显示光标
Lcd_Delay1ms(10);
LcdWriteCom(0x06); //写一个指针加1
Lcd_Delay1ms(10);
LcdWriteCom(0x01); //清屏
Lcd_Delay1ms(10);
}