51单片机1602液晶显示屏上显示当前的年月日代码(D$1302)
时间: 2023-11-13 12:04:41 浏览: 95
以下是基于51单片机和1602液晶显示屏的代码,可以在屏幕上显示当前的年月日(使用DS1302实时时钟模块来获取当前时间):
```c
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define nop() _nop_()
#define LCD_RS P2_0
#define LCD_RW P2_1
#define LCD_EN P2_2
#define LCD_DAT P1
void delay(uint z) {
uint x, y;
for (x = z; x > 0; x--)
for (y = 110; y > 0; y--);
}
void write_com(uchar com) {
LCD_RS = 0;
LCD_RW = 0;
nop();
LCD_EN = 1;
nop();
LCD_DAT = com;
delay(5);
LCD_EN = 0;
delay(5);
}
void write_data(uchar dat) {
LCD_RS = 1;
LCD_RW = 0;
nop();
LCD_EN = 1;
nop();
LCD_DAT = dat;
delay(5);
LCD_EN = 0;
delay(5);
}
void init() {
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
void write_str(uchar *p) {
while (*p != '\0') {
write_data(*p);
p++;
}
}
void write_time() {
uchar year, month, day;
DS1302_Init();
year = DS1302_Get_Year();
month = DS1302_Get_Month();
day = DS1302_Get_Date();
write_com(0x80 + 0x40);
write_str("20");
write_data(year / 10 + '0');
write_data(year % 10 + '0');
write_data('-');
write_data(month / 10 + '0');
write_data(month % 10 + '0');
write_data('-');
write_data(day / 10 + '0');
write_data(day % 10 + '0');
}
void main() {
init();
while (1) {
write_time();
delay(1000);
}
}
```
在这个代码中,我们定义了一些常量和变量,以及一些函数。其中,write_com()函数和write_data()函数用来向液晶显示屏写入命令或数据;delay()函数用来延时;init()函数用来初始化液晶显示屏;write_str()函数用来向液晶显示屏写入字符串;write_time()函数用来获取当前时间,并将其显示在液晶显示屏上;main()函数则是程序的主函数,其中的while循环用来不断地更新液晶显示屏上的时间信息。