#include <reg52.h> #define uchar unsigned char #define uint unsigned int sbit RS = P1 ^ 0; sbit EN = P1 ^ 2; sbit RW = P1 ^ 1; uchar count; uchar miao, shi, fen; uchar code tab1[] = "Electronic Clock"; uchar code tab2[] = " 14:00:00"; void delay(uint t) { uint x, y; for (x = t; x > 0; x--) for (y = 100; y > 0; y--); } void WrOp(uchar com) { RS = 0; P0 = com; delay(1); EN = 1; delay(1); EN = 0; } void WrDat(uchar dat) { RS = 1; P0 = dat; delay(1); EN = 1; delay(1); EN = 0; } void LCD_Init() { uchar num; RW = 0; WrOp(0x38); WrOp(0x0c); WrOp(0x06); WrOp(0x01); WrOp(0x80); for (num = 0; num < 16; num++) { WrDat(tab1[num]); delay(1); } WrOp(0x80 + 0x40); for (num = 0; num < 12; num++) { WrDat(tab2[num]); delay(1); } } void Out_Char(uchar add, uchar date) { uchar shi, ge; shi = date / 10; ge = date % 10; WrOp(0x80 + 0x40 + add); WrDat(0x30 + shi); WrDat(0x30 + ge); } void main() { TMOD = 0x01; TH0 = (65536 - 50000) / 256; TL0 = (65536 - 50000) % 256; EA = 1; ET0 = 1; TR0 = 1; LCD_Init(); while (1); } void timer0() interrupt 1 { TH0 = (65536 - 60000) / 256; TL0 = (65536 - 60000) % 256; count++; if (count == 20) { count = 0; miao++; if (miao == 60) { miao = 0; fen++; if (fen == 60) { fen = 0; shi++; if (shi == 24) { shi = 0; } Out_Char(4, shi); } Out_Char(7, fen); } Out_Char(10, miao); } }在上列代码中添加4个按键,分别是模式按键(功能是切换模式,正常模式和设置闹钟时间模式)、移位按键(在闹钟模式下起作用,用来切换当前设置闹钟时间的位置,分别是:时、分、秒)、增加按键(在闹钟模式下起作用,增加值)、减小按键(在闹钟模式下起作用,减小值)
时间: 2023-10-24 11:08:43 浏览: 110
以下为代码修改部分:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS = P1 ^ 0;
sbit EN = P1 ^ 2;
sbit RW = P1 ^ 1;
sbit mode_key = P2 ^ 0; // 模式按键
sbit shift_key = P2 ^ 1; // 移位按键
sbit add_key = P2 ^ 2; // 增加按键
sbit sub_key = P2 ^ 3; // 减小按键
uchar count;
uchar miao, shi, fen;
uchar alarm_miao, alarm_shi, alarm_fen; // 闹钟时间
uchar code tab1[] = "Electronic Clock";
uchar code tab2[] = " 14:00:00";
uchar mode = 0; // 模式标志位,0为正常模式,1为设置闹钟时间模式
uchar shift = 0; // 移位标志位,0为时位,1为分位,2为秒位
void delay(uint t) {
uint x, y;
for (x = t; x > 0; x--)
for (y = 100; y > 0; y--);
}
void WrOp(uchar com) {
RS = 0;
P0 = com;
delay(1);
EN = 1;
delay(1);
EN = 0;
}
void WrDat(uchar dat) {
RS = 1;
P0 = dat;
delay(1);
EN = 1;
delay(1);
EN = 0;
}
void LCD_Init() {
uchar num;
RW = 0;
WrOp(0x38);
WrOp(0x0c);
WrOp(0x06);
WrOp(0x01);
WrOp(0x80);
for (num = 0; num < 16; num++) {
WrDat(tab1[num]);
delay(1);
}
WrOp(0x80 + 0x40);
for (num = 0; num < 12; num++) {
WrDat(tab2[num]);
delay(1);
}
}
void Out_Char(uchar add, uchar date) {
uchar shi, ge;
shi = date / 10;
ge = date % 10;
WrOp(0x80 + 0x40 + add);
WrDat(0x30 + shi);
WrDat(0x30 + ge);
}
void main() {
TMOD = 0x01;
TH0 = (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
EA = 1;
ET0 = 1;
TR0 = 1;
LCD_Init();
while (1) {
if (mode_key == 0) { // 模式按键按下
while (mode_key == 0); // 等待松开
mode = !mode; // 切换模式
if (mode == 1) { // 进入设置闹钟时间模式
shift = 0; // 初始位置为时位
alarm_shi = shi; // 闹钟时间初始值为当前时间
alarm_fen = fen;
alarm_miao = 0;
}
}
if (mode == 1) { // 闹钟时间设置模式
if (shift_key == 0) { // 移位按键按下
while (shift_key == 0); // 等待松开
shift++; // 移位
if (shift == 3) shift = 0; // 移到最后一位时回到第一位
}
if (add_key == 0) { // 增加按键按下
while (add_key == 0); // 等待松开
switch (shift) {
case 0: // 时位
alarm_shi++;
if (alarm_shi == 24) alarm_shi = 0;
break;
case 1: // 分位
alarm_fen++;
if (alarm_fen == 60) alarm_fen = 0;
break;
case 2: // 秒位
alarm_miao++;
if (alarm_miao == 60) alarm_miao = 0;
break;
}
}
if (sub_key == 0) { // 减小按键按下
while (sub_key == 0); // 等待松开
switch (shift) {
case 0: // 时位
if (alarm_shi == 0) alarm_shi = 23;
else alarm_shi--;
break;
case 1: // 分位
if (alarm_fen == 0) alarm_fen = 59;
else alarm_fen--;
break;
case 2: // 秒位
if (alarm_miao == 0) alarm_miao = 59;
else alarm_miao--;
break;
}
}
Out_Char(4, alarm_shi); // 显示闹钟时间
Out_Char(7, alarm_fen);
Out_Char(10, alarm_miao);
}
}
}
void timer0() interrupt 1 {
TH0 = (65536 - 60000) / 256;
TL0 = (65536 - 60000) % 256;
count++;
if (count == 20) {
count = 0;
miao++;
if (miao == 60) {
miao = 0;
fen++;
if (fen == 60) {
fen = 0;
shi++;
if (shi == 24) {
shi = 0;
}
if (mode == 0 && shi == alarm_shi && fen == alarm_fen && miao == alarm_miao) { // 判断是否到达闹钟时间
WrOp(0x01); // 清屏
WrOp(0x80);
WrDat('A');
WrDat('L');
WrDat('A');
WrDat('R');
WrDat('M');
}
}
if (mode == 0) Out_Char(7, fen); // 正常模式下显示当前时间
}
if (mode == 0) Out_Char(10, miao);
}
}
阅读全文