51单片机按键控制流水灯左移右移
代码如下
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit key_1 = P3^7;
uchar Led_L,Led_R;
uint flag=1;
uint flag_start=1;
void delay(uint i)
{
uint j;
for(i;i>0;i--)
for(j=110;j>0;j--);
}
void getkey()
{
if(key_1==0)
{
delay(10);
if(key_1==0)
{
while(!key_1);
flag=!flag;
flag_start = 0;
}
}
}
void main()
{
Led_L=0xfe;
Led_R=0x7f;
while(1){
do{
getkey();
}while(flag_start);
if(flag==0)
{
P2=Led_L;
delay(500);
Led_L=_crol_(Led_L,1);
}
if(flag==1)
{
P2=Led_R;
delay(500);
Led_R=_cror_(Led_R,1);
}
}
}