把各种花样流水灯用数组排列好,按下按键立即切换。
- #include <reg51.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit key = P3^2;
- unsigned char code table1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
- unsigned char code table2[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
- unsigned char code table3[]={0x7e,0xbd,0xdb,0xe7,0x7e,0xbd,0xdb,0xe7};
- unsigned char code table4[]={0x55,0xaa,0x55,0xaa,0x55,0xaa,0x55,0xaa};
- uchar count = 0;
- uchar LED = 0xff;
- uchar i;
- void delay10ms(uint n)
- {
- uchar i,j;
- for(;n>0;n--)
- {
- for(i=0;i<18;i++)
- {
- for(j=0;j<184;j++){;}
- }
- }
- }
- void main(void)
- {
- IT0 = 1;
- EX0 = 1;
- EA = 1;
- while(1)
- {
- switch(count)
- {
- case 1: P1=table1[i]; break;
- case 2: P1=table2[i]; break;
- case 3: P1=table3[i]; break;
- case 4: P1=table4[i]; break;
- default:P1=0xff; break;
- }
- i=++i%8;
- delay10ms(50);
- }
- }
- void key_check(void) interrupt 0
- {
- if(key == 0)
- {
- delay10ms(1);
- if(key == 0)
- {
- ++count;
- if(count == 5)
- count = 1;
- }
- }
- }
复制代码 |