当前位置:网站首页>(原创)制作一个采用 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相连的按钮来发生改变。
源码和仿真链接
链接: 点击前往下载
边栏推荐
- 1.初识C语言(1)
- IPv6 experiment
- Small exercise of library management system
- 【九阳神功】2018复旦大学应用统计真题+解析
- View UI plus released version 1.3.1 to enhance the experience of typescript
- TYUT太原理工大学2022数据库大题之分解关系模式
- Inheritance and polymorphism (I)
- The overseas sales of Xiaomi mobile phones are nearly 140million, which may explain why Xiaomi ov doesn't need Hongmeng
- 学编程的八大电脑操作,总有一款你不会
- 2. C language matrix multiplication
猜你喜欢
8. C language - bit operator and displacement operator
Common method signatures and meanings of Iterable, collection and list
The latest tank battle 2022 full development notes-1
TYUT太原理工大学2022软工导论大题汇总
fianl、finally、finalize三者的区别
Redis的两种持久化机制RDB和AOF的原理和优缺点
C语言入门指南
(super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow
西安电子科技大学22学年上学期《基础实验》试题及答案
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
随机推荐
Quickly generate illustrations
IPv6 experiment
(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)
(ultra detailed onenet TCP protocol access) arduino+esp8266-01s access to the Internet of things platform, upload real-time data collection /tcp transparent transmission (and how to obtain and write L
[Topic terminator]
Set container
[中国近代史] 第九章测验
Introduction pointer notes
Network layer 7 protocol
Abstract classes and interfaces
【九阳神功】2018复旦大学应用统计真题+解析
ABA问题遇到过吗,详细说以下,如何避免ABA问题
C语言入门指南
【九阳神功】2019复旦大学应用统计真题+解析
Mortal immortal cultivation pointer-2
8. C language - bit operator and displacement operator
Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
【九阳神功】2017复旦大学应用统计真题+解析
【九阳神功】2020复旦大学应用统计真题+解析
Common method signatures and meanings of Iterable, collection and list