CC2530应用——按键控制灯光状态变化

独立新建工程并编写、编译代码,实现按键控制灯光闪烁状态的变换,实现以下任务要求:
【1】程序开始运行:D4灯闪烁,D3、D5、D6灯熄灭。
【2】按下模块上的SW1按键松开后,实现D5、D6灯轮流闪烁。
【3】再次按下SW1按键,D5、D6灯灭。
【4】重复上述两个步骤。
此题需要定义一个灯光状态的标志位。通过按键的标志位有三个状态。
状态1:D4灯闪烁,D3、D5、D6灯熄灭。
状态2:D5、D6灯轮流闪烁。
状态3:D5、D6灯灭。

 1 #include "ioCC2530.h"
 2 
 3 #define D3  P1_0
 4 #define D4  P1_1
 5 #define D5  P1_3
 6 #define D6  P1_4
 7 #define SW1 P1_2
 8 
 9 unsigned char stat = 0;   //灯光状态标志
10 
11 /*=======================简单的延时函数========================*/
12 void Delay(unsigned int t)
13 {
14   while(t--);
15 }
16 /*=======================端口初始化函数========================*/
17 void Init_Port()
18 {
19   P1SEL &= ~0x1b;       //将P1_0,P1_1,P1_3,P1_4设置为通用I/O端口  
20   P1DIR |= 0x1b;        //将P1_0,P1_1,P1_3,P1_4设置为输出方向
21   P1 &= ~0x1b;          //关闭4个LED灯
22   
23   P1SEL &= ~0x04;       //将P1_2设置为通用I/O端口
24   P1DIR &= ~0x04;       //将P1_2设置为输入方向
25   P1INP &= ~0x04;       //将P1_2设置为上拉/下拉
26   P2INP &= ~0x40;       //将P1_2设置为上拉
27 }
28 
29 /*=====================D4灯闪烁函数======================*/
30 void D4_Flicker()
31 {
32   D4 = 1;
33   Delay(60000);
34   D4 = 0;
35   Delay(60000);
36 }
37 
38 /*=====================D5D6灯闪烁函数======================*/
39 void D5D6_Flicker()
40 {
41   D3 = 0;
42   D4 = 0;
43   D5 = 1;
44   Delay(60000);
45   D5 = 0;
46   Delay(60000);
47   D6 = 1;
48   Delay(60000);
49   D6 = 0;
50   Delay(60000);
51 }
52 
53 /*=======================按键扫描函数=========================*/
54 void Scan_Keys()
55 {
56   if(SW1 == 0)            //发现有SW1按键信号
57   {
58     Delay(100);           //延时片刻,去抖动处理
59     if(SW1 == 0)          //确认为SW1按键信号
60     {
61       if(stat == 0)
62       {
63         stat = 1;
64       }
65        else if(stat == 1)  //重复
66       {
67         stat = 2;
68       }
69       else if(stat == 2)
70       {
71         stat = 1;
72       }
73       
74     }
75   }       
76 }
77 
78 /*==========================主函数============================*/
79 void main()
80 {
81   Init_Port();            //端口初始化
82   while(1)
83   {
84     Scan_Keys();          //按键扫描
85     switch(stat)
86     {
87       case 0:             //上电状态,D4闪烁
88         D4_Flicker();     
89       break;
90       case 1:             //运行状态1:D5和D6闪烁
91         D5D6_Flicker();
92       break;
93       case 2:             //运行状态2:D5和D6熄灭
94         D5 = 0;
95         D6 = 0;
96       break;
97     }
98   }
99 }
View Code
原文地址:https://www.cnblogs.com/yuling520/p/12691326.html