读取 GPIOB_IDR 寄存器时值不正确

Incorrect value when reading GPIOB_IDR register

我在 atollic true studio 上对 stm32f030r8 arm 编程感到困惑 ide。

我在从 idr 寄存器读取正确数据时遇到了一些问题。

我下拉GPIOB的PUPR寄存器(0,1,2,3)。

GPIOB的其他引脚是我用MODER寄存器做的输出。

当我每次读取F值时循环读取idr数据但没有任何输入

请帮我解决这个问题[

#include "main.h"
int main(void)
{
 volatile static uint16_t PortDataInput=0x00;
 RCC->CR|=(uint32_t)0xF1; //set hsi clock source and with max speed
 GPIOB->PUPDR|=0xAA; //set firt 4 bit of gpiob as pull down
 GPIOB_RCC->AHBENR|=(1<<18); //enable gpiob clock source
 GPIOB->MODER|=0x55555500; //set firt 4 bit of gpiob as input
 GPIOB->OTYPER|=0x00000000; //set output pins of gpiob as push pull
 while (1)
 { 
 PortDataInput=GPIOB->IDR;
 PortDataInput&=0xF;
 }

您尝试在启用外设时钟之前设置gpio 寄存器。所以你不能写入它的任何寄存器。

我找到了解决方案。 解决方案是 that.I 必须在启用外设时钟后设置所有 gpio 寄存器。

#include "main.h"
int main(void)
{
 volatile static uint16_t PortDataInput=0x00;
 RCC->CR|=(uint32_t)0xF1; //set hsi clock source and with max speed
 GPIOB_RCC->AHBENR|=(1<<18); //enable gpiob clock source
 GPIOB->PUPDR|=0xAA; //set firt 4 bit of gpiob as pull down
 GPIOB->MODER|=0x55555500; //set firt 4 bit of gpiob as input
 GPIOB->OTYPER|=0x00000000; //set output pins of gpiob as push pull
while(1)
{
 PortDataInput=GPIOB->IDR;
 PortDataInput&=0xF;
}