简体   繁体   English

STM32闪烁的LED错误寄存器?

[英]STM32 blinking LED wrong register?

Has anyone an idea why the code doesn't work?有谁知道为什么代码不起作用?

RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
GPIOB->MODER &= ~(0x3u << 6u);
GPIOB->MODER |= (0x1u << 6u);
for (int i = 0; i < 1000; i++) {
    GPIOB->ODR |= (0x1u << 0x3u);
}

I'm using a STM board (STM32L432KC) which has an integrated LED that is called pin PB3 (Port 26) but after flashing nothing happens.我使用的是 STM 板 (STM32L432KC),它有一个集成 LED,称为 PB3 引脚(端口 26),但在闪烁后没有任何反应。 Actually there should shine a LED.实际上应该有一个发光二极管。 Do I use the right register?我是否使用了正确的寄存器?

You're setting a bit in the ODR but never clearing it, so it won't flash, you will need a delay between on/off transitions to be able to visibly see it flashing.您在ODR设置了一点但从未清除它,因此它不会闪烁,您需要在开/关转换之间有一个延迟才能明显看到它在闪烁。

It's good practice to write to the BSRR register instead of the ODR too where possible, this allows you to avoid read-modify-write cycles.在可能的情况下写入BSRR寄存器而不是ODR是一种很好的做法,这样可以避免读取-修改-写入周期。

If that code is your whole main() then you might want to replace the for (int i = 0; i < 1000; i++) with a while(1) , you generally don't want to return from main in an embedded context.如果该代码是您的整个main()那么您可能想用while(1)替换for (int i = 0; i < 1000; i++) while(1) ,您通常不想在嵌入式上下文中从main返回.

This sequence is wrong (I did not check if you use the correct RCC register)这个顺序是错误的(我没有检查你是否使用了正确的RCC寄存器)

RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
GPIOB->MODER &= ~(0x3u << 6u);

When you enable the peripheral clock you need to readback the value or add some delay your changes to propagate through the bus.当您启用外设时钟时,您需要回读该值或添加一些延迟您的更改以通过总线传播。 I usually use the barrier instruction for that.我通常使用屏障指令。

RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
__DMB();
GPIOB->MODER &= ~(0x3u << 6u);

If you do not add this delay (or readback) the first opearation will fail as it will be performed on the non clocked GPIO periperal.如果您不添加此延迟(或回读),则第一次操作将失败,因为它将在非时钟 GPIO 外设上执行。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM