cc2530-projects/task008/source/code.c
2021-09-24 23:06:46 +08:00

136 lines
2.8 KiB
C

#include <ioCC2530.h>
#define LED1 P1_0
#define LED2 P1_1
#define SW1 P1_2
//按钮锁定
unsigned char Button_lock;
unsigned char Long_click_relase = 0;
//双击间隔时间
unsigned int time = 0;
//延时
void delay(unsigned int time){
unsigned int tt,i = 0;
for(i = 0; i < time; i ++){
for(tt = 0; tt<1040; tt++);
}
}
//初始化端口
void initial_gpio(void){
P1SEL &=~0X07;
P1DIR |=0X03;
P1DIR &=~0X04;
P1INP &=~0X04;
P2INP &=~0X40;
P1 &=~0X03;
}
/**
* 按钮上锁
*/
void LED_flicker(void){
unsigned char i,j;
for(i=0;i<2;i++){
LED1 = 1;
delay(100);
LED1 = 0;
delay(100);
}
for(j=0;j<3;j++){
LED2 = 1;
delay(100);
LED2 = 0;
delay(100);
}
Button_lock = 1;
}
/**
* 检测按钮状态
* 长按返回 111
* 双击返回 222
* 单击返回 333
*/
unsigned Scan_keydown(){
if(Long_click_relase){
if(SW1 == 1){
Long_click_relase = 0;
return (444);
}
}
if(SW1 == 0){
delay(10);
if(SW1 == 0){
time = 0;
while(!SW1){
delay(1);
time++;
if(time>1200){
Long_click_relase = 1;
return (111);
}
}
delay(50);
time = 10;
while(time--){
if(SW1 == 0){
while(!SW1);
return (222);
}
delay(10);
}
return (333);
}
}
return (0);
}
/*
* 控制LED
* 双击点亮LED2
* 单击点亮LED1
*/
void control_LED(){
int key_status = Scan_keydown();
//判断是否长按
if(key_status == 444){
//检测上锁状态
if(Button_lock){ //如果上锁则解锁
LED1 = 1;
delay(350);
LED1 = 0;
Button_lock = 0;
}else if(!Button_lock){ //如果解锁则上锁
LED1 = 1;
LED2 = 1;
delay(350);
LED1 = 0;
LED2 = 0;
Button_lock = 1;
}
}
if(!Button_lock){ //解锁状态下判断按键状态
//如果双击
if(key_status == 222){
LED2 = ~LED2;
}
//如果单击
if(key_status == 333){
LED1 = ~LED1;
}
key_status = 0;
}
}
void main(void){
initial_gpio();
LED_flicker();
for(;;){
control_LED();
}
}