当前位置:网站首页>(原创)制作一个采用 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相连的按钮来发生改变。
源码和仿真链接
链接: 点击前往下载
边栏推荐
- 5.函数递归练习
- System design learning (III) design Amazon's sales rank by category feature
- hashCode()与equals()之间的关系
- 2. Preliminary exercises of C language (2)
- 9.指针(上)
- The latest tank battle 2022 - Notes on the whole development -2
- 【话题终结者】
- 8. C language - bit operator and displacement operator
- 1.初识C语言(1)
- 魏牌:产品叫好声一片,但为何销量还是受挫
猜你喜欢

Inheritance and polymorphism (I)

Questions and answers of "signal and system" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology

3. C language uses algebraic cofactor to calculate determinant

Alibaba cloud microservices (IV) service mesh overview and instance istio

13 power map

Mortal immortal cultivation pointer-2

E-R graph to relational model of the 2022 database of tyut Taiyuan University of Technology

View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件

Arduino+ water level sensor +led display + buzzer alarm

Cookie和Session的区别
随机推荐
(super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow
Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
Implement queue with stack
Set container
凡人修仙学指针-2
8.C语言——位操作符与位移操作符
MPLS experiment
Voir ui plus version 1.3.1 pour améliorer l'expérience Typescript
[中国近代史] 第五章测验
Branch and loop statements
TYUT太原理工大学2022数据库之关系代数小题
Share a website to improve your Aesthetics
C language to achieve mine sweeping game (full version)
165. Compare version number - string
Database operation of tyut Taiyuan University of technology 2022 database
Cloud native trend in 2022
Atomic and nonatomic
MySQL中count(*)的实现方式
C语言入门指南
4.30动态内存分配笔记