简体   繁体   English

STM32 使用 CMSIS 闪烁 LED

[英]STM32 Blinking an LED using CMSIS

//setup
    RCC->AHBENR |= 0x20000; //bit 17
    GPIOA->MODER |= 0x100000; // MODER10 (PA10)
    GPIOA->OTYPER |= (1<<10); // bit 10
// main
      GPIOA->BSRR = 0x400; LED HIGH
      HAL_Delay(1000);
      GPIOA->BRR = 0x400; LED LOW
      HAL_Delay(1000);

I am having trouble blinking an external led using PA10.我无法使用 PA10 闪烁外部 LED。 I read the datasheet and used the corresponding registers.我阅读了数据表并使用了相应的寄存器。

The code is supposed to make an external LED blink using registers only (CMSIS) and this LED is connected on PA10.该代码应该仅使用寄存器 (CMSIS) 使外部 LED 闪烁,并且该 LED 连接在 PA10 上。 I am using Nucleo-F303RE board.我正在使用 Nucleo-F303RE 板。

I think you are configuring PA10 to be an open drain (OD) output by setting its OTYPER bit to 1. You forgot to say how your LED is connected, but if you have to drive the I/O pin high to turn on the LED, then you don't want it to be an open drain output.我认为您通过将其 OTYPER 位设置为 1 将 PA10 配置为开漏 (OD) output。您忘了说您的 LED 是如何连接的,但是如果您必须将 I/O 引脚驱动为高电平以打开 LED ,那么您不希望它是开漏 output。 Try removing the line that sets the OTYPER bit.尝试删除设置 OTYPER 位的行。

OTYPER registers selects output type, where 0 in a bit means push-pull mode on the corresponding pin, and 1 - open-drain . OTYPER寄存器选择 output 类型,其中一位为 0 表示相应引脚上的推挽模式,1 -开漏

Open-drain means that port is connected to the GND when output value is 0, and left floating when output value is 1. This mode is intended to use with a pull-up resistor (internal or external).开漏是指当 output 值为 0 时端口连接到 GND,当 output 值为 1 时悬空。此模式旨在与上拉电阻(内部或外部)一起使用。

If your LED is connected between the output and GND, you should use push-pull mode.如果您的 LED 连接在 output 和 GND 之间,则应使用推挽模式。 Ie you need to keep zero value in the bit in MODER register.即您需要在MODER寄存器的位中保持零值。

And also my advice: try to avoid "magic numbers" use bit names from CMSIS.还有我的建议:尽量避免“幻数”使用 CMSIS 中的位名称。 Eg: instead of例如:代替

RCC->AHBENR |= 0x20000;

write

RCC->AHBENR |= RCC_AHBENR_IOPAEN;

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

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