#include<msp430g2553.h>
#include "in430.h"
void init_plint();
unsigned char keybuf;
void delay()
{
unsigned int i;
for(i=0;i<500;i++);
}
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P1SEL=0;
P3SEL=0;
P3DIR|=0xff;//设置P3为输出方向,接LED灯
P3OUT|=0xff;//输出高电平
P1DIR=0x0f;//设置P1.0-P1.3为输出方向,矩阵键盘行线;P1.4-P1.7为输入方向,矩阵键盘列线
P1REN=0xf0;
P1OUT=0xf0;
init_plint();
_EINT();
}
void init_plint()
{
P1IE=0Xf0;//设置P1.4-P1.7可以中断
P1IES=0xf0;//设置P1.4-P1.7位下降沿中断
P1IFG =0; // P1中断标志位清零
}
unsigned char p1keyj()
{
unsigned char i;
i=P1IN^0xf0;//P1.4-P1.7接有按键
return i;
}
unsigned char keycode()
{
unsigned char k;
if(p1keyj()==0x10)//是否第一个按键
k=1;
else if((p1keyj())==0x20)//是否第二个按键
k=2;
else if((p1keyj()<<2)==0x40)//是否第三个按键
k=3;
else if((p1keyj())==0x80)//是否第四个按键
k=4;
else k=0;
return k;
}
#pragma vector=PORT1_VECTOR
__interrupt void p1int()
{
_DINT(); //关中断
P1IFG &= ~0xFF;
delay();
while(p1keyj()!=0x00)//没有键按下,返回1--0x00
{
delay();//延时消抖
while(p1keyj()!=0x00)
{
keybuf=keycode();//确信有按键按下,找按键的键值,送到全局变量keybuf
while(p1keyj()==0x00)//等待按键松开
{
switch(keybuf)
{
case 1:P3OUT&=~BIT0;//点亮LED0
case 2:P3OUT&=~BIT1;//点亮LED1
case 3:P3OUT&=~BIT2;//点亮LED2
case 4:P3OUT&=~BIT3;//点亮LED3
}
}
}
}
}
|