51单片机的问题,本人小白一个,请各位大神帮帮忙
#include<reg52.h>sbitKEY4=P3^3;unsignedcharcodeLedChar[]={0x3f,0x06,0x5b,0x4f,0x66,0x...
#include<reg52.h>
sbit KEY4 = P3^3;
unsigned char code LedChar[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
bit KeySta = 1;
void main()
{
bit backup = 1;
unsigned char cnt = 0;
EA = 1;
TMOD = 0x01;
TH0 = 0xF8;
TL0 = 0xCD;
ET0 = 1;
TR0 = 1;
P0 = LedChar[cnt];
while(1)
{
if(KeySta != backup)
{
if(backup == 0)
{
cnt++;
if(cnt >= 16)
{
cnt = 0;
}
P0 = LedChar[cnt];
}
backup = KeySta;
}
}
}
void InterruptTimer0() interrupt 1
{
static unsigned char keybuf = 0xFF;
TH0 = 0xF8;
TL0 = 0xCD;
keybuf = (keybuf <<1) |KEY4;
if(keybuf == 0x00)
{
KeySta = 0;
}
else if(keybuf == 0xFF)
{
KeySta = 1;
}
else
{
}
}
它的运行顺序是不是,首先在main中运行TH0.TL0 中的2毫秒。然后TF0=1.调用中断函数,TF0自动清零,然后运行while(1)死循环中。每当2毫秒调用一次中断函数。程序一直在while(1)中死循环。~~~~这么理解对吗 展开
sbit KEY4 = P3^3;
unsigned char code LedChar[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
bit KeySta = 1;
void main()
{
bit backup = 1;
unsigned char cnt = 0;
EA = 1;
TMOD = 0x01;
TH0 = 0xF8;
TL0 = 0xCD;
ET0 = 1;
TR0 = 1;
P0 = LedChar[cnt];
while(1)
{
if(KeySta != backup)
{
if(backup == 0)
{
cnt++;
if(cnt >= 16)
{
cnt = 0;
}
P0 = LedChar[cnt];
}
backup = KeySta;
}
}
}
void InterruptTimer0() interrupt 1
{
static unsigned char keybuf = 0xFF;
TH0 = 0xF8;
TL0 = 0xCD;
keybuf = (keybuf <<1) |KEY4;
if(keybuf == 0x00)
{
KeySta = 0;
}
else if(keybuf == 0xFF)
{
KeySta = 1;
}
else
{
}
}
它的运行顺序是不是,首先在main中运行TH0.TL0 中的2毫秒。然后TF0=1.调用中断函数,TF0自动清零,然后运行while(1)死循环中。每当2毫秒调用一次中断函数。程序一直在while(1)中死循环。~~~~这么理解对吗 展开
1个回答
展开全部
//在程序上给你说一下执行顺序
void main() //上电时,程序从0000H开始,一般这里放一个跳转语句,跳转到这里开始
{
bit backup = 1;
unsigned char cnt = 0;
EA = 1; //这是第一条有用的执行语句
TMOD = 0x01;
TH0 = 0xF8;
TL0 = 0xCD;
ET0 = 1;
TR0 = 1; //从这里开始,定时器开始工作,与以下语句是并行工作的。
P0 = LedChar[cnt]; //开启定时器后仅仅执行了这条语句就进入“死循环”里面了
while(1) //以下循环体中执行,定时器每2毫秒检测一次按键。
{
if(KeySta != backup)
{
if(backup == 0)
{
cnt++;
if(cnt >= 16)
{
cnt = 0;
}
P0 = LedChar[cnt];
}
backup = KeySta;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询