用Keil uVision4写一个程序

用Keil uVision4写一个程序:用AT89C51芯片。控制8个LED从左到由每两个灯为一组依次循环显示,每组显示持续时间 分别为0.5S、1.0S、1.5S、2.0S。

#include <reg51.h>
#define LED P2
unsigned char time_flag;
void InitTimer0(void)
{
TMOD = 0x01;
TH0 = 0x3C;
TL0 = 0x0B0;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void main(void)
{
InitTimer0();
while(1)
{
if(time_flag==100) time_flag =0;
switch(time_flag/10)
{
case 0: LED = 0xfc;break;
case 1:
case 2: LED = 0xf3;break;
case 3:
case 4:
case 5: LED = 0xcf;break;
case 6:
case 7:
case 8:
case 9: LED = 0x3f;break;
default:LED = 0xff;
}
}
}
void Timer0Interrupt(void) interrupt 1
{
TH0 = 0x3C;
TL0 = 0x0B0;
//add your code here!
time_flag++;
}

晶振12M。定时器是50ms中断一次。

追问

不行啊,用软件测试时间上挺快的。

追答

哦,晶振是11.0592M的。

温馨提示:内容为网友见解,仅供参考
第1个回答  2013-04-28
用中断吧
相似回答