我从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);
}