0

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

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

当我阅读参考手册(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;
}
4

1 回答 1

1

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

GPIO_MODER_MODER6_0常数的定义是0x00001000。位的掩码GPIO_MODER_MODER60x00003000,因此您将位01放在正确的位置。

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

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

_0和后缀指的_1是用于掩码的位数,而不是正在写入的值。

于 2014-11-26T22:26:05.490 回答