ayb_ice 发表于 2014-11-6 08:25
你这个错误关键在num1的处理上,这个变量在不停的被中断累加,在主循环的一次循环处理中可能值会发生变化, ...
虽然现在我还没有发现问题出在什么地方,但是我认为应该不是出在num1上,下面我这程序也是用的num++,运行就很正确
#include"reg51.h"
#define uchar unsigned char
#define uint unsigned int
sbit led1=P1^0;
sbit led2=P1^1;
sbit led3=P1^2;
sbit led4=P1^3;
sbit led5=P1^4;
sbit led6=P1^5;
sbit led7=P1^6;
sbit led8=P1^7;
uchar num1,num2;
void main()
{
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
switch(num1)
{
case 0:
while(num1<=20)
{
if(num2==4)
{
num2=0;
led1=~led1;
}
}
case 20:
while(num1<=40)
{
if(num2==4)
{ led1=1;
num2=0;
led2=~led2;
}
}
case 40:
while(num1<=60)
{
if(num2==4)
{ led2=1;
num2=0;
led3=~led3;
}
}
case 60:
while(num1<=80)
{
if(num2==4)
{ led3=1;
num2=0;
led4=~led4;
}
}
case 80:
while(num1<=100)
{
if(num2==4)
{ led4=1;
num2=0;
led5=~led5;
}
}
case 100:
while(num1<=120)
{
if(num2==4)
{ led5=1;
num2=0;
led6=~led6;
}
}
case 120:
while(num1<=140)
{
if(num2==4)
{ led6=1;
num2=0;
led7=~led7;
}
}
case 160:
while(num1<=180)
{
if(num2==4)
{ led7=1;
num2=0;
led8=~led8;
}
}led8=1;num1=0;break;
}
}
}
void T0_time()interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num1++;
num2++;
}
|