老实了,我把程序改了一下让计数器做最简单的计数工作。
#include <REGX51.H>
void Delay(int ms);
void Timer0_Init(void);
void UpdateLED(void);
char LED = 0;
bit direction = 0;
bit auto_mode = 0;
unsigned int counter = 0; // 将计数器移出中断
void main()
{
P2 = 0xFF;
Timer0_Init();
while(1)
{
if(P3_0 == 0)
{
Delay(5);
if(P3_0 == 0)
{
while(P3_0 == 0);
Delay(5);
if(direction != 0) // 如果当前方向不同,只改变方向
{
direction = 0;
}
auto_mode = 1;
}
}
if(P3_1 == 0)
{
Delay(5);
if(P3_1 == 0)
{
while(P3_1 == 0);
Delay(5);
if(direction != 1) // 如果当前方向不同,只改变方向
{
direction = 1;
}
auto_mode = 1;
}
}
// 主循环中处理LED更新
if(counter >= 10)
{
counter = 0;
if(auto_mode)
{
UpdateLED();
}
}
}
}
void Timer0_ISR(void) interrupt 1
{
TH0 = 0xB0;
TL0 = 0x18;
// 只做最简单的计数工作
counter++;
}
void UpdateLED(void)
{
if(direction == 0) // 向右循环
{
LED++;
if(LED >= 8)
{
LED = 0;
}
}
else // 向左循环
{
if(LED == 0)
{
LED = 7; // 从0减到7,实现循环
}
else
{
LED--;
}
}
P2 = ~(0x01 << LED); // 更新LED显示
}
void Timer0_Init(void)
{
TMOD &= 0xF0;
TMOD |= 0x01;
TH0 = 0xB0;
TL0 = 0x18;
ET0 = 1; //允许定时器0中断
EA = 1; //开启总中断
TR0 = 1;
}
void Delay(int ms)
{
int i,j;
while(ms--)
{
for(i=1;i<=ms;i++)
for(j=1;j<=300;j++);
}
}
现在他加了一条要求。当中断切换时,从当前灯切换循环点亮熄灭。我没懂什么意思,这个意思是指关机后启动灯还是在那个位置还是什么意思?
|