把下列代码中的正计时改成倒计时。#include <reg51.h> unsigned char code table[] = { 0xFC, 0x60, 0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE0, 0xFE, 0xF6, 0xEE, 0x3E, 0x9C, 0x7A, 0x9E, 0x8E}; unsigned char i, j, flag,Max; char Count; bit Sym=0; sbit Srg=P1^2; sbit Nrg=P1^4; sbit Yel=P1^3; void putch(unsigned char n) { SBUF=n; while(TI==0); TI=0; } void main() { TMOD=0X01; EA=1; ET0=1; TH0=(65536-50000)/256; TL0=(65536-50000)%256; TR0=1; flag=0; Count=0; Yel=Srg=Nrg=0; Max=10; i=table[Count/10]; j=table[Count%10]; putch(i); putch(j); while(1) { if(Max==10){ if(Sym){ Nrg=1; Srg=0; Yel=0; } else{ Srg=1; Nrg=0; Yel=0; } } else{ Yel=1; Srg=0; Nrg=0; } } } void Timer0() interrupt 1 { flag++; TH0=(65536-50000)/256; TL0=(65536-50000)%256; if(flag==20){ flag=0; Count++; } if(Count>=Max){ Count=0; if(Max==10){ Max=3; Yel=0; Sym=~Sym; } else { Yel=0; Max=10; } } i=table[Count/10]; j=table[Count%10]; putch(i); putch(j); }
时间: 2024-04-25 11:27:14 浏览: 83
将代码中的 Count 和 Max 的初始值都设为需要倒计时的数值,然后将 Count 和 Max 递减即可实现倒计时功能。同时需要将倒计时的数字转换为对应的数码管显示值,可以参考代码中的 table 数组。修改后的代码如下:
#include <reg51.h>
unsigned char code table[] = {
0xFC, 0x60, 0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE0,
0xFE, 0xF6, 0xEE, 0x3E, 0x9C, 0x7A, 0x9E, 0x8E
};
unsigned char i, j, flag;
char Count, Max;
bit Sym = 0;
sbit Srg = P1^2;
sbit Nrg = P1^4;
sbit Yel = P1^3;
void putch(unsigned char n) {
SBUF=n;
while(TI==0);
TI=0;
}
void main() {
TMOD=0X01;
EA=1;
ET0=1;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
flag=0;
Count=30; // 初始倒计时为 30 秒
Max=Count;
while(1) {
i = table[Count/10];
j = table[Count%10];
putch(i);
putch(j);
}
}
void Timer0() interrupt 1 {
flag++;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
if(flag==20) {
flag=0;
Count--; // 每 1 秒递减
}
if(Count <= 0) { // 倒计时结束
Yel = 1;
Srg = 0;
Nrg = 0;
Count = Max;
} else if(Count <= 10) { // 倒计时最后 10 秒
Yel = 1;
Srg = 0;
Nrg = 0;
if(Sym) {
Nrg = 1;
} else {
Srg = 1;
}
Sym = ~Sym;
} else {
Yel = 0;
Srg = 0;
Nrg = 0;
}
i = table[Count/10];
j = table[Count%10];
putch(i);
putch(j);
}
阅读全文