
#include<reg52.h>
#include
#define uchar unsigned char
#define uint unsigned int
//****端口定義*************************
sbit lcdrst=P3^3;
sbit lcdrs=P3^2;
sbit lcdrw=P3^1;
sbit lcde=P3^0;
//****端口定義*************************
sbit ST=P2^7; //A/D啟動(dòng)轉(zhuǎn)換信號(hào)
sbit OE=P2^5; //數(shù)據(jù)輸出允許信號(hào)
sbit EOC=P2^6; //A/D轉(zhuǎn)換結(jié)束信號(hào)
sbit CLK=P2^4; //時(shí)鐘脈沖
sbit ADDA=P2^0;
sbit ADDB=P2^1;
sbit ADDC=P2^2;
//****顯示緩存定義*********************
uint tabSambuffer[8]={0,0,0,0,0,0,0,0};
long tempvalue=0; //采集回來(lái)數(shù)據(jù)第一次處理
uint value_V=0;
uchar diapbuffer[4]={0,10,0,0};
uchar code tab_zix[]={"0123456789.:"};
uchar code dis1[]={" CH0: CH1: "};
uchar code dis2[]={" CH2: CH3: "};
uchar code dis3[]={" CH4: CH5: "};
uchar code dis4[]={" CH6: CH7: "};
uchar code dia11[]={">C"}; //當(dāng)前數(shù)據(jù)跟新
uchar code dia00[]={" C"};
uchar xh,cnt;
void longdelay(uchar t)
{
uint j;
for(;t>0;t--)
{for(j=1000;j>0;j--);}
}
void shortdelay(uchar i)
{
for(;i>0;i--);
}
//****讀狀態(tài)函數(shù)*************************
uchar readstatus(void)
{
uchar i;
P1=0xff;
lcdrw=1;
lcdrs=0;
lcde=0;
shortdelay(10);
lcde=1;
shortdelay(20);
i=P1;
shortdelay(40);
lcde=0;
return(i);
}
//****寫(xiě)命令函數(shù)*************************
void lcdwriteinstruc(uchar instruction)
{
while((readstatus()&0x80)!=0x00);
P1=0;
lcdrw=0;
lcdrs=0;
lcde=0;
P1=instruction;
shortdelay(100);
lcde=1;
shortdelay(100);
lcde=0;
}
//****初始化LCD*************************
void initlcd(void)
{
longdelay(40);
lcdrst=0;
longdelay(1);
lcdrst=1;
longdelay(1); //復(fù)位LCD
lcdwriteinstruc(0x30);
longdelay(1);
lcdwriteinstruc(0x30);
longdelay(1);
lcdwriteinstruc(0x0c); //顯示開(kāi) 游標(biāo)關(guān) 游標(biāo)位置反白
longdelay(1);
lcdwriteinstruc(0x01);
longdelay(1);
lcdwriteinstruc(0x06); //增量方式 不移位
longdelay(1);
}
//****寫(xiě)一個(gè)字節(jié)函數(shù)*************************
void writeonebyte(uchar dat)
{
while((readstatus()&0x80)!=0x00);
P1=0;
lcdrw=0;
lcdrs=1;
lcde=0;
P1=dat;
shortdelay(100);
lcde=1;
shortdelay(100);
lcde=0;
}
//****顯示地址解析*************************
void disponeword(uchar x,uchar y)
{
uchar dispaddr;
switch(x)
{
case 0: dispaddr=0x80;break;
case 1: dispaddr=0x90;break;
case 2: dispaddr=0x88;break;
case 3: dispaddr=0x98;break;
default:break;
}
dispaddr=dispaddr+y;
lcdwriteinstruc(dispaddr);
}
void Clear()
{
//******************************************************************
disponeword(0,0);
for(xh=0;xh<2;xh++)
{
writeonebyte(dia00[xh]);
shortdelay(1);
}
disponeword(1,0);
for(xh=0;xh<2;xh++)
{
writeonebyte(dia00[xh]);
shortdelay(1);
}
disponeword(2,0);
for(xh=0;xh<2;xh++)
{
writeonebyte(dia00[xh]);
shortdelay(1);
}
disponeword(3,0);
for(xh=0;xh<2;xh++)
{
writeonebyte(dia00[xh]);
shortdelay(1);
}
//******************************************************************
}
void main()
{
initlcd();
//***********初始頁(yè)面顯示畫(huà)面*************************************
disponeword(0,0);
for(xh=0;xh<17;xh++)
{
writeonebyte(dis1[xh]);
shortdelay(1);
}
disponeword(1,0);
for(xh=0;xh<17;xh++)
{
writeonebyte(dis2[xh]);
shortdelay(1);
}
disponeword(2,0);
for(xh=0;xh<17;xh++)
{
writeonebyte(dis3[xh]);
shortdelay(1);
}
disponeword(3,0);
for(xh=0;xh<17;xh++)
{
writeonebyte(dis4[xh]);
shortdelay(1);
}//***********初始頁(yè)面顯示畫(huà)面***完*****************************
TMOD=0X01; //定時(shí)器中斷0
CLK=0; //脈沖信號(hào)初始值為0
TH0=(65536-2)/256; //定時(shí)時(shí)間高八位初值
TL0=(65536-2)%6; //定時(shí)時(shí)間低八位初值
EA=1; //開(kāi)CPU中斷
ET0=1; //開(kāi)T/C0中斷
TR0=1;
while(1)
{
Clear(); //清除當(dāng)前行指示
for(cnt=0;cnt<8;cnt++)
{
P2&=0xf0;
P2|=cnt; //設(shè)置采集通道
ST=0; //使采集信號(hào)為低
shortdelay(2);
ST=1; //開(kāi)始數(shù)據(jù)轉(zhuǎn)換
shortdelay(2);
ST=0; //停止數(shù)據(jù)轉(zhuǎn)換
longdelay(10); //長(zhǎng)延時(shí)
//while(!EOC); //等待數(shù)據(jù)轉(zhuǎn)換完畢
OE=1; //允許數(shù)據(jù)輸出信號(hào)
shortdelay(1);
tabSambuffer[cnt]=P0; //讀取數(shù)據(jù)
shortdelay(1);
OE=0; //一次數(shù)據(jù)采集完成
tempvalue=tabSambuffer[cnt];
tempvalue=tempvalue*341/256;
value_V=(uint)tempvalue;
diapbuffer[0]=11;
diapbuffer[1]=value_V/100;
diapbuffer[2]=10; //小數(shù)點(diǎn) code tab_zix[]
diapbuffer[3]=value_V0/10;
switch(cnt)
{
case 0x00:disponeword(0,2); break;
case 0x01:disponeword(0,6); break;
case 0x02:disponeword(1,2); break;
case 0x03:disponeword(1,6); break;
case 0x04:disponeword(2,2); break;
case 0x05:disponeword(2,6); break;
case 0x06:disponeword(3,2); break;
case 0x07:disponeword(3,6); break;
}
for(xh=0;xh<4;xh++) //每次更新數(shù)據(jù)
{
writeonebyte(tab_zix[diapbuffer[xh]]);
shortdelay(1);
}
//******************************************************************
if((cnt==0)|(cnt==1))//更新數(shù)據(jù)后指示當(dāng)前行
{
disponeword(0,0);
for(xh=0;xh<2;xh++)
{
writeonebyte(dia11[xh]);
shortdelay(1);
}
}
if((cnt==2)|(cnt==3))//更新數(shù)據(jù)后指示當(dāng)前行
{
disponeword(1,0);
for(xh=0;xh<2;xh++)
{
writeonebyte(dia11[xh]);
shortdelay(1);
}
}
if((cnt==4)|(cnt==5))//更新數(shù)據(jù)后指示當(dāng)前行
{
disponeword(2,0);
for(xh=0;xh<2;xh++)
{
writeonebyte(dia11[xh]);
shortdelay(1);
}
}
if((cnt==6)|(cnt==7))//更新數(shù)據(jù)后指示當(dāng)前行
{
disponeword(3,0);
for(xh=0;xh<2;xh++)
{
writeonebyte(dia11[xh]);
shortdelay(1);
}
}
//******************************************************************
}
}
}
//***********產(chǎn)生AD采樣脈沖*******************************************
void timer0( ) interrupt 1 //定時(shí)器0工作方式1
{
TH0=(65536-2)/256; //重裝計(jì)數(shù)初值
TL0=(65536-2)%6; //重裝計(jì)數(shù)初值
CLK=!CLK; //取?
}