设计一个节目彩灯控制器,在单片机的p0口接有8个发光二极管作为指示灯,p1.0~p1.3接有4个按键开关。

2024-11-01 19:55:36
推荐回答(1个)
回答1:

#include
#include
#define uchar unsigned char
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
sbit key4=P1^3;
uchar led=0xfe,led1=0x00;
void delay(uchar a)
{
uchar i;
while(a--)for(i=0;i<120;i++);
}
main()
uchar keyval=0;
{
while(1)
{
if(key1==0);
{
while(key1==0);
keyval=1;
}
if(key2=0);
{
while(key2==0);
keyval=2;
}
if(key3==0);
{
while(key3==0);
keyval=3;
}
if(key4==0);
{
while(key4==0);
keyval=4;
}
switch(keyval)
{
case 1:
P0=led1;
led1=~led1;
delay(100);
break;
case 2:
P0=0xff;
break;
case 3:
P0=led;
led=_crol_(led,1);
delay(100);
break;
case 4:
P0=led;
led=_cror_(led,1);
delay(100);
break;
}
}
}