ST32F4 GPIO使能两次?

ST32F4 GPIO enabling twice?

我开始在 STM32f4 探索板上进行 arm 开发。我将 CooCox 与 GCC ARM 编译器和 CMSIS 的 STM32 库一起使用。

我创建了以下函数来配置我的 GPIO。首先,我将端口 A4 设置为 DAC 的模拟端口。这就像一个魅力一样。 然后,我为端口 D 上的板载 LED 配置一些输出,并在我的应用程序需要的端口 E 上配置另外两个输出。问题是,直到我(不小心)复制了启用相应外围时钟的行,这些才起作用。我不知道为什么会这样!有什么想法吗?

需要说明的是,代码可以通过任何一种方式编译。只是如果我只启用一次时钟,引脚始终为 0 v,无论我设置还是清除它们。 我还没有测试数字输出后定义的模拟端口和 USART。

void setupGPIO(void){
    static GPIO_InitTypeDef GPIO_InitStruct;

    GPIO_StructInit(&GPIO_InitStruct);
    RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; //BUTTON
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOA,&GPIO_InitStruct);

    GPIO_StructInit(&GPIO_InitStruct);

    //Setup PA4 as Analog
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4; //DAC Output
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    //GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;



    GPIO_Init(GPIOA,&GPIO_InitStruct);
    GPIO_SetBits(GPIOA,GPIO_Pin_4);

    //Setup LED pins as Out

    GPIO_StructInit(&GPIO_InitStruct);
    RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //WHY?!?!?
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14 | GPIO_Pin_13 | GPIO_Pin_12;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
    //GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStruct.GPIO_OType = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOD,&GPIO_InitStruct);

    //PE7 and PE8 as digital Outs
    //PE7 ~HC05 VDD
    //PE8 HC05 AT Mode
    GPIO_StructInit(&GPIO_InitStruct);
    RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);// WHY!?!?!
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
    //GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStruct.GPIO_OType = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOE,&GPIO_InitStruct);

     //Setup PC4 and PC5 Analog PIN
     GPIO_StructInit(&GPIO_InitStruct);
     RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
     GPIO_Init(GPIOC,&GPIO_InitStruct);

     //Setup PD5 and PD6 as USART pins
     GPIO_PinAFConfig(GPIOD,GPIO_PinSource5,GPIO_AF_USART2);
     GPIO_PinAFConfig(GPIOD,GPIO_PinSource6,GPIO_AF_USART2);
     GPIO_StructInit(&GPIO_InitStruct);
     RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
     GPIO_Init(GPIOD,&GPIO_InitStruct);



}

我好像调用了两个不同的函数。

// Enables or disables the Low Speed APB (APB1) peripheral clock.
// notice the APB1 in the function name.
RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

// Enables or disables the AHB1 peripheral clock.
// notice the AHB1 in the function name.
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);