大佬们好,如果定时器T0已经初始化后,开始运行,这时候我一直初始化会发生什么样的情况。原本代码是将Timert0_init(); 放在while外面上电初始化,但是我想试一试放在按键里面会是什么样的效果。结果就是按键1需要一直按下,按键2只需点动,按键3,4在初始化后与按键2一样的效果。有没有大佬解释一下其中的原理。感谢
下面是单片机测试代码:
#include "regx52.h"
#include "timer0.h"
#include "keynum.h"
#include "intrins.h"
#include "Delay.h"
unsigned char wl,mode;//waterfall light 流水灯
void main()
{
P2=0xfe;
while(1)
{
wl=keynum();
if(wl>0)
{
if(wl==1)
{
mode=1;
Timert0_init(); //这里按键一直按下流水灯一直工作,松手停下。
}
if(wl==2)
{
mode=2;
Delay(1); //这里按键按下,松开流水灯继续工作
Timert0_init();
}
if(wl==3)
{
mode=3;
}
if(wl==4)
{
mode=4;
}
}
}
}
void Timer0_Rountine(void) interrupt 1
{
static unsigned int t0count; //静态函数static,保证退出中断时t0count不清零。
TL0 = 0x66; //设置定时初始值
TH0 = 0xFC; //设置定时初始值 //定时器溢出后的值会变成0,需要重新赋初值;
t0count++;
if(t0count>=1000)
{
t0count=0;
if(mode==1)
{
P2=_crol_(P2,1);
}
if(mode==2)
{
P2=_cror_(P2,1);
}
if(mode==3)
{
P2=_crol_(P2,1);
}
if(mode==4)
{
P2=_cror_(P2,1);
}
}
}
|