此代码可实现的功能:
按下k1 依次点亮一个LED,按下k2 依次熄灭一个LED;
按下k3 LED全灭,按下k4 LED全亮。
(可以接在51单片机上,也可以进行仿真)
(单片机小白自己写的代码,有什么建议或者需要改进的地方还请指出,十分感谢!)
#include"reg52.h"
#define led P0
#define smg P1typedef unsigned char u8;
typedef unsigned int u16;sbit k1=P2^0;
sbit k2=P2^1;
sbit k3=P2^2;
sbit k4=P2^3;u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};int a=0;void delay(u16 i)
{while(i--);
}void keypros()
{if(k1==0){delay(20);if(k1==0){a++;led=(0xff<<a);}while(!k1);} if(a>8){a=0;led=0xff;} if(k2==0){delay(20);if(k2==0){led=(~P0>>1);led=~P0;a=a-1;}while(!k2);}if(a<0){led=0x00;a=8;}if(k3==0){delay(20);if(k3==0){led=0xff;a=0;}while(!k3);}if(k4==0){delay(20);if(k4==0){led=0x00;a=8;}while(!k4);}
}void main()
{led=0xff;while(1){ smg=~smgduan[a];keypros();}
}
附上仿真图