c++ - GPIO模式寄存器

标签 c++ embedded gpio stm32f4discovery

我调整了 here 中的示例用于 STM3240G-EVAL 板,以便使 LED 3 和 4 闪烁。我可以正常工作,但对模式寄存器设置感到困惑:

GPIOG->MODER |= (GPIO_MODER_MODER6_0 | GPIO_MODER_MODER8_0) ;

当我阅读 reference manual (p186),它声称必须将模式设置为 01 才能输出,但以这种方式将其设置为 0 就可以了。理想情况下,我希望能够更改为其他模式,但我假设上面的代码会将端口 G 的引脚 6 和 8 更改为输入引脚。我一定是遗漏了什么。

如果相关,这是我完整的主要文档:

#include "stm32f4xx.h"

/* We will use PG6 and PG8 connected to LEDs 1 and 2 because they're the same port. */
/* Find base register value for Port G                                              */


void delay (int a);

int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f0xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f0xx.c file
    */

    /* GPIOG Periph clock enable */
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOGEN;

GPIOG->MODER |= (GPIO_MODER_MODER6_0 | GPIO_MODER_MODER8_0) ;
    /* Configure PG6 and PG8 in output  mode  */

GPIOG->OTYPER &= ~(GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_8) ;
// Ensure push pull mode selected--default

GPIOG->OSPEEDR |= (GPIO_OSPEEDER_OSPEEDR6|GPIO_OSPEEDER_OSPEEDR8);
//Ensure maximum speed setting (even though it is unnecessary)

GPIOG->PUPDR &= ~(GPIO_PUPDR_PUPDR6|GPIO_PUPDR_PUPDR8);
//Ensure all pull up pull down resistors are disabled

while (1)
{
    /* Set PG6 and PG8 */
    /* the bit set/reset low register SETS the output data register     */
    /* the bit set/reset high register RESETS the output data register  */

    GPIOG -> BSRRL = (1 << 6);
    GPIOG -> BSRRL = (1 << 8);
    delay(500000);
    /* Reset PC8 and PC9 */
    GPIOG -> BSRRH = (1 << 6);
    GPIOG -> BSRRH = (1 << 8);
    delay(500000);
    }

return 0;
} 

void delay (int a)
{
volatile int i,j;

for (i=0 ; i < a ; i++)
{
    j++;
}

return;
}

最佳答案

您不是将其设置为零,而是将其设置为一。

GPIO_MODER_MODER6_0 常量的定义是 0x00001000GPIO_MODER_MODER6 位的掩码是 0x00003000,因此您将位 01 放在正确的位置。

如果常量 GPIO_MODER_MODER6_0 被定义为零,那么在任何情况下将其放入配置寄存器都不会产生任何影响。要将这两个位都设置为零,您需要执行以下操作:

GPIOG->MODER &= ~(GPIO_MODER_MODER6_0 | GPIO_MODER_MODER6_1);

_0_1 后缀指的是用于屏蔽的位数,而不是写入的值。

关于c++ - GPIO模式寄存器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27158211/

相关文章:

python - 为什么我的 GPIO 引脚无法使用 RPi.GPIO 正确设置?

python - 使用Python从MCP23017获取信息

android - 在 Centos 7 上为 Android 交叉编译 protobuf 2.5.0

c++ - C++ 类何时在堆栈上实例化?

python - 将 Python 移植到嵌入式系统

c++ - 位设置和代码可读性

c++ - 指针比较

c++ - C/C++ 如何从 2 个数组中获取唯一值?

embedded - 小型嵌入式合成语音库/建议

timer - STM32 MCU 上 DMA 到 GPIO 的可靠性如何?