用STM32L053做矩阵键盘的代码为什么IO口读不出数据

2019-07-14 18:26发布

麻烦各位帮忙看看
#include "KEY.h"
#define KEY_PIN_1            GPIO_PIN_0
#define KEY_PIN_2            GPIO_PIN_1
#define KEY_PIN_3            GPIO_PIN_2
#define KEY_PIN_4            GPIO_PIN_3
#define KEY_PIN_5            GPIO_PIN_4
#define KEY_PIN_6            GPIO_PIN_5
#define KEY_PIN_7            GPIO_PIN_6
#define KEY_PIN_8            GPIO_PIN_7
#define KEY_PIN_ALL         KEY_PIN_1|KEY_PIN_2|KEY_PIN_3|KEY_PIN_4|KEY_PIN_5|KEY_PIN_6|KEY_PIN_7|KEY_PIN_8
#define KEY_PORT             GPIOB

void GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct;
        
  __HAL_RCC_GPIOB_CLK_ENABLE();
        __HAL_RCC_GPIOA_CLK_ENABLE();
        //__HAL_RCC_GPIOH_CLK_ENABLE();
        
  GPIO_InitStruct.Pin = KEY_PIN_1|KEY_PIN_2|KEY_PIN_3|KEY_PIN_4;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        HAL_GPIO_Init(KEY_PORT, &GPIO_InitStruct);
        
        GPIO_InitStruct.Pin = GPIO_PIN_5;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
        
        GPIO_InitStruct.Pin = KEY_PIN_5|KEY_PIN_6|KEY_PIN_7|KEY_PIN_8;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  //GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        
  HAL_GPIO_Init(KEY_PORT, &GPIO_InitStruct);
}

unsigned char HL_Scan(void)
{
        unsigned char KeyVal=0;
        unsigned key=0;
        //unsigned int WriteVal=0;
        if(key==0)
        {
                HAL_GPIO_WritePin(KEY_PORT,KEY_PIN_1|KEY_PIN_2|KEY_PIN_3|KEY_PIN_4,GPIO_PIN_SET);
                if(HAL_GPIO_ReadPin(KEY_PORT,KEY_PIN_5|KEY_PIN_6|KEY_PIN_7|KEY_PIN_8)==0)
                {
                        key=0;
                        return 0;
                }
                else
                {
                        HAL_Delay(5);
                        if(HAL_GPIO_ReadPin(KEY_PORT,KEY_PIN_5|KEY_PIN_6|KEY_PIN_7|KEY_PIN_8)==0)
                        {
                                key=0;
                                return 0;
                        }
                        else
                                key=1;
                }
        }
        if(key==1)
        {        
                HAL_GPIO_WritePin(KEY_PORT,KEY_PIN_1,GPIO_PIN_SET);
                HAL_GPIO_WritePin(KEY_PORT,KEY_PIN_2|KEY_PIN_3|KEY_PIN_4,GPIO_PIN_RESET);
                switch(HAL_GPIO_ReadPin(KEY_PORT,KEY_PIN_5|KEY_PIN_6|KEY_PIN_7|KEY_PIN_8)&0xf0)
                {
                        case 0x10: KeyVal=15;        break;
                        case 0x20: KeyVal=11;        break;
                        case 0x40: KeyVal=7;        break;
                        case 0x80: KeyVal=3;        break;
                }
                HAL_GPIO_WritePin(KEY_PORT,KEY_PIN_2,GPIO_PIN_SET);
                HAL_GPIO_WritePin(KEY_PORT,KEY_PIN_1|KEY_PIN_3|KEY_PIN_4,GPIO_PIN_RESET);
                switch(HAL_GPIO_ReadPin(KEY_PORT,KEY_PIN_5|KEY_PIN_6|KEY_PIN_7|KEY_PIN_8)&0xf0)
                {
                        case 0x10: KeyVal=14;        break;
                        case 0x20: KeyVal=10;        break;
                        case 0x40: KeyVal=6;        break;
                        case 0x80: KeyVal=2;        break;
                }
                HAL_GPIO_WritePin(KEY_PORT,KEY_PIN_3,GPIO_PIN_SET);
                HAL_GPIO_WritePin(KEY_PORT,KEY_PIN_1|KEY_PIN_2|KEY_PIN_4,GPIO_PIN_RESET);
                switch(HAL_GPIO_ReadPin(KEY_PORT,KEY_PIN_5|KEY_PIN_6|KEY_PIN_7|KEY_PIN_8)&0xf0)
                {
                        case 0x10: KeyVal=13;        break;
                        case 0x20: KeyVal=9;        break;
                        case 0x40: KeyVal=5;        break;
                        case 0x80: KeyVal=1;        break;
                }
                HAL_GPIO_WritePin(KEY_PORT,KEY_PIN_4,GPIO_PIN_SET);
                HAL_GPIO_WritePin(KEY_PORT,KEY_PIN_1|KEY_PIN_2|KEY_PIN_3,GPIO_PIN_RESET);
                switch(HAL_GPIO_ReadPin(KEY_PORT,KEY_PIN_5|KEY_PIN_6|KEY_PIN_7|KEY_PIN_8)&0xf0)
                {
                        case 0x10: KeyVal=12;        break;
                        case 0x20: KeyVal=8;        break;
                        case 0x40: KeyVal=4;        break;
                        case 0x80: KeyVal=0;        break;
                }
                key=0;
        }
        return KeyVal;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。