键盘问题

2019-03-24 15:55发布

#include <msp430x14x.h>
#define unt unsigned int

void Delay(void);
int KeyProcess(void);
void Init_Port(void);
void Init_CLK(void);
int KeyScan(void);
void delay1(unt);

void main(void)
{
  int t,i;
  int ch[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
  Init_Port();
  t=KeyScan();
  i=t;
  P5DIR=0xFF;
  P5OUT&=0x00;
  while(i<8)
  {
    P5OUT=ch;
    delay1(6000);
    i++;
  }
}



void Init_Port(void)
{
  //将P1P6口所有的管脚在初始化的时候设置为输入方式
  P1DIR = 0x00;
  P6DIR = 0x00;
  
  //将P1P6口所有的管脚设置为一般I/O口
  P1SEL = 0x00;
  P6SEL = 0x00;
  
  // 将P6.4 P6.5 P6.6 P6.7设置为输出方向
  P6DIR |= BIT7;
  P6DIR |= BIT6;
  P6DIR |= BIT5;
  P6DIR |= BIT4;
  
  //先输出低电平
  P1OUT = 0x00;
  P6OUT = 0X00;
  return;
}

int KeyScan(void)
{
        int nP10,nP11,nP12,nP13;
        int nRes = 0;
        for(;;)
        {
                //读取各个管脚的状态,左移几位,比较当前为的值,判断是否为零
                nP10 = P1IN & BIT0;
                nP11 = (P1IN & BIT1) >> 1;
                nP12 = (P1IN & BIT2) >> 2;
                nP13 = (P1IN & BIT3) >> 3;

                //是否有键被按下
                if(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 Delay(void)
{
  int i;
  for(i = 100;i > 0;i--) ;//延时一点时间
}


void delay1(unt z)
{
unt a;
for(a=0;a<z;a++);
}



int KeyProcess(void)
{
  int nRes = 0x00;
  int nP10;
  int nP11;
  int nP12;
  int nP13;
  //P6.7输出低电平
  P6OUT =0x7f;
  nP10 = P1IN & BIT0;
  if (nP10 == 0) nRes = 0;
  nP11 = (P1IN & BIT1) >> 1;
  if (nP11 == 0) nRes = 4;
  nP12 = (P1IN & BIT2) >> 2;
  if (nP12 == 0) nRes = 0;
  nP13 = (P1IN & BIT3) >> 3;
  if (nP13 == 0) nRes = 4;
  //P6.6输出低电平
  P6OUT =0xbf;
  nP10 = P1IN & BIT0;
  if (nP10 == 0) nRes =1;
  nP11 = (P1IN & BIT1) >> 1;
  if (nP11 == 0) nRes =5;
  nP12 = (P1IN & BIT2) >> 2;
  if (nP12 == 0) nRes = 1;
  nP13 = (P1IN & BIT3) >> 3;
  if (nP13 == 0) nRes = 5;
  //P6.5输出低电平
  P6OUT =0xdf;
  nP10 = P1IN & BIT0;
  if (nP10 == 0) nRes =2;
  nP11 = (P1IN & BIT1) >> 1;
  if (nP11 == 0) nRes = 6;
  nP12 = (P1IN & BIT2) >> 2;
  if (nP12 == 0) nRes = 2;
  nP13 = (P1IN & BIT3) >> 3;
  if (nP13 == 0) nRes = 6;
  //P6.4输出低电平
  P6OUT=0xef;
  nP10 = P1IN & BIT0;
  if (nP10 == 0) nRes = 3;
  nP11 = (P1IN & BIT1) >> 1;
  if (nP11 == 0) nRes = 7;
  nP12 = (P1IN & BIT2) >> 2;
  if (nP12 == 0) nRes = 3;
  nP13 = (P1IN & BIT3) >> 3;
  if (nP13 == 0) nRes = 7;
  
  P1OUT = 0x00;
  P6OUT = 0X00;//恢复以前值。
  
  //读取各个管脚的状态
  nP10 = P1IN & BIT0;
  nP11 = (P1IN & BIT1) >> 1;
  nP12 = (P1IN & BIT2) >> 2;
  nP13 = (P1IN & BIT3) >> 3;
  for(;;)
  {
     nP10 = P1IN & BIT0;
     nP11 = (P1IN & BIT1) >> 1;
     nP12 = (P1IN & BIT2) >> 2;
     nP13 = (P1IN & BIT3) >> 3;
    if(nP10 == 1 && nP11 == 1 && nP12 == 1 && nP13 == 1)
    {
      //等待松开按键
      break;
    }
  }
  return nRes;
}



void Init_CLK(void)
{
  unsigned int i;
  BCSCTL1 = 0X00;                        //将寄存器的内容清零
  //XT2震荡器开启
  //LFTX1工作在低频模式
  //ACLK的分频因子为1
  
  do
  {
    IFG1 &= ~OFIFG;                       // 清除OSCFault标志
    for (i = 0x20; i > 0; i--);               
  }
  while ((IFG1 & OFIFG) == OFIFG);      // 如果OSCFault =1   
  
  BCSCTL2 = 0X00;                        //将寄存器的内容清零
  BCSCTL2 += SELM1;                //MCLK的时钟源为TX2CLK,分频因子为1
  BCSCTL2 += SELS;                //SMCLK的时钟源为TX2CLK,分频因子为1
}

运行的时候主函数的delay老是跑到int KeyScan(void)中去,这是为什么???? 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
ddllxxrr
1楼-- · 2019-03-24 17:33
 精彩回答 2  元偷偷看……
huasn_xu
2楼-- · 2019-03-24 21:49
< :TI_MSP430_内容页_SA7 --> henhao

相关问题

    相关文章