GPIO模式寄存器

时间:2014-11-26 20:23:28

标签: c++ embedded gpio stm32f4discovery

我已经为{3}}调整了STM3240G-EVAL板的示例,以使LED 3和4闪烁。我有它工作,但我对模式寄存器设置感到困惑:

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

当我读到here(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;
}

1 个答案:

答案 0 :(得分:1)

您没有将其设置为零,而是将其设置为一个。

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后缀是指屏蔽的位数,而不是写入的值。