#include <msp430g2553.h>
//端口初始化程序
void Init_Port(void)
{
//将P1口所有的管脚在初始化的时候设置为输入方式
P1DIR = 0;
//将P1所有的管脚设置为一般
P1SEL = 0;
//将P1.4 5 6 7 设置为输出方式
P1DIR |= BIT4;
P1DIR |= BIT5;
P1DIR |= BIT6;
P1DIR |= BIT7;
//先输出低电平
P1OUT =0x00;
return;
}
void Delay(void)
{
int i;
for (i=100;i>0;i--);
}
//按键分析程序
int KeyProcess(void)
{
int nP10=0,nP11=0,nP12=0,nP13=0;
int nRes=0;
//P1.4输出低电平
P1OUT&=~(BIT4);
nP10=P1IN&BIT0;
if (nP10==0) nRes=13;
nP11=(P1IN&BIT1)>>1;
if (nP11==0) nRes=14;
nP12=(P1IN&BIT2)>>2;
if (nP12==0) nRes=15;
nP13=(P1IN&BIT3)>>3;
if (nP13==0) nRes=16;
//P1.5输出低电平
P1OUT&=~(BIT5);
nP10=P1IN&BIT0;
if (nP10==0) nRes=9;
nP11=(P1IN&BIT1)>>1;
if (nP11==0) nRes=10;
nP12=(P1IN&BIT2)>>2;
if (nP12==0) nRes=11;
nP13=(P1IN&BIT3)>>3;
if (nP13==0) nRes=12;
//P1.6输出低电平
P1OUT&=~(BIT6);
nP10=P1IN&BIT0;
if (nP10==0) nRes=5;
nP11=(P1IN&BIT1)>>6;
if (nP11==0) nRes=6;
nP12=(P1IN&BIT2)>>2;
if (nP12==0) nRes=7;
nP13=(P1IN&BIT3)>>3;
if (nP13==0) nRes=8;
//P1.7输出低电平
P1OUT&=~(BIT7);
nP10=P1IN&BIT0;
if (nP10==0) nRes=1;
nP11=(P1IN&BIT1)>>1;
if (nP11==0) nRes=2;
nP12=(P1IN&BIT2)>>2;
if (nP12==0) nRes=3;
nP13=(P1IN&BIT3)>>3;
if (nP13==0) nRes=4;
P1OUT = 0x00; //恢复以前值
//读取各个管脚的状态
nP10=P1IN&BIT0;
nP11=(P1IN&BIT1)>>1;
nP12=(P1IN&BIT2)>>2;
nP13=(P1IN&BIT3)>>3;
for(;;)
{
if(nP10==1&&nP11==1&&nP12==1&&nP13==1)
{
//有按键被按下,跳出,执行下边程序
break;
}
}
return nRes;
}
//键盘扫描程序
void KeyScan (void)
{
int nP10=0,nP11=0,nP12=0,nP13=0;
int nRes=0;
//是否有按键被按下
while (nP10==0||nP11==0||nP12==0||nP13==0)
{
//有按键被按下,跳出,执行下边程序
break;
}
Delay();
//读取各个管脚的状态
nP10=P1IN&BIT0;
nP11=(P1IN&BIT1)>>1;
nP12=(P1IN&BIT2)>>2;
nP13=(P1IN&BIT3)>>3;
}
//是否有按键被按下
if (nP10==0||nP11==0||nP12==0||nP13==0)
{
//如果没有
nRes = KeyProcess();
}
else return -1;//没有输入,为干扰
return nRes;
}
//请帮忙看看问题都出在哪里
//端口初始化程序
void Init_Port(void)
{
//将P1口所有的管脚在初始化的时候设置为输入方式
P1DIR = 0;
//将P1所有的管脚设置为一般
P1SEL = 0;
//将P1.4 5 6 7 设置为输出方式
P1DIR |= BIT4;
P1DIR |= BIT5;
P1DIR |= BIT6;
P1DIR |= BIT7;
//先输出低电平
P1OUT =0x00;
return;
}
void Delay(void)
{
int i;
for (i=100;i>0;i--);
}
//按键分析程序
int KeyProcess(void)
{
int nP10=0,nP11=0,nP12=0,nP13=0;
int nRes=0;
//P1.4输出低电平
P1OUT&=~(BIT4);
nP10=P1IN&BIT0;
if (nP10==0) nRes=13;
nP11=(P1IN&BIT1)>>1;
if (nP11==0) nRes=14;
nP12=(P1IN&BIT2)>>2;
if (nP12==0) nRes=15;
nP13=(P1IN&BIT3)>>3;
if (nP13==0) nRes=16;
//P1.5输出低电平
P1OUT&=~(BIT5);
nP10=P1IN&BIT0;
if (nP10==0) nRes=9;
nP11=(P1IN&BIT1)>>1;
if (nP11==0) nRes=10;
nP12=(P1IN&BIT2)>>2;
if (nP12==0) nRes=11;
nP13=(P1IN&BIT3)>>3;
if (nP13==0) nRes=12;
//P1.6输出低电平
P1OUT&=~(BIT6);
nP10=P1IN&BIT0;
if (nP10==0) nRes=5;
nP11=(P1IN&BIT1)>>6;
if (nP11==0) nRes=6;
nP12=(P1IN&BIT2)>>2;
if (nP12==0) nRes=7;
nP13=(P1IN&BIT3)>>3;
if (nP13==0) nRes=8;
//P1.7输出低电平
P1OUT&=~(BIT7);
nP10=P1IN&BIT0;
if (nP10==0) nRes=1;
nP11=(P1IN&BIT1)>>1;
if (nP11==0) nRes=2;
nP12=(P1IN&BIT2)>>2;
if (nP12==0) nRes=3;
nP13=(P1IN&BIT3)>>3;
if (nP13==0) nRes=4;
P1OUT = 0x00; //恢复以前值
//读取各个管脚的状态
nP10=P1IN&BIT0;
nP11=(P1IN&BIT1)>>1;
nP12=(P1IN&BIT2)>>2;
nP13=(P1IN&BIT3)>>3;
for(;;)
{
if(nP10==1&&nP11==1&&nP12==1&&nP13==1)
{
//有按键被按下,跳出,执行下边程序
break;
}
}
return nRes;
}
//键盘扫描程序
void KeyScan (void)
{
int nP10=0,nP11=0,nP12=0,nP13=0;
int nRes=0;
//是否有按键被按下
while (nP10==0||nP11==0||nP12==0||nP13==0)
{
//有按键被按下,跳出,执行下边程序
break;
}
Delay();
//读取各个管脚的状态
nP10=P1IN&BIT0;
nP11=(P1IN&BIT1)>>1;
nP12=(P1IN&BIT2)>>2;
nP13=(P1IN&BIT3)>>3;
}
//是否有按键被按下
if (nP10==0||nP11==0||nP12==0||nP13==0)
{
//如果没有
nRes = KeyProcess();
}
else return -1;//没有输入,为干扰
return nRes;
}
//请帮忙看看问题都出在哪里

