正向流水正常进行,一按按钮反向流水就把所有灯都点亮了,为什么?错在哪?
代码
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit k1=P2^0;//按键
uchar code display[9]={0xfe,0xfd,0xfb,
0xf7,0xef,0xdf,
0xbf,0x7f,0xff}; //设置P0口的代码
delay(uint k)//延时
{
uint i,j;
for(i=0;i<k;i++)
for(j=0;j<200;j++);
}
void main()
{
uchar i =0;
P0 = 0xFF;
while(1)
{
if(k1==1)
{
if(i<=7)//当前位置正向流水
{
P0 = display[i];
i++;
delay(200);
}
else //返回第一位
i=0;
}
else
{
if(i>=0)//当前位置反向流水
{
P0 = display[i];
i--;
delay(200);
}
else //返回最后一位
i=7;
}
}
}