gpt4 book ai didi

c++ - GPIO模式寄存器

转载 作者:行者123 更新时间:2023-11-28 06:33:25 24 4
gpt4 key购买 nike

我调整了 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com