embedded - 内置 LED 不会打开 STM32F303RE Nucleo 板

标签 embedded stm32 cpu-registers gpio led

我正在尝试通过 STM32CubeIDE 仅使用寄存器来打开 nucleo 板内的 LED(原理图中的 LD2)。

Schematic for the STM32F303RE board.

用户手册声明了时钟、模式和数据寄存器的以下地址:

Led pin: PA5

Address of the Clock control register: RCC_AHBENR
[base address] + [offset] ===> [Result]
  0x4002 1000  +   0x14   ===> 0x40021014

Address of the GPIOA mode register
0x4800 0000 + 0x00 ===> 0x48000000

Address of the GPIOA output data register
0x4800 0000 + 0x14 ===> 0x48000014

我正在使用以下代码来设置/清除板中的寄存器:

#include <stdint.h>

int main(void)
{
    uint32_t *pClkCtrlReg = (uint32_t*)0x40021014;
    uint32_t *pPortAModeReg = (uint32_t*)0x48000000;
    uint32_t *pPortAOutReg = (uint32_t*)0x48000014;

    //1. enable the clock for GPIOA peripheral in the AHBENR
    *pClkCtrlReg |= 0x00020000;

    //2. configure the mode of the IO pin as output
    //a. clear the 24th and 25th bit positions
    *pPortAModeReg &= 0xFCFFFFFF;
    //b set 24th bit position as 1
    *pPortAModeReg |= 0x01000000;

    //3. SET 12th bit of the output data register to make I/O pin-12 as HIGH
    *pPortAOutReg |= 0x20;



    while(1);
}

使用 IDE 中的寄存器查看器,我可以看到 PA5 被设置为输出,但实际上,我的 LED 没有打开。

Position 6 is the bit that is turned on

我不知道我做错了什么。我怀疑 PA5 的引脚是错误的,但我也尝试了 PA12,但它不起作用。有人可以帮我解决这个问题吗?

最佳答案

我手头有引用手册,遍历了您的代码。 RM0316 STM32F303 Reference manual .

您正确激活了 GPIO 端口 A 的时钟(此外,如果 GPIOA 寄存器未被激活,它也会读取所有 0x00)。

然后你将 GPIO 模式设置为,引用你的话:

//2. configure the mode of the IO pin as output
//a. clear the 24th and 25th bit positions
*pPortAModeReg &= 0xFCFFFFFF;
//b set 24th bit position as 1
*pPortAModeReg |= 0x01000000;

您使用第 24 位和第 25 位。它们是:

enter image description here

因此,您为引脚 A12 而不是 A5 设置了模式。对于 GPIOA 引脚 5,您需要操作位 10 和 11。

//clear pin5 bits
*pPortAModeReg &= ~(0x03 << 10); //take 0b11, shift it to position 10, flip all bits, AND with original state of register

然后将这些位设置为“通用输出模式”01,您也可以对错误的位执行此操作:

//b set 10th bit position as 1
*pPortAModeReg |= (0x01 << 10);

我检查了 GPIO 的所有寄存器,应该没有其他需要设置的东西。如果仍然无法正常工作,请发布所有 GPIOA 寄存器的内容。

编辑:另外,尝试使用位设置/重置寄存器。请注意,它们是只读的,因此没有“|=”,只有“=”。将 0 写入它们不会执行任何操作。所以你只需要直接写入 (0x01<<5) 到 GPIOA BSRR 寄存器。

GPIOA->BSRR = (1U<<5); //or however you want to address that register

关于embedded - 内置 LED 不会打开 STM32F303RE Nucleo 板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72892782/

相关文章:

stm32 - 关于STM32H7 SPI在从机模式下的限制

cpu-registers - 两个命名的特殊用途寄存器之间的区别 - MBR 和 IR

c - 抢占式调度算法

assembly - 我如何在 Linux 64 位上从 C 编写简单的内联 asm 指令?

c++ - 如何随时暂停 pthread?

C语言数组和for循环不能正常工作

oop - 为什么面向对象的语言在嵌入式世界中不流行?

embedded - 我可以在不重置开发板的情况下重置 Microchip TCP/IP 堆栈吗?

eclipse-plugin - STM32 Atollic TrueSTUDIO - 内存的图形 View

c - 如何在 STM32F10x 上重定向 printf()?