#include<reg52.h> sbit led=P2^0; sbit key=P3^1; unsigned int t=1,q=0; void delay(unsigned int c) { while(--c); } void delay2(unsigned int c1) { for(;c1>0;c1--) { led=0; delay(t); led=1; delay(500-t); } } void main() { unsigned int dir=0; while(1) { delay2(1); if(key==0&&q==0) { q=1; if(dir==0) t+=100; else t-=100; if(t>=400) dir=1; if(t<=100) dir=0; delay2(10); } if(key==1&&q==1) { q=0; delay2(10); } } }
- 接线:
- LED灯接P2^0
- 独立按键接P3^1