基于stc89c52RC单片机的倒计时的代码
时间: 2024-01-10 18:02:55 浏览: 84
51单片机STC89C52RC开发板实验:元旦倒计时(串口传输倒计时信息)程序源代码
以下是基于STC89C52RC单片机的倒计时代码:
```c
#include <reg52.h>
#include <intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define LED P0
sbit KEY1 = P3^2;
sbit KEY2 = P3^3;
sbit KEY3 = P3^4;
uchar code table[]={
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90
};
uint count = 0;
void delayms(uint xms) {
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void display() {
uchar i,j;
for(i=0;i<6;i++) {
switch(i) {
case 0:
j = count / 60000;
break;
case 1:
j = count % 60000 / 10000;
break;
case 2:
j = count % 10000 / 1000;
break;
case 3:
j = count % 1000 / 100;
break;
case 4:
j = count % 100 / 10;
break;
case 5:
j = count % 10;
break;
}
LED = table[j];
_nop_();
_nop_();
_nop_();
LED = 0x00;
delayms(1);
}
}
void main() {
KEY1 = 1;
KEY2 = 1;
KEY3 = 1;
while(1) {
if(KEY1 == 0) {
delayms(5);
if(KEY1 == 0) {
while(!KEY1);
count += 60000;
}
}
if(KEY2 == 0) {
delayms(5);
if(KEY2 == 0) {
while(!KEY2);
count += 1000;
}
}
if(KEY3 == 0) {
delayms(5);
if(KEY3 == 0) {
while(!KEY3);
count = 0;
}
}
display();
}
}
```
该代码使用了STC89C52RC单片机,通过按键控制倒计时时间,并在LED数码管上显示倒计时的时间。按一次KEY1加1分钟,按一次KEY2加1秒钟,按一次KEY3清零倒计时时间。
阅读全文