#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启动转换信号
sbit OE=P2^5; //数据输出允许信号
sbit EOC=P2^6; //A/D转换结束信号
sbit CLK=P2^4; //时钟脉冲
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;//采集回来数据第一次处理
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"};//当前数据跟新
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--);
}
//****读状态函数*************************
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);
}
//****写命令函数*************************
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); //复位LCD
lcdwriteinstruc(0x30);
longdelay(1);
lcdwriteinstruc(0x30);
longdelay(1);
lcdwriteinstruc(0x0c); //显示开 游标关 游标位置反白
longdelay(1);
lcdwriteinstruc(0x01);
longdelay(1);
lcdwriteinstruc(0x06); //增量方式 不移位
longdelay(1);
}
//****写一个字节函数*************************
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();
//***********初始页面显示画面*************************************
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);
}//***********初始页面显示画面***完*****************************
TMOD=0X01; //定时器中断0
CLK=0; //脉冲信号初始值为0
TH0=(65536-2)/256; //定时时间高八位初值
TL0=(65536-2)%6; //定时时间低八位初值
EA=1; //开CPU中断
ET0=1; //开T/C0中断
TR0=1;
while(1)
{
Clear();//清除当前行指示
for(cnt=0;cnt<8;cnt++)
{
P2&=0xf0;
P2|=cnt; //设置采集通道
ST=0;//使采集信号为低
shortdelay(2);
ST=1;//开始数据转换
shortdelay(2);
ST=0;//停止数据转换
longdelay(10); //长延时
//while(!EOC);//等待数据转换完毕
OE=1;//允许数据输出信号
shortdelay(1);
tabSambuffer[cnt]=P0;//读取数据
shortdelay(1);
OE=0;//一次数据采集完成
tempvalue=tabSambuffer[cnt];
tempvalue=tempvalue*341/256;
value_V=(uint)tempvalue;
diapbuffer[0]=11;
diapbuffer[1]=value_V/100;
diapbuffer[2]=10; //小数点 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++) //每次更新数据
{
writeonebyte(tab_zix[diapbuffer[xh]]);
shortdelay(1);
}
//******************************************************************
if((cnt==0)|(cnt==1))//更新数据后指示当前行
{
disponeword(0,0);
for(xh=0;xh<2;xh++)
{
writeonebyte(dia11[xh]);
shortdelay(1);
}
}
if((cnt==2)|(cnt==3))//更新数据后指示当前行
{
disponeword(1,0);
for(xh=0;xh<2;xh++)
{
writeonebyte(dia11[xh]);
shortdelay(1);
}
}
if((cnt==4)|(cnt==5))//更新数据后指示当前行
{
disponeword(2,0);
for(xh=0;xh<2;xh++)
{
writeonebyte(dia11[xh]);
shortdelay(1);
}
}
if((cnt==6)|(cnt==7))//更新数据后指示当前行
{
disponeword(3,0);
for(xh=0;xh<2;xh++)
{
writeonebyte(dia11[xh]);
shortdelay(1);
}
}
//******************************************************************
}
}
}
//***********产生AD采样脉冲*******************************************
void timer0( ) interrupt 1 //定时器0工作方式1
{
TH0=(65536-2)/256; //重装计数初值
TL0=(65536-2)%6; //重装计数初值
CLK=!CLK; //取?
}