使用ds1302,lcd1602,ds18b20和按键,写成带闹钟的可调时钟代码,51单片机
时间: 2024-03-19 19:44:40 浏览: 74
DS1302+18B20+LCM1602显示时钟温度信息红外遥控C51单片机软件源代码.rar
5星 · 资源好评率100%
以下是使用DS1302实时时钟模块、LCD1602液晶屏、DS18B20温度传感器和按键,写成带闹钟的可调时钟代码,适用于51单片机。代码注释中有详细的说明,希望能够帮到你!
```
#include <reg52.h> //引用51单片机头文件
#include <intrins.h> //引用延时函数头文件
//定义液晶屏引脚
sbit LCD_RS = P2^6;
sbit LCD_RW = P2^5;
sbit LCD_EN = P2^7;
sbit LCD_D4 = P0^4;
sbit LCD_D5 = P0^5;
sbit LCD_D6 = P0^6;
sbit LCD_D7 = P0^7;
//定义DS1302实时时钟模块引脚
sbit DS1302_CE = P1^0;
sbit DS1302_CLK = P1^1;
sbit DS1302_DAT = P1^2;
//定义温度传感器引脚
sbit DS18B20_IO = P3^4;
//定义闹钟时间
unsigned char alarmHour = 8;
unsigned char alarmMinute = 0;
//定义闹钟标志
bit alarmSet = 0;
//函数声明
void InitLCD();
void WriteCommand(unsigned char Command);
void WriteData(unsigned char Data);
void LCD1602_Display(unsigned char X,unsigned char Y,unsigned char *Text);
void DS1302_WriteByte(unsigned char dat);
unsigned char DS1302_ReadByte();
void DS1302_Write(unsigned char address,unsigned char dat);
unsigned char DS1302_Read(unsigned char address);
void DS1302_SetTime(unsigned char year,unsigned char month,unsigned char day,unsigned char hour,unsigned char minute,unsigned char second);
void DS1302_GetTime(unsigned char *year,unsigned char *month,unsigned char *day,unsigned char *hour,unsigned char *minute,unsigned char *second);
void DS18B20_Init();
float DS18B20_GetTemp();
void main() {
//初始化LCD1602液晶屏
InitLCD();
//初始化DS1302实时时钟模块
DS1302_SetTime(21, 8, 15, 8, 0, 0); //设置初始时间为2021年8月15日8时0分0秒
//初始化DS18B20温度传感器
DS18B20_Init();
while (1) {
//获取当前时间和温度
unsigned char year, month, day, hour, minute, second;
DS1302_GetTime(&year, &month, &day, &hour, &minute, &second);
float temperature = DS18B20_GetTemp();
//显示时间和温度
unsigned char text[16];
sprintf(text, "Time: %02d:%02d", hour, minute);
LCD1602_Display(0, 0, text);
sprintf(text, "Temp: %.1fC", temperature);
LCD1602_Display(0, 1, text);
//检查是否到达闹钟时间
if (hour == alarmHour && minute == alarmMinute && !alarmSet) {
//闹钟时间到,显示闹钟提示
LCD1602_Display(0, 0, "Alarm!");
LCD1602_Display(0, 1, "Press reset");
alarmSet = 1; //设置闹钟标志
}
//检查是否按下重置按钮
if (!P3_0 && alarmSet) {
//重置闹钟
alarmSet = 0;
}
//检查是否按下设置按钮
if (!P3_1) {
//进入设置模式
unsigned char hourSetting = hour;
unsigned char minuteSetting = minute;
while (!P3_1) {
//等待设置完成
if (!P3_2) {
//按下确认按钮,保存设置
DS1302_SetTime(year, month, day, hourSetting, minuteSetting, 0);
break;
}
//调整时间
if (!P3_3) {
//按下小时调整按钮
hourSetting++;
if (hourSetting > 23) {
hourSetting = 0;
}
sprintf(text, "Set Time: %02d:%02d", hourSetting, minuteSetting);
LCD1602_Display(0, 0, text);
}
if (!P3_4) {
//按下分钟调整按钮
minuteSetting++;
if (minuteSetting > 59) {
minuteSetting = 0;
}
sprintf(text, "Set Time: %02d:%02d", hourSetting, minuteSetting);
LCD1602_Display(0, 0, text);
}
_nop_();
}
}
}
}
//LCD1602初始化函数
void InitLCD() {
WriteCommand(0x38); //设置16*2显示,5*7点阵,8位数据
WriteCommand(0x0c); //显示器开,光标不显示
WriteCommand(0x06); //光标右移,字符不移动
WriteCommand(0x01); //清屏
}
//LCD1602发送命令函数
void WriteCommand(unsigned char Command) {
LCD_RS = 0;
LCD_RW = 0;
LCD_D7 = Command & 0x80;
LCD_D6 = Command & 0x40;
LCD_D5 = Command & 0x20;
LCD_D4 = Command & 0x10;
LCD_EN = 1;
_nop_();
_nop_();
LCD_EN = 0;
_nop_();
_nop_();
LCD_D7 = Command & 0x08;
LCD_D6 = Command & 0x04;
LCD_D5 = Command & 0x02;
LCD_D4 = Command & 0x01;
LCD_EN = 1;
_nop_();
_nop_();
LCD_EN = 0;
_nop_();
_nop_();
_nop_();
}
//LCD1602发送数据函数
void WriteData(unsigned char Data) {
LCD_RS = 1;
LCD_RW = 0;
LCD_D7 = Data & 0x80;
LCD_D6 = Data & 0x40;
LCD_D5 = Data & 0x20;
LCD_D4 = Data & 0x10;
LCD_EN = 1;
_nop_();
_nop_();
LCD_EN = 0;
_nop_();
_nop_();
LCD_D7 = Data & 0x08;
LCD_D6 = Data & 0x04;
LCD_D5 = Data & 0x02;
LCD_D4 = Data & 0x01;
LCD_EN = 1;
_nop_();
_nop_();
LCD_EN = 0;
_nop_();
_nop_();
_nop_();
}
//LCD1602显示函数
void LCD1602_Display(unsigned char X,unsigned char Y,unsigned char *Text) {
unsigned char i;
if (X > 15) {
X = 0;
Y++;
}
if (Y > 1) {
Y = 0;
}
WriteCommand(0x80 + Y * 0x40 + X);
for (i = 0; Text[i] != '\0'; i++) {
WriteData(Text[i]);
}
}
//DS1302写入一个字节函数
void DS1302_WriteByte(unsigned char dat) {
unsigned char i;
for (i = 0; i < 8; i++) {
if (dat & 0x01) {
DS1302_DAT = 1;
} else {
DS1302_DAT = 0;
}
DS1302_CLK = 1;
_nop_();
_nop_();
dat >>= 1;
DS1302_CLK = 0;
_nop_();
_nop_();
}
}
//DS1302读取一个字节函数
unsigned char DS1302_ReadByte() {
unsigned char i;
unsigned char dat = 0;
for (i = 0; i < 8; i++) {
dat >>= 1;
if (DS1302_DAT) {
dat |= 0x80;
}
DS1302_CLK = 1;
_nop_();
_nop_();
DS1302_CLK = 0;
_nop_();
_nop_();
}
return dat;
}
//DS1302写入一个寄存器函数
void DS1302_Write(unsigned char address,unsigned char dat) {
DS1302_CE = 0;
_nop_();
DS1302_CLK = 0;
_nop_();
DS1302_CE = 1;
_nop_();
DS1302_WriteByte(address);
DS1302_WriteByte(dat);
DS1302_CE = 0;
}
//DS1302读取一个寄存器函数
unsigned char DS1302_Read(unsigned char address) {
unsigned char dat;
DS1302_CE = 0;
_nop_();
DS1302_CLK = 0;
_nop_();
DS1302_CE = 1;
_nop_();
DS1302_WriteByte(address);
dat = DS1302_ReadByte();
DS1302_CE = 0;
return dat;
}
//DS1302设置时间函数
void DS1302_SetTime(unsigned char year,unsigned char month,unsigned char day,unsigned char hour,unsigned char minute,unsigned char second) {
DS1302_Write(0x8e,
阅读全文