矩阵按键的MSP430驱动函数
一、电路图
二、工作原理
如图所示,对列4567扫描,检测行0123。逐一让列值为0,检测行值,该列有键按下,则相应行值为低,其余行值为高。通过列值行值的低电平可判断按键。
三、代码
ArrayKeyDriver.c
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /**************引用头文件***************/
2 #include <msp430F149.h>
3 #include "ArrayKeyDriver.h"
4 /*******************************************
5 函数名称:ArrayKeyInit
6 功 能:初始化连接矩阵按键的IO口
7 参 数:无
8 返回值 :无
9 ********************************************/
10 void ArrayKeyInit(void)
11 {
12 P5SEL = 0;
13 //行检测位,设为输入
14 P5DIR &=~(BIT0+BIT1+BIT2+BIT3) ;
15 //列扫描位,设为输出
16 P5DIR |= BIT4+BIT5+BIT6+BIT7;
17 //扫描位先输出低电平
18 P5OUT&=~(BIT4+BIT5+BIT6+BIT7);
19 }
20 /*******************************************
21 函数名称:KeyScan
22 功 能:等待按键按下,并调用KeyProcess得到按键值nRes
23 参 数:无
24 返回值 :uint nRes。返回按键值
25 ********************************************/
26 uint KeyScan(void)
27 {
28 uint nP50,nP51,nP52,nP53,nRes=0;
29 for(;;)
30 {
31 //读取各个管脚的状态
32 nP50 = P5IN & BIT0;
33 nP51 = (P5IN & BIT1) >> 1;
34 nP52 = (P5IN & BIT2) >> 2;
35 nP53 = (P5IN & BIT3) >> 3;
36 //是否有键被按下
37 if(nP50 == 0 || nP51 == 0 || nP52 == 0 || nP53 == 0)
38 {
39 //等待按键按下
40 break;
41 }
42
43 }
44 DelayDebounce(); //延时一点时间,消除抖动
45
46 nP50 = P5IN & BIT0; //读取各个管脚的状态
47 nP51 = (P5IN & BIT1) >> 1;
48 nP52 = (P5IN & BIT2) >> 2;
49 nP53 = (P5IN & BIT3) >> 3;
50
51 //是否有键被按下
52 if(nP50 == 0 || nP51 == 0 || nP52 == 0 || nP53 == 0)
53 {
54 //有键被按下,进行键盘输入分析
55 nRes=KeyProcess();
56 }
57
58 return nRes;
59 }
60 /*******************************************
61 函数名称:DelayDebounce
62 功 能:按键去抖动延时
63 参 数:无
64 返回值 :无
65 ********************************************/
66 void DelayDebounce(void)
67 {
68 int i;
69 for(i = 100;i>0;i --) ;//延时一点时间
70 }
71 /*******************************************
72 函数名称:KeyProcess
73 功 能:通过扫描获取按键值送到nRes
74 参 数:无
75 返回值 :uint nRes。返回按键值
76 ********************************************/
77 uint KeyProcess( void )
78 {
79 uint nP50,nP51,nP52,nP53,nRes=0;
80 //P5.4输出低电平
81 P5OUT =~(BIT4);
82 nP50 = P5IN & BIT0;//读取各个管脚的状态
83 nP51 = (P5IN & BIT1) >> 1;
84 nP52 = (P5IN & BIT2) >> 2;
85 nP53 = (P5IN & BIT3) >> 3;
86 if (nP50 == 0) nRes = 11;//第1行第1列,下同
87 else if (nP51 == 0) nRes = 12;
88 else if (nP52 == 0) nRes = 13;
89 else if (nP53 == 0) nRes = 14;
90 //P5.5输出低电平
91 P5OUT =~(BIT5);
92 nP50 = P5IN & BIT0;//读取各个管脚的状态
93 nP51 = (P5IN & BIT1) >> 1;
94 nP52 = (P5IN & BIT2) >> 2;
95 nP53 = (P5IN & BIT3) >> 3;
96 if (nP50 == 0) nRes = 21;//第2行第1列,下同
97 else if (nP51 == 0) nRes = 22;
98 else if (nP52 == 0) nRes = 23;
99 else if (nP53 == 0) nRes = 24;
100 //P5.6输出低电平
101 P5OUT =~(BIT6);
102 nP50 = P5IN & BIT0;//读取各个管脚的状态
103 nP51 = (P5IN & BIT1) >> 1;
104 nP52 = (P5IN & BIT2) >> 2;
105 nP53 = (P5IN & BIT3) >> 3;
106 if (nP50 == 0) nRes = 31;//第3行第1列,下同
107 else if (nP51 == 0) nRes = 32;
108 else if (nP52 == 0) nRes = 33;
109 else if (nP53 == 0) nRes = 34;
110 //P5.7输出低电平
111 P5OUT =~(BIT7);
112 nP50 = P5IN & BIT0;//读取各个管脚的状态
113 nP51 = (P5IN & BIT1) >> 1;
114 nP52 = (P5IN & BIT2) >> 2;
115 nP53 = (P5IN & BIT3) >> 3;
116 if (nP50 == 0) nRes = 41;//第4行第1列,下同
117 else if (nP51 == 0) nRes = 42;
118 else if (nP52 == 0) nRes = 43;
119 else if (nP53 == 0) nRes = 44;
120
121 P5OUT = 0x00;//恢复以前值。
122 //等待松开按键
123 for(;;)
124 {
125
126 nP50 = P5IN & BIT0;
127 nP51 = (P5IN & BIT1) >> 1;
128 nP52 = (P5IN & BIT2) >> 2;
129 nP53 = (P5IN & BIT3) >> 3;
130 if(nP50 == 1 && nP51 == 1 && nP52 == 1 && nP53 == 1)
131 break;
132 }
133
134 return nRes;
135 }
ArrayKeyDriver.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <msp430F149.h>
2
3 #define uchar unsigned char
4 #define uint unsigned int
5
6 extern void ArrayKeyInit(void);
7 extern uint KeyScan(void);
8 extern void DelayDebounce(void);
9 extern uint KeyProcess( void );
/***万好好-科研论文培训,微信号:wanhaohao258。如转载本博客内容请注明,如果存在知识产权问题请主动联系。***/