基于51单片机的简单LCD1602时钟
1.要求: 1.1-在LCD1602上显示时间时、分、秒; 1.2-在Proteus上仿真实现; 1.3-焊接制作实物; 2.程序代码
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define LCD_Port P1
sbit RS=P0^0;
sbit RW=P0^1;
sbit E=P0^2;
uint sec=0;
uint min=0;
uint hour=0;
uint k=0;
uchar LCD1602_1[]="LCD1602 Clock";
uchar LCD1602_2[]="Time";
void delay_50us(uchar t);
void delay_50ms(uchar t);
void write_zhiling(uint zhiling);
void write_shuju(uint shuju);
void dis_char(uint hang,uint lie,uint dat);
void dis_string(uint hang,uint lie,uint *p);
void dis_shuju(uint hang,uint lie,uint dat);
void timer0Init();
void dis_time(shi,fen,miao);
void LCD_Init();
void main()
{
timer0Init();
LCD_Init();
dis_string(1,0,LCD1602_1);
dis_string(2,0,LCD1602_2);
dis_char(2,7,'-');
dis_char(2,10,'-');
while(1)
{
dis_shuju(2,14,k);
dis_time(hour,min,sec);
}
}
void delay_50us(uchar t)
{
uchar i,j;
for(i=t;t>0;t--)
for(j=19;j>0;j--);
}
void delay_50ms(uchar t)
{
uchar i,j;
for(i=t;i>0;i--)
for(j=6245;j>0;j--);
}
void timer0Init()
{
EA=1;
ET0=1;
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
}
void timer0() interrupt 1
{
TR0=0;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
k++;
if(k==20){k=0;sec++;}
if(sec==60){sec=0;min++;}
if(min==60){min=0;hour++;}
if(hour==24){hour=0;}
}
void LCD_Init(void)
{
delay_50us(300);
write_zhiling(0x38);
delay_50us(100);
write_zhiling(0x38);
delay_50us(100);
write_zhiling(0x38);
write_zhiling(0x38);
write_zhiling(0x08);
write_zhiling(0x01);
write_zhiling(0x06);
write_zhiling(0x0c);
}
void write_zhiling(uint zhiling)
{
E=0;
RS=0;
RW=0;
LCD_Port=zhiling;
delay_50us(10);
E=1;
delay_50us(20);
E=0;
}
void write_shuju(uint shuju)
{
E=0;
RS=1;
RW=0;
LCD_Port=shuju;
delay_50us(10);
E=1;
delay_50us(20);
E=0;
}
void dis_char(uint hang,uint lie,uint dat)
{
if(hang==1)
{
write_zhiling(0x80+lie);
}
if(hang==2)
{
write_zhiling(0xC0+lie);
}
write_shuju(dat);
}
void dis_string(uint hang,uint lie,uchar *p)
{
uint a;
if(hang==1)a=0x80;
if(hang==2)a=0xC0;
a=a+lie;
write_zhiling(a);
while(1)
{
if(*p=='\0')break;
write_shuju(*p);
p++;
}
}
void dis_shuju(uint hang,uint lie,uint dat)
{
uint shi,ge;
shi=dat/10%10+0x30;
ge=dat%10+0x30;
dis_char(hang,lie,shi);
dis_char(hang,lie+1,ge);
}
void dis_time(uint HOUR,uint MIN,uint SEC)
{
dis_shuju(2,11,SEC);
dis_shuju(2,8,MIN);
dis_shuju(2,5,HOUR);
}
3.仿真电路 4.实物演示 4.1-电路连接
|