独立按键的MSP430驱动

IndepKey_Driver.c
View Code
1 //*******************************************************************************
2  //说明:4个独立按键接P1.0-P1.3
3 // 采用P1口中断方式检测独立按键模块
4 //*******************************************************************************
5 /**************引用头文件***************/
6 #include <msp430F149.h> //视具体单片机型号引用不同头文件
7 #include "IndepKey_Driver.h"
8 #define uchar unsigned char
9 #define uint unsigned int
10
11 /**************接口定义***************/
12 #define IndepKeyDir P1DIR
13 #define IndepKeyIES P1IES
14 #define IndepKeyIE P1IE
15 /*******************************************
16 函数名称:void IndepKeyInit(void)
17 功 能:初始化单片机对独立按键模块的IO口,
18 设置为下降沿中断触发,开中断使能
19 参 数:无
20 返回值 :无
21 ********************************************/
22 void IndepKeyInit(void)
23 {
24 IndepKeyDir&=~(BIT0+BIT1+BIT2+BIT3);
25 IndepKeyIES|=BIT0+BIT1+BIT2+BIT3;
26 IndepKeyIE|=BIT0+BIT1+BIT2+BIT3;
27 }
IndepKey_Driver.h
View Code
1 #define uchar unsigned char
2 #define uint unsigned int
3
4 extern void IndepKeyInit(void);

中断服务函数

View Code
1 /*******************************************
2 函数名称:__interrupt void IndepKey_ISR(void)
3 功 能:按键后实时扫描独立按键值,根据相应值
4 实现相应功能
5 参 数:无
6 返回值 :无
7 ********************************************/
8 #pragma vector=PORT1_VECTOR
9 __interrupt void IndepKey_ISR(void)
10 {
11 uint i;
12 uchar PushKey;
13 PushKey=P1IFG&(BIT0+BIT1+BIT2+BIT3);
14 for(i=0;i<1000;i++){};
15 if((P1IN&PushKey)==PushKey)//如果按键变高,则判为毛刺
16 {
17 P1IFG=0;return;
18 }
19 if(PushKey&BIT0){P2OUT=~BIT0;};
20 if(PushKey&BIT1){P2OUT=~BIT1;};
21 if(PushKey&BIT2){P2OUT=~BIT2;};
22 if(PushKey&BIT3){P2OUT=~BIT3;};
23 P1IFG=0;
24 return;
25 }
原文地址:https://www.cnblogs.com/yuesheng/p/2101803.html