微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 有关stm32,4*4矩阵键盘的键值用1602显示的问题

有关stm32,4*4矩阵键盘的键值用1602显示的问题

时间:10-02 整理:3721RD 点击:
下面是我的按键函数:
#include "key_scan.h"
void Keyboard_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2| GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//??à ê?è?
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
uint8_t Get_KeyValue(void)
{
uint8_t temp_key =0, key;
key=0;
GPIO_Write(GPIOC, 0xfe);
temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
temp_key = temp_key & 0xf0;     //11110000


if(temp_key != 0xf0)
{
  Delay_Us(10);
  temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
  temp_key = temp_key & 0xf0;
  if(temp_key != 0xf0)
  {
   temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
   switch(temp_key)
   {
    case 0xee: key = '/';  break;
    case 0xde: key = '*'; break;
    case 0xbe: key = '-';  break;
    case 0x7e: key = '+';  break;
   }   
   while(temp_key != 0xf0)    //?
   {
    temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
    temp_key = temp_key&0xf0;
   }
  }
}    /* end of if(temp_key!=0xf0)*/
GPIO_Write(GPIOC,0xfd);
temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
temp_key=temp_key&0xf0;     //11110000
if(temp_key!=0xf0)
{
  Delay_Us(10);
  temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
  temp_key=temp_key&0xf0;
  if(temp_key!=0xf0)
  {
   temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
   switch(temp_key)
   {
    case 0xed: key='=';  break;
    case 0xdd: key='9';  break;
    case 0xbd: key='6';  break;
    case 0x7d: key='3';  break;
   }   
   while(temp_key!=0xf0)    //?
   {
    temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
    temp_key=temp_key&0xf0;
   }
  }
}    /* end of if(temp_key!=0xf0)*/
GPIO_Write(GPIOC,0xfb);
temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
temp_key=temp_key&0xf0;     //11110000
if(temp_key!=0xf0)
{
  Delay_Us(10);
  temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
  temp_key=temp_key&0xf0;
  if(temp_key!=0xf0)
  {
   temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
   switch(temp_key)
   {
    case 0xeb: key='0';  break;
    case 0xdb: key='8';  break;
    case 0xbb: key='5';  break;
    case 0x7b: key='2';  break;
   }   
   while(temp_key!=0xf0)    //?
   {
    temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
    temp_key=temp_key&0xf0;
   }
  }
}    /* end of if(temp_key!=0xf0)*/
GPIO_Write(GPIOC,0xf7);
temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
temp_key=temp_key&0xf0;     //11110000
if(temp_key!=0xf0)
{
  Delay_Us(10);
  temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
  temp_key=temp_key&0xf0;
  if(temp_key!=0xf0)
  {
   temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
   switch(temp_key)
   {
    case 0xe7: key='.';  break;
    case 0xd7: key='7';  break;
    case 0xb7: key='4';  break;
    case 0x77: key='1';  break;
   
   }   
   while(temp_key!=0xf0)    //?
   {
    temp_key = GPIO_ReadInputData(GPIOC);//>>0x8;
    temp_key=temp_key&0xf0;
   }
  }
}   
return key;
}
void Delay_Us(unsigned int t)
{   
while(--t);
}
这是我在主函数中用1602显示键值的代码:
LCD_Write_Char(0,0,Get_KeyValue());
可是不知道为什么1602上面总显示不出,为什么?求助。难道是那个get_keyvalue函数返回的类型不对吗?

1602上面总显示不出,要确定能显示一些内容,即,你的LCD驱动是好的。
按设置成输入模式。仿真下,看能不能读取到IO数据

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top