【问题标题】:GPIO mode registerGPIO模式寄存器
【发布时间】:2014-11-27 06:26:05
【问题描述】:

我已经为 STM3240G-EVAL 板调整了 here 中的示例,以使 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;
}

【问题讨论】:

    标签: c++ embedded gpio stm32f4discovery


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 啊哈!那讲得通。谢谢!
    猜你喜欢
    • 2022-11-11
    • 2021-11-03
    • 2019-07-11
    • 1970-01-01
    • 2014-11-17
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多