51单片机lcd1602显示时间按键可调时间,带有闹钟功能
时间: 2023-12-13 21:05:52 浏览: 191
实现这个功能,你需要以下硬件和步骤:
硬件:
- 51单片机
- LCD1602液晶显示屏
- DS1302实时时钟芯片
- 4个按键
- 4.7K电阻
- 杜邦线等
步骤:
1. 连接硬件,将LCD1602和DS1302分别与51单片机连接。其中DS1302需要连接时钟、数据和使能端,LCD1602需要连接数据线、使能线、RS和RW端。
2. 初始化DS1302,设置时间并开始计时。
3. 在LCD1602上显示DS1302的时间。
4. 监听按键,当按下某个按键时,根据按键的不同执行不同的操作。比如按下第一个按键进行时间设置,按下第二个按键进行闹钟设置等。
5. 根据需要,设置闹钟并在闹钟时间到达时进行提醒。可以使用蜂鸣器发出声音、LCD1602显示闹钟信息等方式。
具体实现细节和代码可以参考相关的教程和资料。
相关问题
51单片机lcd1602显示时间按键可调时间,带有闹钟功能C代码
以下是一个简单的51单片机LCD1602显示时间并带有闹钟功能的C代码:
```c
#include <reg51.h>
sbit RS = P2^0; // LCD1602 控制线
sbit RW = P2^1; // LCD1602 控制线
sbit EN = P2^2; // LCD1602 控制线
unsigned char hour=0, min=0, sec=0; // 时间变量
unsigned char alarm_hour=0, alarm_min=0; // 闹钟变量
unsigned char flag_alarm=0; // 闹钟标志
void delay_ms(unsigned int ms) // 延时函数
{
unsigned int i, j;
for(i=0; i<ms; i++)
for(j=0; j<100; j++);
}
void lcd_init() // LCD1602 初始化函数
{
RW = 0;
EN = 0;
delay_ms(15);
P0 = 0x38;
EN = 1;
delay_ms(5);
EN = 0;
delay_ms(5);
P0 = 0x38;
EN = 1;
delay_ms(5);
EN = 0;
delay_ms(5);
P0 = 0x38;
EN = 1;
delay_ms(5);
EN = 0;
delay_ms(5);
P0 = 0x0c;
EN = 1;
delay_ms(5);
EN = 0;
delay_ms(5);
P0 = 0x06;
EN = 1;
delay_ms(5);
EN = 0;
delay_ms(5);
P0 = 0x01;
EN = 1;
delay_ms(5);
EN = 0;
delay_ms(5);
}
void lcd_write_cmd(unsigned char cmd) // 写命令函数
{
RS = 0;
P0 = cmd;
EN = 1;
delay_ms(5);
EN = 0;
delay_ms(5);
}
void lcd_write_data(unsigned char dat) // 写数据函数
{
RS = 1;
P0 = dat;
EN = 1;
delay_ms(5);
EN = 0;
delay_ms(5);
}
void lcd_set_pos(unsigned char x, unsigned char y) // 设置光标位置函数
{
unsigned char pos;
if(x == 0)
pos = y;
else
pos = y + 0x40;
lcd_write_cmd(pos | 0x80);
}
void lcd_clear() // 清屏函数
{
lcd_write_cmd(0x01);
delay_ms(5);
}
void display_time() // 显示时间函数
{
lcd_set_pos(0, 0);
lcd_write_data(hour/10+'0');
lcd_write_data(hour%10+'0');
lcd_write_data(':');
lcd_write_data(min/10+'0');
lcd_write_data(min%10+'0');
lcd_write_data(':');
lcd_write_data(sec/10+'0');
lcd_write_data(sec%10+'0');
}
void display_alarm() // 显示闹钟时间函数
{
lcd_set_pos(1, 0);
lcd_write_data(alarm_hour/10+'0');
lcd_write_data(alarm_hour%10+'0');
lcd_write_data(':');
lcd_write_data(alarm_min/10+'0');
lcd_write_data(alarm_min%10+'0');
}
void update_time() // 更新时间函数
{
sec++;
if(sec >= 60)
{
sec = 0;
min++;
if(min >= 60)
{
min = 0;
hour++;
if(hour >= 24)
hour = 0;
}
}
}
void check_alarm() // 检查闹钟函数
{
if(hour == alarm_hour && min == alarm_min && flag_alarm == 0)
{
flag_alarm = 1;
lcd_set_pos(1, 13);
lcd_write_data('A');
lcd_write_data('L');
lcd_write_data('A');
lcd_write_data('R');
lcd_write_data('M');
}
}
void main()
{
lcd_init();
lcd_clear();
lcd_set_pos(0, 3);
lcd_write_data('T');
lcd_write_data('I');
lcd_write_data('M');
lcd_write_data('E');
lcd_set_pos(1, 3);
lcd_write_data('A');
lcd_write_data('L');
lcd_write_data('A');
lcd_write_data('R');
lcd_write_data('M');
while(1)
{
display_time();
display_alarm();
update_time();
check_alarm();
if(P3 == 0x01) // 按键处理
{
delay_ms(10);
if(P3 == 0x01)
{
hour++;
if(hour >= 24)
hour = 0;
lcd_clear();
}
}
if(P3 == 0x02)
{
delay_ms(10);
if(P3 == 0x02)
{
min++;
if(min >= 60)
min = 0;
lcd_clear();
}
}
if(P3 == 0x04)
{
delay_ms(10);
if(P3 == 0x04)
{
alarm_hour++;
if(alarm_hour >= 24)
alarm_hour = 0;
lcd_clear();
}
}
if(P3 == 0x08)
{
delay_ms(10);
if(P3 == 0x08)
{
alarm_min++;
if(alarm_min >= 60)
alarm_min = 0;
lcd_clear();
}
}
}
}
```
在这个代码中,我们使用了一个计时器来更新时间,并使用一个标志变量来检查闹钟是否已经响铃。按键可以调整时间和闹钟时间。当闹钟响铃时,在LCD1602屏幕上显示“ALARM”字样。
使用ds1302,lcd1602,ds18b20和按键,写成带闹钟的可调时钟代码,51单片机
以下是使用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,
阅读全文