MSP430 矩阵键盘 if函数问题

2019-08-06 14:09发布

我写的矩阵键盘的程序有个问题
int KeyProcess()
{
  int nRes = 0;;


  //判断S1~S4
  P1OUT = 0xe0;
  if (m10IN == 0)         我按下了这个对应按键 ,我一步一步仿真到这边了,这条语句也执行了为真
    nRes = 1;            然后就执行了这条语句,这条语句执行完之后我发现  监视里面  nRes  是个无效的值   压根没有赋值,  最后nRes 的值是到最后没有按键按下的时候通过  else  赋值为0。请叫各位大神这是个什么情况   (我先没有用中断做)
   
//下面是程序全部类容


#include "io430.h"
#define led20 P2OUT_bit.P0
#define led21 P2OUT_bit.P1
#define led22 P2OUT_bit.P2
#define m10IN P1IN_bit.P0
#define m11IN P1IN_bit.P1
#define m12IN P1IN_bit.P2
#define m13IN P1IN_bit.P3
#define m14OUT P1OUT_bit.P4
#define m15OUT P1OUT_bit.P5
#define m16OUT P1OUT_bit.P6
#define m17OUT P1OUT_bit.P7


void Delay(void)
{
  unsigned int i;
  for(i=0;i<100;i++);
  
}

void Display(int i)
{
  int j;
  for(j=i;j>0;j--)
  {
    led20 = 0;
    led21 = 0;
    led22 = 0;
    Delay();
    Delay();
    led20 = 1;
    led21 = 1;
    led22 = 1;
    Delay();
    Delay();
  }  
  return;  
}

/******************************************
端口初始化
*******************************************/
void int_port()
{  
  P1SEL = 0;  //将P1设置成IO口功能
  P1DIR = 0x00;
  P1DIR = 0xf0;  //将P1.0 1.1 1.2 1.3 设置成输入IO,将1.4 1.5 1.6 1.7设置成输出方向
  P1OUT = 0x00;
  
  P2SEL = 0;
  P2DIR = 0x07;
  P2OUT = 0x07;  
  return ;
  
}


/******************************************
键盘扫描程序
*******************************************/
int KeyScan()
{
  int nRes = 0;
  P1OUT = 0x00;
  for(;;)
  {
    //读取各个管脚的状态
    if(m10IN == 0 || m11IN == 0 || m12IN == 0 || m13IN == 0)//检测是否有按键按下
    {
      break;
      
    }   
  }
  
  Delay();
  
  if(m10IN == 0 || m11IN == 0 || m12IN == 0 || m13IN == 0)//检测是否有按键按下
    {
      
      nRes = KeyProcess();
      
    }   
  else return -1;
  
  return nRes;  
}

/******************************************
键盘分析程序
*******************************************/
int KeyProcess()
{
  int nRes = 0;;


  //判断S1~S4
  P1OUT = 0xe0;
  if (m10IN == 0)
    nRes = 1;
  if (m11IN == 0)
    nRes = 2;
  if (m12IN == 0)
    nRes = 3;
  if (m13IN == 0)
    nRes = 4;

  //判断S5~S8
   P1OUT = 0xd0;
  if (m10IN == 0)
    nRes = 5;
  if (m11IN == 0)
    nRes = 6;
  if (m12IN == 0)
    nRes = 7;
  if (m13IN == 0)
    nRes = 8;

  //判断S9~S12
  P1OUT = 0xb0;
  if (m10IN == 0)
    nRes = 9;
  if (m11IN == 0)
    nRes = 10;
  if (m12IN == 0)
    nRes = 11;
  if (m13IN == 0)
    nRes = 12;
  
  //判断S13~S16
   P1OUT = 0x70;
  if (m10IN == 0)
    nRes = 13;
  if (m11IN == 0)
    nRes = 14;
  if (m12IN == 0)
    nRes = 15;
  if (m13IN == 0)
    nRes = 16;
  else nRes = 0;
  P1OUT = 0x00;
  
  
  //按键释放
  for(;;)
  {
     if (m10IN == 1 && m11IN == 1 && m12IN == 1 && m13IN == 1)
     {
       break;
     }
   
  }

   Display(nRes);
   return nRes;
  
  
}


int main( void )
{
  WDTCTL = WDTPW + WDTHOLD;
  BCSCTL1 = 0x47;  //最高时钟
  BCSCTL2 = 0x80;  
  int_port();  
  while(1)
  {
    KeyScan();
  }
}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。