用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;
}
}
此帖出自
小平头技术问答
一周热门 更多>