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();
  }
}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
5条回答
lujiangyuan
1楼-- · 2019-08-06 18:36
受字数限制,我删掉了函数的声明,和一些注释
yirongfu
2楼-- · 2019-08-06 19:54
  //判断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;

红 {MOD}部分有误吧,你的代码是顺序执行的,这样只要m13IN不等于0,就都会把nRes赋值为0。
dirtwillfly
3楼-- · 2019-08-06 23:12
楼上说的对
lujiangyuan
4楼-- · 2019-08-07 03:21
yirongfu 发表于 2014-2-17 21:18
//判断S13~S16
   P1OUT = 0x70;
  if (m10IN == 0)

你说的对  
G21372
5楼-- · 2019-08-07 05:30
 精彩回答 2  元偷偷看……

一周热门 更多>