STM32引脚B0用作输入,但B1不起作用

问题描述 投票:0回答:1

我正在使用STM32F103C4、2个按钮和7seg显示屏进行简单的向上/向下计数,并且代码以Keil uVision编写,然后将十六进制文件加载到Proteus中。计数器递增部分工作正常,但计数器递减部分只是未注册任何输入。我尝试过切换引脚,并得出结论,B0引脚正常工作,但由于某些原因,B1引脚无效。我到处搜索,并询问了许多,但找不到解决方案。这与特殊的端口B重置值有关吗?

下面是我的代码和proteus方案的ss。

#include "stm32f10x.h"  // Device header

int dispBroj[]={0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92,  0x82, 0xF8, 0x80, 0x90};
int j = 0;

void reset()
{
    for(int i = 0; i < 7; i++)
    {
     GPIOA->ODR &=~ (1<<i);
    }
}

void cifre(int broj) 
{

    GPIOA->ODR |= dispBroj[broj];

}

int main(void)
{

RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;

GPIOA->CRL |=GPIO_CRL_MODE;
GPIOA->CRL &=~GPIO_CRL_CNF;

GPIOB->CRL|=((1<<7)|(1<<3));
GPIOB->CRL &=~((1<<2)|(1<<1)|(1<<0)|(1<<4)|(1<<5)|(1<<6));
GPIOB->CRH|=((1<<7)|(1<<3));
GPIOB->CRH &=~((1<<2)|(1<<1)|(1<<0)|(1<<4)|(1<<5)|(1<<6));


while(1){

    cifre(j%10);

    if(GPIOB->IDR & GPIO_IDR_IDR0){

        reset();
        j++;
        cifre(j%10);
        while(GPIOB->IDR & GPIO_IDR_IDR0){
            if(!GPIOB->IDR & GPIO_IDR_IDR0)
                break;
        }
    }

    if(GPIOB->IDR & GPIO_IDR_IDR1){

        reset();
        j--;
        cifre(j%10);
        while(GPIOB->IDR & GPIO_IDR_IDR1){
            if(!GPIOB->IDR & GPIO_IDR_IDR1)
                break;
        }
    }
}
}

enter image description here

我怀疑我声明GPIO错误。

c++ stm32 keil proteus
1个回答
0
投票

尝试使用提供的GPIO init的定义:

  GPIO_InitTypeDef  GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1| GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //you have an external pulldown.
  GPIO_Init(GPIOB, &GPIO_InitStructure);
© www.soinside.com 2019 - 2024. All rights reserved.