#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit rw=P2^5;//一下三行是设置lcd1602的使能端
sbit rs=P2^6;
sbit ep=P2^7;
typedef bit BOOL;
uchar t,A1,A2,A3,A4,A5,A6,Flag=0,count,H,L;
void delay(uchar ms) //延时
{
uchar i;
while(ms--)
for(i=0;i<100;i++);
}
BOOL lcd_bz()//测试lcd忙碌状态返回值为布尔型数值真或假'1'.'0'
{
BOOL result;
rs=0; // 读忙信号
rw=1;
ep=1;
_nop_();
_nop_();
_nop_();
_nop_();
result=(BOOL)(P0&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);//LCD第一行的首地址为0x80第二行的首地址为0x80+0x40=0xc0
}
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() //lcd初始化
{
write_cmd(0x38);//设置LCD两行显示一个数据由5*7点阵表示,数据由8跟线传输
write_cmd(0x0c);//清除屏幕显示
write_cmd(0x06);//设定输入方式增量不移位
write_cmd(0x01);//开整体显示关光标不闪烁
}
void display(uchar addr,uchar q)//在某一地址上显示内容adder表示的是地址偏移量q表示显示的字符或数字
{
write_addr(addr);
write_byte(q);
delay(1);//修改此时间可以改变LCD上数值跳变的数度
}
void init()
{
TMOD = 0x51;
TH0 = (65535-45872)/256;
TL0 = (65535-45872)%256;
TH1 = 0x00;
TL1 = 0x00;
ET0 = 1;
ET1 = 1;
EA = 1;
TR0 = 1;
TR1 = 1;
}
void Time0() interrupt 1
{
TL0 = 0x00;
TH0 = 0x4c;
t++;
if (t == 20)
{
TR0 = 0;
TR1 = 0;
t = 0;
Flag = 1;
}
}
void Time1() interrupt 3
{
count++;
}
void main()
{
unsigned long x;
P3 = 0xFF;
init();
lcd_init();
while (1)
{
if (Flag)
{
H = TH1;
L = TL1;
x = ((count*65535)+(H*256)+L);
A1=x/100000;
A2=x%100000/10000;
A3=x%10000/1000;
A4=x%1000/100;
A5=x%100/10;
A6=x%10;
Flag = 0;
count = 0;
TH1 = 0x00;
TL1 = 0x00;
TH0 = (65535-45872)/256;
TL0 = (65535-45872)%256;
TR1 = 1;
TR0 = 1;
}
display(0x00,A1+0x30);
display(0x01,A2+0x30);
display(0x02,A3+0x30);
display(0x03,A4+0x30);
display(0x04,A5+0x30);
display(0x05,A6+0x30);
}
}
|