用T0外部脉冲计数的仿真
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={ //0~F数组
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar i=0,ge,shi,bai,qian;
uint j=0;
void InitTimer0()
{
TMOD= 0x15; //T0外部脉冲计数,T1定时50ms
TH0 = 0x00;
TL0 = 0x00;
TH1 = 0x3C; //设置定时初值50ms
TL1 = 0xB0; //设置定时初值50ms
TR1 = 1; //定时器1开始计时
TR0 = 1; //定时器0开始计数
EA = 1;
ET1 = 1;
}
void display()
{
static uchar num=0;
P0=0x00; //消隐
switch(num)
{
case 0:
P2=0xfe;
P0=table[qian];
num++;
break;
case 1:
P2=0xfd;
P0=table[bai];
num++;
break;
case 2:
P2=0xfb;
P0=table[shi];
num++;
break;
case 3:
P2=0xf7;
P0=table[ge];
num=0;
break;
}
}
void main()
{
InitTimer0();
while(1)
{
qian = j/1000; // 千位
bai = j%1000/100; // 百位
shi = j%100/10; // 十位
ge = j%10; // 个位
display();
}
}
void timer1() interrupt 3
{
TH1 = 0x3C;
TL1 = 0xB0;
i++;
if(i>=20)//1秒
{
i=0;
j=TH0<<8|TL0;
TH0=TL0=0;
}
}
|