51单片机1602lcd实现时间显示,可调时间和闹钟c语言
时间: 2023-12-14 10:37:19 浏览: 133
51单片机教程实例103-DS1302可调时钟
5星 · 资源好评率100%
以下是51单片机1602lcd实现时间显示,可调时间和闹钟的C语言代码:
```c
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS = P2^6;
sbit RW = P2^5;
sbit E = P2^7;
sbit K1 = P3^2;
sbit K2 = P3^3;
sbit K3 = P3^4;
sbit K4 = P3^5;
uchar code table[] = "2019-01-01 00:00:00";
uchar time[20];
uchar sec,min,hour,day,month,year,week;
uchar alarm_sec,alarm_min,alarm_hour;
void delay(uint ms)
{
uint i,j;
for(i=0; i<ms; i++)
for(j=0; j<125; j++);
}
void write_com(uchar com)
{
RS = 0;
RW = 0;
P0 = com;
_nop_();
E = 1;
_nop_();
E = 0;
delay(1);
}
void write_data(uchar dat)
{
RS = 1;
RW = 0;
P0 = dat;
_nop_();
E = 1;
_nop_();
E = 0;
delay(1);
}
uchar read_data()
{
uchar dat;
RS = 1;
RW = 1;
E = 1;
_nop_();
dat = P0;
E = 0;
return dat;
}
void init()
{
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
delay(5);
write_com(0x80);
}
void lcd_display(uchar *p)
{
while(*p != '\0')
{
write_data(*p++);
}
}
void ds1302_write(uchar dat)
{
uchar i;
for(i=0; i<8; i++)
{
SCK = 0;
DSIO = dat & 0x01;
dat >>= 1;
SCK = 1;
}
}
uchar ds1302_read()
{
uchar i,dat=0;
for(i=0; i<8; i++)
{
SCK = 0;
if(DSIO)
dat |= 0x01 << i;
SCK = 1;
}
return dat;
}
void ds1302_init()
{
uchar i;
CE = 0;
SCK = 0;
ds1302_write(0x8e);
ds1302_write(0x00);
for(i=0; i<8; i++)
ds1302_write(table[i]);
CE = 1;
}
void ds1302_read_time()
{
uchar i;
CE = 0;
ds1302_write(0xbe);
for(i=0; i<7; i++)
time[i] = ds1302_read();
CE = 1;
sec = time[0] & 0x0f;
sec += (time[0] >> 4) * 10;
min = time[1] & 0x0f;
min += (time[1] >> 4) * 10;
hour = time[2] & 0x0f;
hour += (time[2] >> 4) * 10;
day = time[3] & 0x0f;
day += (time[3] >> 4) * 10;
month = time[4] & 0x0f;
month += (time[4] >> 4) * 10;
week = time[5] & 0x07;
year = time[6] & 0x0f;
year += (time[6] >> 4) * 10;
}
void ds1302_write_time()
{
uchar i;
CE = 0;
ds1302_write(0x8e);
for(i=0; i<7; i++)
ds1302_write(time[i]);
CE = 1;
}
void set_time()
{
uchar i;
for(i=0; i<20; i++)
{
time[i] = 0;
}
lcd_display("Set Time:");
write_com(0xc0);
lcd_display("YYYY-MM-DD-hh-mm-ss");
write_com(0xc3);
K1 = 0;
while(K1 == 0);
for(i=0; i<19; i++)
{
if(i == 4 || i == 7 || i == 10 || i == 13 || i == 16)
{
write_com(0xc2+i);
K1 = 0;
while(K1 == 0);
}
else
{
write_com(0xc2+i);
K1 = 0;
while(K1 == 0)
{
if(i == 15)
time[i] = (time[i]+1) % 60;
else if(i == 12)
time[i] = (time[i]+1) % 24;
else if(i == 9)
time[i] = (time[i]+1) % 31 + 1;
else if(i == 6)
time[i] = (time[i]+1) % 13 + 1;
else if(i == 3)
time[i] = (time[i]+1) % 100;
}
}
if(i == 18)
break;
delay(50);
}
write_com(0x01);
}
void set_alarm()
{
uchar i;
for(i=0; i<20; i++)
{
time[i] = 0;
}
lcd_display("Set Alarm:");
write_com(0xc0);
lcd_display("HH-MM-SS");
write_com(0xc3);
K1 = 0;
while(K1 == 0);
for(i=0; i<8; i++)
{
if(i == 2 || i == 5)
{
write_com(0xc2+i);
K1 = 0;
while(K1 == 0);
}
else
{
write_com(0xc2+i);
K1 = 0;
while(K1 == 0)
{
if(i == 7)
alarm_sec = (alarm_sec+1) % 60;
else if(i == 4)
alarm_min = (alarm_min+1) % 60;
else if(i == 1)
alarm_hour = (alarm_hour+1) % 24;
}
}
if(i == 7)
break;
delay(50);
}
write_com(0x01);
}
void main()
{
init();
ds1302_init();
while(1)
{
ds1302_read_time();
sprintf(time, "%04d-%02d-%02d %02d:%02d:%02d", 2000+year, month, day, hour, min, sec);
write_com(0x80);
lcd_display(time);
delay(500);
if(alarm_hour == hour && alarm_min == min && alarm_sec == sec)
{
write_com(0x01);
lcd_display("Alarm!!!");
delay(200);
write_com(0x01);
}
if(K2 == 0)
set_time();
if(K3 == 0)
set_alarm();
}
}
```
这个代码实现了一个简单的时钟,可以通过按下K2键设置当前时间,按下K3键设置闹钟时间。每秒钟LCD屏幕上显示当前时间,并且如果闹钟时间到了,LCD屏幕上会显示"Alarm!!!"。
阅读全文