当前位置:网站首页>(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
2022-07-06 09:20:00 【axu_990707】
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:
(1)k1——进入时间修改。
(2)k2——修改时数,按一下 k2,当前时数增1。
(3)k3——修改分数,按一下 k3,当前分数增 1。
(4)k4——确认修改完成,电子钟按修改后的时间运行显示。
源码和仿真链接
链接: 点击前往下载
以下是程序代码
//头文件部分 名称见主程序 需要创建 .h 文件
//不会使用请直接下载源码 链接在文章开头和结尾都有
#include<intrins.h>
sbit RS=P2^6;
sbit RW=P2^5;
sbit E=P2^7;
unsigned char j=20;
unsigned char hour = 23;
unsigned char minute = 59;
unsigned char second = 58;
sbit P30=P3^0;
sbit P31=P3^1;
sbit P32=P3^2;
sbit P33=P3^3;
void delay(unsigned char time){
unsigned char i,j,k;
for(k=0;k<time;k++){
for(i=0;i<20;i++)
for(j=0;j<15;j++);
}
}
void delay1(unsigned char time){
unsigned char i,j,k;
for(k=0;k<time;k++){
for(i=0;i<255;i++)
for(j=0;j<255;j++);
}
}
void delay2(){
unsigned char i,j;
for(i=0;i<255;i++)
for(j=0;j<255;j++);
}
void write_command(unsigned char com){
E=0;
RS=0;
RW=0;
P0=com;
E=1;
_nop_();
E=0;
delay(1);
}
void lcd_initial(void){
write_command(0x38);
write_command(0x0c);
write_command(0x06);
write_command(0x01);
delay(1);
}
void write_data(unsigned char dat){
E=0;
RS=1;
RW=0;
P0=dat;
E=1;
_nop_();
E=0;
delay(1);
}
void string(unsigned char ad,unsigned char *s){
write_command(ad);
while(*s>0){
write_data(*s++);
delay(4);
}
}
void string1(unsigned char ad,unsigned char s){
unsigned char ge = s%10;
unsigned char shi = s/10;
write_command(ad);
write_data(shi+48);
delay(4);
write_data(ge+48);
delay(4);
}
void showSchHo(void){
string(0x80,"schooling hours");
string(0xc0,"Time");
string1(0xc5,hour);
string(0xc7,":");
string1(0xc8,minute);
string(0xca,":");
string1(0xcb,second);
}
void showOK(void){
string(0x80,"...IT IS OK!...");
string(0xc0,"Time");
string1(0xc5,hour);
string(0xc7,":");
string1(0xc8,minute);
string(0xca,":");
string1(0xcb,second);
}
void showCa(void){
string(0x80,".....Casio.....");
string(0xc0,"Time");
string1(0xc5,hour);
string(0xc7,":");
string1(0xc8,minute);
string(0xca,":");
string1(0xcb,second);
}
#include<reg51.h>
#include<LCD1602Time.h>
void main(){
IT0=1;
EA=1;
EX0=1;
TR0=1;
ET0=1;
TMOD=0x01;
TH0=0x4c;
TL0=0x00;
lcd_initial();
while(1){
showCa();
}
}
void int0(void) interrupt 0 {
unsigned char key = 1;
showSchHo();
while(key){
if(P30 == 0){
delay2();
if(P30 == 0){
hour++;
showSchHo();
if(hour == 24){
hour = 0;
showSchHo();
}
}
}
if(P31 == 0){
delay2();
if(P31 == 0){
minute++;
showSchHo();
if(minute == 60){
minute = 0;
showSchHo();
}
}
}
if(P33 == 0){
delay2();
if(P33 == 0){
key = 0;
showOK();
delay1(3);
}
}
}
}
void time(void) interrupt 1 {
TH0=0x4c;
TL0=0x00;
j--;
if(j==0){
second++;
if(second == 60){
second = 0;
minute++;
if(minute == 60){
minute = 0;
hour++;
if(hour == 24){
hour = 0;
}
}
}
j = 20;
}
}
仿真图片: 
总结
主程序一直显示时,分,秒三个变量到1602上,无论时分秒三个变量的值是否改变,每当显数任务完成后就会立即刷新一次。这样,只要时分秒变量改变,程序会立即做出响应。而时分秒三变量的值通过定时和int0中断一起完成的。在正常计数状态,三变量的值通过定时中断进行增加/置零。当按下按钮1(也就是int0中断)后,三变量的值倚靠P30、P31、P33相连的按钮来发生改变。
源码和仿真链接
链接: 点击前往下载
边栏推荐
- Change vs theme and set background picture
- Database operation of tyut Taiyuan University of technology 2022 database
- Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
- 8. C language - bit operator and displacement operator
- Aurora system model of learning database
- (super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow
- Decomposition relation model of the 2022 database of tyut Taiyuan University of Technology
- 凡人修仙学指针-2
- 【毕业季·进击的技术er】再见了,我的学生时代
- vector
猜你喜欢
随机推荐
Branch and loop statements
C语言实现扫雷游戏(完整版)
魏牌:产品叫好声一片,但为何销量还是受挫
Set container
View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)
What are the advantages of using SQL in Excel VBA
抽象类和接口的区别
The latest tank battle 2022 - Notes on the whole development -2
4.30 dynamic memory allocation notes
Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
TYUT太原理工大学往年数据库简述题
TYUT太原理工大学2022数据库大题之概念模型设计
167. Sum of two numbers II - input ordered array - Double pointers
凡人修仙学指针-2
13 power map
Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
TYUT太原理工大学2022软工导论考试题型大纲
Change vs theme and set background picture
System design learning (III) design Amazon's sales rank by category feature









