|
#include <AT89X52.H>
#define LEDDATA P0
#define v20_on {s3=0;s2=0;s1=1;} //宏定義不同量程,不同的開關(guān)狀態(tài)
#define v2_on {s3=0;s2=1;s1=0;}
#define v02_on {s3=1;s2=0;s1=0;}
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
unsigned char dispbuf[8]={0,0,0,0,0,0,0,0};
unsigned char getdata;
unsigned long temp;
unsigned char i,k,l,m;
unsigned char code mytable0[]=" Welcome to use ";
unsigned char code mytable1[]="Auto Voltmeter!";
unsigned char code line0[]=" Voltmeter "; //初始化顯示
unsigned char code line1[]=" Value: V ";
//引腳定義
sbit lcdrs=P2^0;
sbit lcden=P2^1;
sbit s3=P3^7;
sbit s2=P3^6;
sbit s1=P3^5;
sbit OE=P3^0;
sbit EOC=P3^1;
sbit ST=P3^2;
void delay(unsigned int z) //延時(shí)子函數(shù) z*1ms
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(unsigned char c) //寫命令子函數(shù)
{
lcdrs=0; //低電平選擇為 寫指令
lcden=0;
LEDDATA=c; //把指令寫入P0口
delay(5); //參考時(shí)序圖
lcden=1; //開使能
delay(5); //讀取指令
lcden=0; //關(guān)閉使能
}
void write_data(unsigned char d) //寫數(shù)據(jù)子函數(shù)
{
lcdrs=1; //高電平選擇為 寫數(shù)據(jù)
LEDDATA=d; //把數(shù)據(jù)寫入P0口
delay(5); //參考時(shí)序圖
lcden=1; //開使能
delay(5); //讀取數(shù)據(jù)
lcden=0; //關(guān)閉使能
}
void initialize() //LCD初始化函數(shù)
{
unsigned char num;
lcden=0;
write_com(0x38); //設(shè)置16x2顯示,5x7點(diǎn)陣顯示,8位數(shù)據(jù)接口
write_com(0x0c); //00001DCB,D(開關(guān)顯示),C(是否顯示光標(biāo)),B(光標(biāo)閃爍,光標(biāo)不顯示)
write_com(0x06); //000001N0,N(地址指針+-1)
write_com(0x01); //清屏指令 每次顯示下一屏內(nèi)容時(shí),必須清屏
write_com(0x80+0x10); //第一行,頂格顯示
for(num=0;num<17;num++)
{
write_data(mytable0[num]);
delay(10);
}
write_com(0x80+0x50); //第二行,從第一格開始顯示
for(num=0;num<15;num++)
{
write_data(mytable1[num]);
delay(10);
}
for(num=0;num<16;num++)
{
write_com(0x1c); //0001(S/C)(R/L)**; S/C:高電平移動(dòng)字符,低電平移動(dòng)光標(biāo); R/L:高電平左移,低電平右移
delay(300);
}
delay(1000);
write_com(0x01); //清屏指令 每次顯示下一屏內(nèi)容時(shí),必須清屏
write_com(0x80);
for(num=0;num<14;num++)
{
write_data(line0[num]);
delay(10);
}
write_com(0x80+0x40);
for(num=0;num<15;num++)
{
write_data(line1[num]);
delay(10);
}
}
void value(unsigned char add,unsigned char dat)
{
write_com(0x80+0x47+add);
if(l==3&&add==2||l!=3&&add==1)
{
write_data(0x2e);
}
else
{
write_data(0x30+dat);
}
}
main()
{
initialize();
while(1)
_20v:
{
v20_on;
ST=0;
ST=1;
ST=0;
while(EOC==0);
OE=1;
getdata=P1;
OE=0;
if(getdata<21)
{
goto _2v;
}
l=3;
temp=getdata;
temp=(temp*1000/51)/2;
goto disp;
_2v:
v2_on;
ST=0;
ST=1;
ST=0;
while(EOC==0);
OE=1;
getdata=P1;
OE=0;
if(getdata<21)
{
goto _02v;
}
else if(getdata>204)
{
goto _20v;
}
l=2;
temp=getdata;
temp=(temp*1000/51)/2;
goto disp;
_02v:
v02_on;
ST=0;
ST=1;
ST=0;
while(EOC==0);
OE=1;
getdata=P1;
OE=0;
if(getdata>204)
{
goto _2v;
}
l=1;
temp=getdata;
temp=(temp*1000/51)/2;
m=temp%10;
if(m>5){temp=temp/10+1;}
else{temp=temp/10;}
goto disp;
disp: for(i=0;i<=3;i++)
{
dispbuf[i]=temp%10;
temp=temp/10;
}
if(l==3)
{
for(i=4;i>=3;i--)
dispbuf[i]=dispbuf[i-1];
}
else
{
dispbuf[4]=dispbuf[3];
}
for(k=0;k<5;k++)
{
value(k,dispbuf[4-k]);
}
if(l==2){goto _2v;}
else if(l==1){goto _02v;}
}
}
|
|