stm32+TM1638不能读键,程序出了什么问题,请大神指点,谢谢。

2019-07-14 19:03发布

这是我的程序(是STM32F767+tm1638)。就是键值一直读不出来,不知是什么原因?
TM1638.c
#include "delay.h"
#include "stm32f7xx_hal.h"
#include "TM1638.h"       

void TM1638_GPIO_Init(void)                         //ÅäÖÃGPIO
        {
    GPIO_InitTypeDef GPIO_Initure;
                 __HAL_RCC_GPIOA_CLK_ENABLE();           //¿ªÆôGPIOAʱÖÓ
                          
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP ;
                GPIO_Initure.Pull=GPIO_PULLUP;
                GPIO_Initure.Speed=GPIO_SPEED_HIGH ;
                GPIO_Initure.Pin=GPIO_PIN_4;
    HAL_GPIO_Init(GPIOA,&GPIO_Initure);
                          
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP ;
                GPIO_Initure.Pull=GPIO_PULLUP;
                GPIO_Initure.Speed=GPIO_SPEED_HIGH ;
                GPIO_Initure.Pin=GPIO_PIN_5;
    HAL_GPIO_Init(GPIOA,&GPIO_Initure);
                          
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_OD  ;
                GPIO_Initure.Pull=GPIO_PULLUP;
                GPIO_Initure.Speed=GPIO_SPEED_HIGH ;
                GPIO_Initure.Pin=GPIO_PIN_1;
    HAL_GPIO_Init(GPIOA,&GPIO_Initure);        
}

void TM1638_Write(unsigned char        DATA)                        //写数据函数
{
        unsigned char i;
        for(i=0;i<8;i++)
        {
                Reset_TM1638_CLK;
                delay_us(1);
                if(DATA&0X01){
                        Set_TM1638_DIO;
                }
                else{
                        Reset_TM1638_DIO;
                }
                DATA>>=1;
                Set_TM1638_CLK ;
                delay_us(1);
        }
}

unsigned char TM1638_Read(void)                                        //读数据函数
{
        unsigned char i;
        unsigned char temp=0;
        Set_TM1638_DIO;
        for(i=0;i<8;i++)
        {
          temp>>=1;
    Reset_TM1638_CLK;
               
                if(Get_tm1638_DIO)
                        temp|=0x80;
                Set_TM1638_CLK;
                delay_us(1);
        }
        return temp;
}               


unsigned char Read_key(void)       //读取按键值
{
        unsigned char c[4],i,key_value=0;
        Reset_TM1638_STB;
        TM1638_Write(0x42);
        delay_ms(10);
        for(i=0;i<4;i++)
                c=TM1638_Read();
  Set_TM1638_STB;                                       
        if(c[0]==0x04) key_value=1;
        if(c[0]==0x40) key_value=2;
        if(c[1]==0x04) key_value=3;
        if(c[1]==0x40) key_value=4;     
        if(c[2]==0x04) key_value=5;
        if(c[2]==0x40) key_value=6;
        if(c[3]==0x04) key_value=7;
        if(c[3]==0x40) key_value=8;
        if(c[0]==0x02) key_value=9;
        if(c[0]==0x20) key_value=10;
        if(c[1]==0x02) key_value=11;
        if(c[1]==0x20) key_value=12;
        if(c[2]==0x02) key_value=13;
        if(c[2]==0x20) key_value=14;
        if(c[3]==0x02) key_value=15;
        if(c[3]==0x20) key_value=16;
        return key_value;
}


TM1638.h:
#ifndef __TM1638_H
#define __TM1638_H
#include "sys.h"
       
#define  Set_TM1638_DIO  HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET)
#define  Reset_TM1638_DIO  HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET)
#define  Set_TM1638_CLK  HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET)
#define  Reset_TM1638_CLK  HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET)
#define  Set_TM1638_STB   HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET)
#define  Reset_TM1638_STB  HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET)
#define  Get_tm1638_DIO    HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_1)



void RCC_Config(void);
void GPIO_Config(void);
void TM1638_Write(uint8_t byte);
unsigned char TM1638_Read(void)        ;
unsigned char Read_key(void);

#endif


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