//////////////////////////////////////////////
#include<REG52.H>
#define uchar unsigned char
#define uint unsigned int
unsigned char code command[6][7]={
0xB0,0xC0,0xA8,0x01,0x01,0x00,0x1A,//读取电压值
0xB1,0xC0,0xA8,0x01,0x01,0x00,0x1B,//读取电流值
0xB3,0xC0,0xA8,0x01,0x01,0x00,0x1D,//读取电量值
0x7e,0x05,0x41,0x00,0x04,0x40,0xef,
0x7e,0x05,0x41,0x00,0x05,0x41,0xef,
0x7e,0x05,0x41,0x00,0x06,0x42,0xef
};//串口控制音乐指令
uchar shuju[8];
uchar jishu=0;
uchar CRC=0;
uchar j=0;
long AA=0;
uint BB=0;
uint CC;
//******************LCD1602****************************//
sbit lcden=P1^2;
sbit lcdrw=P1^1;
sbit lcdrs=P1^0;
//////////////LCD1602///////////////////////////////////////
void delay1(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
lcdrs=0;
P0=com;
delay1(5);
lcden=1;
delay1(5);
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1;
P0=date;
delay1(5);
lcden=1;
delay1(5);
lcden=0;
}
void init1602()
{
lcden=0;
write_com(0x38);
write_com(0x0c);// write_com(0x0e);
write_com(0x06);
write_com(0x01);
write_com(0x80+2);
}
///////////////LCD1602
void SendASC(uchar d) //串口发送字符
{
SBUF=d; //数据赋值到缓冲区
while(!TI); //发送完
TI=0; //清零
}
void deal(void)
{
//电压读取
jishu=0;
for(j=0;j<7;j++)
{ shuju[j]=0;
SendASC(command[0][j]);
}
delay1(200);
delay1(20);
if(jishu>0)
{ CRC=0;
for(j=0;j<6;j++)
{
CRC=CRC+shuju[j];
}
if((CRC==shuju[6])&&(shuju[0]==0xa0))
{
AA=shuju[1]*256+shuju[2];
write_com(0x80+0x40);
if(AA>=100)
write_data(AA/100+0x30);
else write_data(' ');
delay1(20);
write_data(AA%100/10+0x30);
delay1(20);
write_data(AA%10+0x30);
delay1(20);
write_data('.');
delay1(20);
write_data(shuju[3]+0x30);
delay1(20);
write_data('V');
delay1(20);
}
}
//电流读取
jishu=0;
for(j=0;j<7;j++)
{ shuju[j]=0;
SendASC(command[1][j]);
}
delay1(200);
if(jishu>0)
{ CRC=0;
for(j=0;j<6;j++)
{
CRC=CRC+shuju[j];
}
if((CRC==shuju[6])&&(shuju[0]==0xa1))
{
AA=shuju[2];
write_com(0x80+0x40+9);
if(AA>=100)
write_data(AA/100+0x30);
else write_data(' ');
delay1(20);
write_data(AA%100/10+0x30);
delay1(20);
write_data(AA%10+0x30);
delay1(20);
write_data('.');
delay1(20);
write_data(shuju[3]/10+0x30);
delay1(20);
write_data(shuju[3]%10+0x30);
delay1(20);
write_data('A');
delay1(20);
}
}
//电量读取
jishu=0;
for(j=0;j<7;j++)
{ shuju[j]=0;
SendASC(command[2][j]);
}
delay1(200);
if(jishu>0)
{ CRC=0;
for(j=0;j<6;j++)
{
CRC=CRC+shuju[j];
}
if((CRC==shuju[6])&&(shuju[0]==0xa3))
{
AA=shuju[1];
AA=AA<<16;
AA=AA+shuju[2]*256;
AA=AA+shuju[3];
write_com(0x80);
if(AA>=1000000)
write_data(AA/1000000+0x30);
else write_data(' ');
delay1(20);
if(AA>=100000)
write_data(AA%1000000/100000+0x30);
else write_data(' ');
delay1(20);
if(AA>=10000)
write_data(AA%100000/10000+0x30);
else write_data(' ');
delay1(20);
if(AA>=1000)
write_data(AA%10000/1000+0x30);
else write_data(' ');
delay1(20);
if(AA>=100)
write_data(AA%1000/100+0x30);
else write_data(' ');
delay1(20);
if(AA>=10)
write_data(AA%100/10+0x30);
else write_data(' ');
delay1(20);
write_data(AA%10+0x30);
delay1(20);
write_data('W');
delay1(20);
write_data('H');
delay1(20);
}
}
}
void main(void)
{
lcdrw=0;
init1602();
//串口通信
TMOD=0x21;//?????0?????1
TH1=0Xfd;
TL1=0Xfd; //9600波特率
TR1=1; // 定时器1启动
SM0=0; // 设置串口的工作模式
SM1=1; //方式1
REN=1; // 允许串口接收数据
ES=1; // 串口中断应许
EA=1;
while(1)
{
deal();
}
}
void ser() interrupt 4
{
if(RI)
{
RI=0;
shuju[jishu++]=SBUF;
if(jishu>7)
jishu=0;
}
}
|