51单片机整点报时代码和仿真
时间: 2024-05-13 10:06:00 浏览: 53
51单片机检测代码以及仿真图
以下是51单片机整点报时的代码和仿真:
代码:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[] = {
0x7f, 0x41, 0x3e, 0x79, 0x47, 0x6d, 0x6f, 0x61, 0x7f, 0x6f, //0~9
0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x3d, 0x76, 0x06, 0x1f, //A~J
0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x3d, 0x76, 0x40, 0x00, //K~S
0x38, 0x3f, 0x73, 0x63, 0x00, 0x00, 0x00, 0x5b //T~Z, 空格
};
uchar code time[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, //0~9
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00 //1~9, 0
};
uchar sec = 0; //秒
uchar min = 0; //分
uchar hour = 0; //时
sbit LSA = P2^2;
sbit LSB = P2^3;
sbit LSC = P2^4;
void delay(uint i) {
while(i--);
}
void display(uchar num) {
LSA = table[num] & 0x01;
LSB = table[num] & 0x02;
LSC = table[num] & 0x04;
}
void main() {
TMOD = 0x01; //设置定时器0为模式1
TH0 = 0x4c; //设置定时器初值
TL0 = 0x00;
TR0 = 1; //启动定时器
ET0 = 1; //开启定时器中断
EA = 1; //开启总中断
while(1) {
display(hour / 10);
delay(1000);
display(hour % 10);
delay(1000);
display(40); //显示冒号
delay(500);
display(min / 10);
delay(1000);
display(min % 10);
delay(1000);
display(40); //显示冒号
delay(500);
display(sec / 10);
delay(1000);
display(sec % 10);
delay(1000);
}
}
void timer0() interrupt 1 {
TH0 = 0x4c; //重新设置定时器初值
TL0 = 0x00;
sec++; //秒加1
if(sec == 60) { //一分钟过去了
sec = 0;
min++; //分加1
if(min == 60) { //一小时过去了
min = 0;
hour++; //时加1
if(hour == 24) { //一天过去了
hour = 0;
}
}
}
}
仿真:
![image](https://user-images.githubusercontent.com/87290316/127749071-1e62c3e3-4d4c-4aa9-9a2d-7ea0f8e0c5b5.png)
阅读全文