用兩個按鍵控制單片機流水燈的暫停和啟動
- #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;
- }
- }
- }
- }
復制代碼 |