用两个按键控制单片机流水灯的暂停和启动
- #include "reg51.h" /* 此文件中定义了单片机的一些特殊功能寄存器 */
- #include <intrins.h>
- typedef unsigned int int16_t; /* 对数据类型进行声明定义 */
- typedef unsigned char int8_t;
- #define LED P2
- /**************端口定义************************************************/
- sbit Key_A=P3^1; //开始
- sbit Key_B=P3^2; //停止
- /*********变量定义******************************************/
- bit Start=0;
- bit sign=0;
- int16_t count,count1;
- void Key_Scan()
- {
- if(Key_A==0||Key_B==0)//检测按键
- {
- if(++count1>=250 && sign==0)//检测开始按键
- {
- sign=1;
- if(Key_A==0)Start=1; //开始
- if(Key_B==0)Start=0; //停止
- }
- }
- else
- {
- sign=0;
- count1=0;
- }
- }
- void main()
- {
- LED=0xfe;
- while(1)
- {
- Key_Scan();
- if(Start)
- {
- count++;
- if(count>=10000)
- {
- count=0;
- LED=LED<<1|0x01;
- if(LED==0xff)
- LED=0xfe;
- }
- }
- }
- }
复制代码 |