实现999999--0的倒数,用T0时可以用模式1;用T1却要用模式2;一个16位、一个8位自动重装,TH、TL的数值一致,为什么结果能一样??T1为什么不能用模式1?
当实现0--999999的正数,T0、T1都可以用模式1,倒数就不行了?为什么?
#include<reg52.h>
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
unsigned char code LedChar[] = {
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E};
unsigned char LedBuff[6] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
unsigned char flagls = 0;
unsigned int cnt = 0;
unsigned char i = 0;
void main()
{
unsigned long sec = 999999;
ENLED = 0;
ADDR3 = 1;
TMOD = 0x10; //模式设置
//当使用T0时可用模式1 (TMOD=0x01)
//但用T1时却不行,倒计时不准,
//要用模式2 (TMOD=0x10)
//为什么?T0、T1应该一样才对吗?
//T0用模式1,16位;T1用模式2,8位自动重装;TH、TL数值一样,为什么结果一样?
//T0、T1都用模式1可以实现正数00-999999,但倒数999999--0只有T0可以,T1要用模式2!为什么?
TH1 = 0xFC;
TL1 = 0x66;
TR1 = 1;
//打开中断
EA = 1;
ET1 = 1;
while(1)
{
if(flagls == 1)
{
flagls = 0;
sec--;
LedBuff[0] = LedChar[sec%10];
LedBuff[1] = LedChar[sec/10%10];
LedBuff[2] = LedChar[sec/100%10];
LedBuff[3] = LedChar[sec/1000%10];
LedBuff[4] = LedChar[sec/10000%10];
LedBuff[5] = LedChar[sec/100000%10];
if(sec == 0)
{
sec = 1;
}
}
}
}
//中断函数,防闪烁
void InterruptTime1() interrupt 3 //T0时为1 T1时为3
{
TH1 = 0xFC;
TL1 = 0x66;
cnt++;
if(cnt >= 1000)
{
cnt = 0;
flagls = 1;
}
P0 = 0xFF;
switch(i)
{
case 0: ADDR2 = 0; ADDR1 = 0; ADDR0 = 0;
i++; P0 = LedBuff[0]; break;
case 1: ADDR2 = 0; ADDR1 = 0; ADDR0 = 1;
i++; P0 = LedBuff[1]; break;
case 2: ADDR2 = 0; ADDR1 = 1; ADDR0 = 0;
i++; P0 = LedBuff[2]; break;
case 3: ADDR2 = 0; ADDR1 = 1; ADDR0 = 1;
i++; P0 = LedBuff[3]; break;
case 4: ADDR2 = 1; ADDR1 = 0; ADDR0 = 0;
i++; P0 = LedBuff[4]; break;
case 5: ADDR2 = 1; ADDR1 = 0; ADDR0 = 1;
i = 0; P0 = LedBuff[5]; break;
default : break;
}
}