#include "ioCC2530.h" #define D3 P1_0#define D4 P1_1#define D5 P1_3#define D6 P1_4#define SW1 P1_2 unsigned char count_t = 0; //長定時累計變數unsigned char K ...
#include "ioCC2530.h" #define D3 P1_0
#define D4 P1_1
#define D5 P1_3
#define D6 P1_4
#define SW1 P1_2 unsigned char count_t = 0; //長定時累計變數
unsigned char K_Press = 0; //按鍵按下標誌
/*=======================簡單的延時函數========================*/
void Delay(unsigned int t)
{
while(t--);
}
/*======================埠初始化函數========================*/
void Init_Port()
{
//初始化LED燈的I/O埠
P1SEL &= ~0x1b; //P1_0、P1_1、P1_3和P1_4作為通用I/O埠
P1DIR |= 0x1b; //P1_0、P1_1、P1_3和P1_4埠輸出
//關閉所有的LED燈
P1 &= ~0x1b;
//初始化按鍵
P1SEL &= ~0x04; //P1_2作為通用I/O埠
P1DIR &= ~0x04; //P1_2埠輸入
P1INP &= ~0x04; //P1_2設置為上拉/下拉模式
P2INP &= ~0x40; //P1_2設置為上拉
} /*=====================定時器1初始化函數======================*/
void Init_Timer1()
{
T1CC0L = 0xd4; //16MHz時鐘128分頻定時100ms
T1CC0H = 0x30; //設先填低8位,再填高8位
T1CCTL0 |= 0x04; //開啟通道0的輸出比較模式
T1IE = 1; //使能定時器1中斷
T1OVFIM = 1; //使能定時器1溢出中斷
EA = 1; //使能總中斷
T1CTL = 0x0e; //分頻繫數是128,模模式
} /*====================定時器1中斷服務函數=====================*/
#pragma vector = T1_VECTOR
__interrupt void Timer1_Sevice()
{
T1STAT &= ~0x01; //清除定時器1通道0中斷標誌
if(K_Press != 0) //按鍵按下
{
count_t++; //計算按下按下的時間值
}
} /*====================按鍵掃描處理函數========================*/ void Scan_Keys() {
if(SW1 == 0)
{
Delay(100); //去抖動處理
if(SW1 == 0)
{
K_Press = 1; //標誌按鍵正在按下
while(SW1 == 0); //等待按鍵鬆開
K_Press = 0; //標誌按鍵已經鬆開
if(count_t > 10) //按鍵長按
{
D6 = ~D6;
}
else //按鍵短按
{
D4 = ~D4;
}
count_t = 0; //按鍵計數值清零
}
}
} /*==========================主函數============================*/
void main()
{
Init_Port(); //埠初始化
Init_Timer1(); //初始化定時器1
while(1)
{
Scan_Keys(); //掃描按鍵
}
}