|
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
void senddata(uchar dat);
uchar cal [7]= {0};// cal[0]:起始位 0xAA
// cal[1] : Vout(H)
// cal[2] : Vout(L)
// cal[3] : Vref(H)
// cal[4] : Vref(L)
// cal[5] : 校验位
// cal[6] : 结束位 OxFF
float Vo;
int i=0;
sbit rs=P1^0;
sbit lcden=P2^5;
sbit rw=P1^1;
sbit voice=P2^3;
sbit light=P2^2;
uchar buf,sum,count;
uchar num,TV,a,b,c;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
rs=0;
rw=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date)
{
rs=1;
rw=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
lcden=0; //使能端为低电平
write_com(0x38);
/*显示模式设置 00111000 设置16*2显示 5*7点阵 8位数据接口*/
write_com(0x0f);
/*显示开关及光标设置 00001DCB
D=1,开显示 D=0, 关显示
C=1,显示光标 C=0,不显示光标
B=1,光标闪烁 B=0,光标不闪烁*/
write_com(0x06);//地址指针自动+1且光标+1,写字符屏幕不会移动
write_com(0x01);
write_com(0x80);
write_data ('P');
write_com (0x81);
write_data ('M');
write_com (0x82);
write_data ('2');
write_com (0x83);
write_data ('.');
write_com (0x84);
write_data ('5');
write_com (0x85);
write_data (':');
write_com (0xcb);
write_data ('m');
write_com (0xcc);
write_data ('g');
write_com (0xcd);
write_data ('/');
write_com (0xce);
write_data ('m');
write_com (0xcf);
write_data ('3');
}
void warn()
{
if (num>=200)
{
voice=0;
light=0;
}
}
void main()
{
init();
SCON=0x50; //设定串口工作方式
PCON=0x00; //波特章不倍增
TMOD=0x20; //定时器1工作于8位自动重载横式,用于产生波特率
EA=1;
ES = 1; //允许串口中断
TL0=0x80;
TH0=0xFB;
TR0=1;
num=TV;
a= TV /100+0x30;
TV %=100;
b= TV /10+0x30;
TV %=10;
c= TV +0x30;
write_com (0x88);
write_data (a);
write_com (0x89);
write_data (b);
write_com (0x8a);
write_data (c);
//warn();
while(1);
}
void serial() interrupt 4
{
ES = 0; //关闭串行中断
RI = 0; //清除串行接受标志位
buf = SBUF; //从串口缓冲区取得数据
senddata(buf);
ES = 1; //允许串口中断
}
void senddata(uchar dat)
{if(dat==170) //判断起始位 OxAA, (0xAA==l70〉
{
i=0;
cal[i]=dat;
}
else
{
i=i+1;
cal[i]=dat;
}
if(i==6)
{
sum= cal[1]+cal[2]+cal[3]+cal[4];//sum=Vout(M)+Vout(L)+Vref(M)+Vref(L)
}
if (sum == cal [5] && cal[6]==255) //sum与校验 相同 &&最后一位是 OxFF 结束位
{
for (i=0; i <=6; i++) //此处for循环是显示出所有数据,即cal [7]中的数据
{
SBUF=cal[i];
while(!TI);
TI=0;
}
Vo=(cal[1]*256+cal[2])/2048*5;
TV=3000* Vo;
}
}
|
|