一.GPIO设置
1.1使用RCC开启GPIO的时钟
void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState);//AHB外设时钟
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);//APB1外设时钟
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);//APB2外设时钟
上面是常用的三种时钟控制,设置所使用的情况可以跳转到.c文件里寻找,如下图所示:
/**
* @brief Enables or disables the High Speed APB (APB2) peripheral clock.
* @param RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.
* This parameter can be any combination of the following values:
* @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,
* RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,
* RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,
* RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,
* RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,
* RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17,
* RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11
* @param NewState: new state of the specified peripheral clock.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
下面我将以stm32最小系统板点亮A0口的LED灯为例介绍使用:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//选择GPIOA,使能开启
这样时钟就开启了。
1.2使用GPIO_Init函数初始化GPIO
GPIO_Init第一个选择GPIOA,第二个参数是结构体GPIO_InitTypeDef,重新起名为GPIO_InitStructure,然后依次配置结构体成员。
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//GPIO有八种工作模式,下节讲解。此处用的是推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//选择GPIO外设的0号引脚
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//输出速度
参数配置好后,再输入初始化即可:
GPIO_Init(GPIOA, &GPIO_InitStructure);
1.3使用输出或者输入的函数控制GPIO口
到这一步就可以随心所欲使用函数控制LED灯了。
while (1)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_0);
Delay_ms(500);
GPIO_SetBits(GPIOA, GPIO_Pin_0);
Delay_ms(500);
}
上面就是每隔500ms点亮或熄灭LED灯的函数了。