51单片机lcd1602和ds1302
时间: 2024-01-21 14:03:55 浏览: 34
51单片机可以通过与LCD1602液晶模块和DS1302实时时钟模块的连接来实现显示和时间功能。下面是一个简单的示例代码:
```c
#include <reg52.h>
#include <intrins.h>
#define LCD1602_DB P0
sbit LCD1602_RS = P2^0;
sbit LCD1602_RW = P2^1;
sbit LCD1602_E = P2^2;
void delay(unsigned int t)
{
while(t--);
}
void LCD1602_WriteCmd(unsigned char cmd)
{
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_E = 1;
_nop_();
_nop_();
LCD1602_E = 0;
delay(100);
}
void LCD1602_WriteData(unsigned char dat)
{
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
_nop_();
_nop_();
LCD1602_E = 0; delay(100);
}
void LCD1602_Init()
{
LCD1602_WriteCmd(0x38); // 设置显示模式为2行,5x7点阵
LCD1602_WriteCmd(0x0C); // 显示开,光标关闭
LCD1602_WriteCmd(0x06); // 光标右移,不移动显示
LCD1602_WriteCmd(0x01); // 清屏
}
void LCD1602_DisplayString(unsigned char x, unsigned char y, unsigned char *str)
{
unsigned char addr;
if (y == 0)
addr = 0x80 + x;
else
addr = 0xC0 + x;
LCD1602_WriteCmd(addr);
while (*str != '\0')
{
LCD1602_WriteData(*str);
str++;
}
}
void DS1302_WriteByte(unsigned char dat)
{
unsigned char i;
for (i = 0; i < 8; i++)
{
P2 = 0x00;
P0 = dat & 0x01;
P2 = 0x01;
dat >>= 1;
}
}
unsigned char DS1302_ReadByte()
{
unsigned char i, dat = 0;
for (i = 0; i < 8; i++)
{
P2 = 0x00;
_nop_();
_nop_();
dat >>= 1;
if (P0 & 0x01)
dat |= 0x80;
P2 = 0x01;
}
return dat;
}
void DS1302_Write(unsigned char addr, unsigned char dat)
{
P2 = 0x80;
DS1302_WriteByte(addr);
DS1302_WriteByte(dat);
P2 = 0x00;
}
unsigned char DS1302_Read(unsigned char addr)
{
unsigned char dat;
P2 = 0x80;
DS1302_WriteByte(addr);
dat = DS1302_ReadByte();
P2 = 0x00;
return dat;
}
void DS1302_SetTime(unsigned char year, unsigned char month, unsigned char day, unsigned char hour, unsigned char minute, unsigned char second)
{
DS1302_Write(0x8E, 0x00); // 关闭写保护
DS1302_Write(0x80, second);
DS1302_Write(0x82, minute);
DS1302_Write(0x84, hour);
DS1302_Write(0x86, day);
DS1302_Write(0x88, month);
DS1302_Write(0x8C, year);
DS1302_Write(0x8E, 0x80); // 打开写保护
}
void DS1302_GetTime(unsigned char *year, unsigned char *month, unsigned char *day, unsigned char *hour, unsigned char *minute, unsigned char *second)
{
*second = DS1302_Read(0x81);
*minute = DS1302_Read(0x83);
*hour = DS1302_Read(0x85);
*day = DS1302_Read(0x87);
*month = DS1302_Read(0x89);
*year = DS1302_Read(0x8D);
}
void main()
{
unsigned char year, month, day, hour, minute, second;
unsigned char str[17];
LCD1602_Init();
DS1302_SetTime(21, 6, 1, 12, 0, 0); // 设置时间为2021年6月1日12时0分0秒
while (1)
{
DS1302_GetTime(&year, &month, &day, &hour, &minute, &second);
sprintf(str, "%02x-%02x-%02x", year, month, day);
LCD1602_DisplayString(0, 0, str);
sprintf(str, "%02x:%02x:%02x", hour, minute, second);
LCD1602_DisplayString(0, 1, str);
delay(1000);
}
}
```
这段代码实现了51单片机与LCD1602液晶模块和DS1302实时时钟模块的连接和操作。其中,LCD1602_WriteCmd和LCD1602_WriteData函数用于向LCD1602发送指令和数据,LCD1602_Init函数用于初始化LCD1602,LCD1602_DisplayString函数用于在LCD1602上显示字符串。DS1302_WriteByte和DS1302_ReadByte函数用于向DS1302写入字节和读取字节,DS1302_Write和DS1302_Read函数用于向DS1302写入数据和读取数据,DS1302_SetTime和DS1302_GetTime函数用于设置和获取DS1302的时间。