使用环境:
C语言,IAR编译器,cc2530开发板,zigbee仿真器
代码:
#include <ioCC2530.h>
#define uint unsigned int
#define uchar unsigned char
#define LED1 P1_0 //定义P1_0为LED1
#define LED2 P1_1 //定义P1_1为LED2
#define LED3 P0_4 //定义P0_4为LED3
#define KEY1 P0_1 //定义P0_1为KEY1
void Delay(uint);
void InitIO(void);
void InitKey(void);
uchar KeyScan(void); //函数声明
uchar Keyvalue = 0;
uint KeyTouchtimes = 0;
/****************************
//延时
*****************************/
void Delay(uint n) //延时函数
{
uint i;
for (i = 0; i < n; i++)
;
for (i = 0; i < n; i++)
;
for (i = 0; i < n; i++)
;
for (i = 0; i < n; i++)
;
for (i = 0; i < n; i++)
;
}
/****************************
//按键初始化函数
*****************************/
void InitKey(void)
{
P0SEL &= ~0X02; //定义端口p0_1为通用IO
P0DIR &= ~0X02; //定义端口p0_1为输入方向
P0INP |= 0x02; //定义端口p0_1的输入模式为三态
}
/**************************
//LED初始化
*****************************/
void InitIO(void)
{
P1DIR |= 0x13; //定义端口p1_0,p1_1,p1_4 为输出
P0DIR |= 0x10; //定义端口p0_4为输出
LED1 = 1; //初始化LED1,LED2,LED3为灭
LED2 = 1;
LED3 = 1;
}
/*****************************************
//按键动作记录函数
*****************************************/
uchar KeyScan(void)
{
if (KEY1 == 0) //按键按下,输入0
{
Delay(100); //消抖
if (KEY1 == 0)
{
while (KEY1 == 0)
;
return (1);
}
}
return (0);
}
/***************************
//主函数
***************************/
void main(void)
{
InitIO(); //初始化LED
InitKey(); //初始化按键
while (1) //死循环
{
Keyvalue = KeyScan(); //给keyvalue赋值,值为按键动作记录的值(捕获按键动作)
if (Keyvalue == 1) //捕获到按键动作
{
LED3 = !LED3;
Delay(50000);
LED2 = !LED2;
Delay(50000);
LED1 = !LED1;
Delay(50000);
}
}
}