K1键流水灯交替闪烁。
K2键流水灯从两边向中间移动闪烁。
K3键流水灯全部闪烁。
K4键流水灯从LED7到LED0流水闪烁。
单片机源程序如下:
#include
sbit S5=P1^4;
sbit S6=P1^5;
sbit S7=P1^6;
sbit S8=P1^7;
unsigned char keyval;
void key_scan(void);
void forward(void);
void backward(void);
void Alter(void);
void blink(void);
void led_delay(void);
void delay10ms(void);
void main(void)
{
keyval=0;
while(1)
{
key_scan();
switch(keyval)
{
case 5:Alter();
break;
case 6:forward();
break;
case 7:blink();
break;
case 8:backward();
break;
}
}
}
void key_scan(void)
{
P1=0xff;
if((P1&0xf0)!=0xf0)
{
delay10ms();
if(S5==0)
keyval=5;
if(S6==0)
keyval=6;
if(S7==0)
keyval=7;
if(S8==0)
keyval=8;
}
}
void Alter(void)
{
P3=0xaa;
led_delay();
P3=0x55;
led_delay();
}
void forward(void)
{
P3=0x7e;
led_delay();
P3=0xbd;
led_delay();
P3=0xdb;
led_delay();
P3=0xe7;
led_delay();
P3=0xe7;
led_delay();
P3=0xdb;
led_delay();
P3=0xbd;
led_delay();
P3=0x7e;
led_delay();
}
void blink(void)
{
P3=0xff;
led_delay();
P3=0x00;
led_delay();
}
void backward(void)
{
P3=0x7f;
led_delay();
P3=0xbf;
led_delay();
P3=0xdf;
led_delay();
P3=0xef;
led_delay();
P3=0xf7;
led_delay();
P3=0xfb;
led_delay();
P3=0xfd;
led_delay();
P3=0xfe;
led_delay();
}
void led_delay(void)
{
unsigned char i,j;
for(i=0;i<220;i++)
for(j=0;j<220;j++);
}
void delay10ms(void)
{
unsigned char i,j;
for(i=0;i<100;i++)
for(j=0;j<100;j++);
}
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』