基于io中断实现的矩阵键盘程序
#include <MSP430X44X.H>
unsigned char Key_Num=0 ;
void time_delay(void) ;
void Display_Num(void) ;
void Key_Process(void) ;
void main()
{
WDTCTL = WDTPW + WDTHOLD ; //停止看门狗
P4OUT = 0X00 ;
P4DIR |= 0X08 ;
P1DIR = 0X00 ; //将P1定义为输入
P1DIR |= 0X0E ; //将P1.1~P1.3定义为输出
P1OUT = 0X00 ; //P1.1~P1.3输出为0
P1IE = 0X00 ;
P1IES = 0X00 ;
P1IFG = 0X00 ;
P1IE |= 0X0E ; //P1允许中断
P1IES |= 0X0E ;
_EINT() ; //系统中断允许
while(1) ;
}
#pragma vector = PORT1_VECTOR
__interrupt void port1(void)
{
time_delay () ; //软件消抖
if((P1IN&0XF0) != 0XF0 ) //有键按下
{
Key_Process () ;
P1IFG = 0X00 ;
P1OUT = 0X0E ;
Display_Num () ;
}
}
void time_delay (void)
{
unsigned int i =1 ;
while (i--) ;
}
void Display_Num(void)
{
P4OUT = 0X08 ;
}
void Key_Process(void)
{
P1OUT=0XEF; //P1.4 OUT 0
if((P1IN & 0X02) == 0X00) Key_Num = 1 ;
if((P1IN & 0X04) == 0X00) Key_Num = 2 ;
if((P1IN & 0X08) == 0X00) Key_Num = 3 ;
P1OUT=0XDF; //P1.5 OUT 0
if((P1IN & 0X02) == 0X00) Key_Num = 4 ;
if((P1IN & 0X04) == 0X00) Key_Num = 5 ;
if((P1IN & 0X08) == 0X00) Key_Num = 6 ;
P1OUT=0XBF; //P1.6 OUT 0
if((P1IN & 0X02) == 0X00) Key_Num = 7 ;
if((P1IN & 0X04) == 0X00) Key_Num = 8 ;
if((P1IN & 0X08) == 0X00) Key_Num = 9 ;
P1OUT=0X7F; //P1.7 OUT 0
if((P1IN & 0X02) == 0X00) Key_Num = 10 ;
if((P1IN & 0X04) == 0X00) Key_Num = 11 ;
if((P1IN & 0X08) == 0X00) Key_Num = 12 ;
}
|