一、倒计数的实现
1、原理分析
倒计数的段选信号需要由一个数组存放起来:
code uchar table[]={
0x6F, 0x7F, 0x07, 0x7D, 0x6D, 0x66, 0x4F, 0x5B, 0x06, 0x3F};
然后需要用到定时器中断来实现数值的计算,所以要先初始化定时器,此处只用了一个定时器中断0;其实也可以由两个定时器中断来实现,只是需要两个中断服务函数来做,也就是一个典型的计数器。
初始化定时中断0:
void Init()
{
TMOD = 0x02;
ET0 = 1; //可用定时器0
TR0 = 1; //运行定时器0
TH0 = 156;
TL0 = 156;
EA = 1;
}
在定时器中断0服务函数中对数值进行计算,来统计时间:
void timer() interrupt 1
{
t++;
if(t==10000) //说明1秒时间到
{
t=0;
shu++;
if(shu==100) //进位到99,则重新置零
{
shu=0;
}
m= shu%10; //得到个位下标
n= shu/10; //得到十位下标
}
}
2、完整参考代码
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
void Init(); //时钟初始化
void dis(uint m, uint n); //显示函数
void delay(uint x);
sbit dula= P2^6;
sbit wela= P2^7;
// 9到0的段选信号
code uchar table[]={
0x6F, 0x7F, 0x07, 0x7D, 0x6D, 0x66, 0x4F, 0x5B, 0x06, 0x3F}