中断扫描键盘程序

2019-03-24 15:50发布

用IO口做3x3中断扫描按键的程序,P1.0~P1.2为输出设置为低电平,P1.3~P1.5做输入且接一上拉电阻,利用下降沿中断,检测到按键后点亮相应的LED灯(通过P2连接),调试的时候按按键似乎没反应无法进入到中断服务函数。
求指点


/***********4 IO port implement 3*3 key************
function:use IO port P1.4~P1.7 to impelment 3*3 key
---------------------------------------------------
test:press a key then watch the relevant LED light
**************************************************/
#include <msp430x14x.h>
#include "BoardConfig.h"

#define keyin (P1IN & 0x38)
#define keyint (P1IFG & 0x38)

void main()
{
          WDTCTL = WDTPW + WDTHOLD;   //turn off the watch dog
            BoardConfig(0xb0);          //turn on the LED light,turn off nixie tube and level translator
       
        P1SEL=0x00;                 //select P1 port as ordinary function
        P1OUT=0x00;                 //set P1.0~P1.2 to low level
        P1DIR=0x07;                    //set P1.3~P1.5 to input state and P1.0 to P1.2 to output state
        P1IES=0x38;                 //set P1.3~P1.5 to falling edge interrupt
        P1IE=0x38;                  //port1 interrupt enable
        P1IFG=0x00;
       
        P2SEL=0x00;                 //select P2 port as ordinary function
        P2OUT=0xff;                 //set P2.0~P2.7 to high level
        P2DIR=0xff;                 //set P2.0~P2.7 to output state
        P1IFG=0x00;
        _EINT();                    //turn on global interrupt
       
        while(1)
        {
                  LPM1;               //entry low power mode1;
        }
}

/***************delay***************
function:wait for jitter clear up
parameter:no
return value:no
***********************************/
void delay()
{
          uint i;
       
        for(i=0; i<12000; i++);
}

/*************KeyPort1_int*************
function:P1 port interrupt
parameter:no
return value:no
**************************************/
#pragma vector=PORT1_VECTOR
__interrupt void PORT1_ISR(void)
{
                  delay();
               
                switch( P1IFG & 0x38 )                                 //judge which key was pressed  
                {
                case 0x08:
                          P1OUT&=~BIT0;
                        if( keyin==0x30 )  //the first key was pressed
                        {
                                  LPM1_EXIT;
                                  P2OUT=~BIT0;  //turn on the first LED light
                                P1IFG=0;
                                return;
                        }
                       
                        P1OUT&=~BIT1;
                        if( keyin==0x30 )  //the second key was pressed
                        {
                                  LPM1_EXIT;
                                  P2OUT=~BIT1;  //turn on the second LED light
                                P1IFG=0;
                                return;
                        }
                       
                        P1OUT&=~BIT2;
                        if( keyin==0x30 )  //the third key was pressed
                        {
                                  LPM1_EXIT;
                                  P2OUT=~BIT2;  //turn on the third LED light
                                P1IFG=0;
                                return;
                        }
                case 0x10:
                          P1OUT&=~BIT0;
                        if( keyin==0x28 )  //the forth key was pressed
                        {
                                  LPM1_EXIT;
                                  P2OUT=~BIT3;  //turn on the fortht LED light
                                P1IFG=0;
                                return;
                        }
                       
                        P1OUT&=~BIT1;
                        if( keyin==0x28 )  //the sfifth key was pressed
                        {
                                  LPM1_EXIT;
                                  P2OUT=~BIT4;  //turn on the fifth LED light
                                P1IFG=0;
                                return;
                        }
                       
                        P1OUT&=~BIT2;
                        if( keyin==0x28 )  //the sixth key was pressed
                        {
                                  LPM1_EXIT;
                                  P2OUT=~BIT5;  //turn on the sixth LED light
                                P1IFG=0;
                                return;
                        }
                       
                case 0x20:
                          P1OUT&=~BIT0;
                        if( keyin==0x18 )  //the seventh key was pressed
                        {
                                  LPM1_EXIT;
                                  P2OUT=~BIT6;  //turn on the seventh LED light
                                P1IFG=0;
                                return;
                        }
                       
                        P1OUT&=~BIT1;
                        if( keyin==0x18 )  //the eighth key was pressed
                        {
                                  LPM1_EXIT;
                                  P2OUT=~BIT7;  //turn on the  eighth LED light
                                P1IFG=0;
                                return;
                        }
                       
                        P1OUT&=~BIT2;
                        if( keyin==0x18 )  //the ninth key was pressed
                        {
                                  LPM1_EXIT;
                                  P2OUT=0;  //turn on the ninth LED light
                                P1IFG=0;
                                return;
                        }
                       
                default:
                            while(keyin != 0x38 );       //wait for key to be free
                            P1IFG=0;
                            break;
                }
} 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
6条回答
chen502
2019-03-24 23:46
< :TI_MSP430_内容页_SA7 --> 谢谢,确实是无法进入中断服务函数,不知道有哪些原因会导致无法进入中断服务函数?而且P1.7这个端口我没有使用也没有使能它的中断,不知道为什么中断标志寄存器总P1IFG的这一位总是为1标志有中断产生,即使用软件清零也没用

一周热门 更多>

相关问题

    相关文章