|
#include <reg51.h>
#include <intrins.h>
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define uint unsigned int
#define uchar unsigned char
sbit io=P1^0;
sbit rw=P2^1;
sbit rs=P2^0;
sbit ep=P2^2;
typedef bit BOOL;
uchar data_byte;
uchar RH,RL,TH,TL;
void delay(uchar ms)
{
uchar i;
while(ms--)
for(i=0;i<100;i++);
}
void delay1()
{
uchar i;
for(i=0;i<1;i++);
}
BOOL lcd_bz()
{
BOOL result;
rs=0;
rw=1;
ep=1;
_nop_();
_nop_();
_nop_();
_nop_();
result = (BOOL)(P2&0x80);
ep=0;
return result ;
}
void write_cmd(uchar cmd)
{
while (lcd_bz());
rs=0;
rw=0;
ep=0;
_nop_();
_nop_();
P0=cmd ;
_nop_();
_nop_();
_nop_();
_nop_();
ep=1;
_nop_();
_nop_();
_nop_();
_nop_();
ep=0;
}
void write_addr(uchar addr)
{
write_cmd(addr|0x80);
}
void write_byte(uchar dat)
{
while (lcd_bz());
rs=1;
rw=0;
ep=0;
_nop_();
_nop_();
P0=dat ;
_nop_();
_nop_();
_nop_();
_nop_();
ep=1;
_nop_();
_nop_();
_nop_();
_nop_();
ep=0;
}
void lcd_init()
{
write_cmd(0x38);
delay(1);
write_cmd(0x0c);
delay(1);
write_cmd(0x06);
delay(1);
write_cmd(0x01);
delay(1);
}
void display(uchar addr,uchar q)
{
delay(10);
write_addr(addr);
write_byte(q);
delay(1);
}
void start()
{
io=1;
delay1();
io=0;
delay(25);
io=1;
delay1();
delay1();
delay1();
}
uchar receive_byte()
{
uchar i,temp;
for(i=0;i<8;i++)
{
while(!io);
delay1();
delay1();
delay1();
temp=0;
if(io==1)
temp=1;
while(io);
data_byte<<=1;
data_byte|=temp;
}
return data_byte;
}
void receive()
{
uchar T_H,T_L,R_H,R_L,check,num_check,i;
start();
io=1;
if(!io)
{
while(!io);
while(io);
R_H=receive_byte();
R_L=receive_byte();
T_H=receive_byte();
T_L=receive_byte();
check=receive_byte();
io=0;
for(i=0;i<7;i++)
delay1();
io=1;
num_check=R_H+R_L+T_H+T_L;
if(num_check==check)
{
RH=R_H;
RL=R_L;
TH=T_H;
TL=T_L;
check=num_check;
}
}
}
void main()
{
lcd_init();
while(1)
{
receive();
display(0x00,'R');
display(0x01,':');
display(0x02,RH/10+0x30);
display(0x03,RH%10+0x30);
display(0X04,'%');
display(0x40,'T');
display(0x41,':');
display(0x42,TH/10+0x30);
display(0x43,TH%10+0x30);
display(0x44,0xdf);
display(0x45,0x43);
}
}
|
评分
-
查看全部评分
|