C 中的 STM32 外设寄存器访问

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

我正在使用 C 对 STM32WB55CGU6 进行编程,并且正在访问外围寄存器以点亮 LED。然而,LED 并未点亮。我的猜测是,我没有以正确的方式写入内存/转换,并且它进入了 HardFault,但我不知道问题出在哪里。我也在组装中完成了它并且有效,所以应该不是硬件问题。它可以正确编译和上传。我的代码有什么问题吗?

#include <stdlib.h>

#define CLOCK_ENABLE_GPIOBC 6
#define PIN_SET_PC15 1<<15
#define MODE_SELECT_PC15 1<<30
static int* RCC = (int*)0x58000000;
static int* GPIOA = (int*)0x48000000;
static int* GPIOB = (int*)0x48000400;
static int* GPIOC = (int*)0x48000800;
static int* GPIOD = (int*)0x48000C00;
static int* GPIOE = (int*)0x48001000;

int main(void)
{
    *(int*)((int)RCC + 0x04C) = CLOCK_ENABLE_GPIOBC;
        
    *GPIOC |= MODE_SELECT_PC15;
    
    *(int*)((int)GPIOC + 0x18) = PIN_SET_PC15;
    
    while(1)
    {}
    
}

我已经从参考手册中获得了所有内存地址和寄存器的布局。

c memory embedded stm32
1个回答
0
投票

问题是

*GPIOC |= MODE_SELECT_PC15

参考手册显示GPIOC MODER寄存器的复位值为0xFFFF FFFF。当我按位或 1 <<15 to it, it changes nothing. For a pin to be in output mode, the corresponding bits have to be 01. When it is in reset state, all the of the bits are 1's.

应用时
© www.soinside.com 2019 - 2024. All rights reserved.