1.直接上代码
#include "stm32f10x.h"
void LedConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定义结构体
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //打开GPIOC的外设时钟
/* 配置结构体并初始化到GPIOC */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //选择需要使用的引脚
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //配置输出频率
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //配置引脚输出模式
GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化结构体
}
int main (void)
{
LedConfig();
GPIO_SetBits(GPIOC,GPIO_Pin_13);
return 0;
}
2.代码解析
GPIO_InitTypeDef GPIO_InitStructure
定义了用于存放IO口初始化的成员变量
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE)
/**
* @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
*/
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
打开GPIOC外设的时钟。第一个参数为需要打开的对应外设时钟,第二参数为打开或者关闭(ENABLE为打开,DISABLE为关闭)。在STM32对于外设的使用里面,每使用到一个外设,都要打开其对应的外设时钟。假如此处使用的IO是GPIOA_PIN13,那么就要将RCC_APB2Periph_GPIOC更改为RCC_APB2Periph_GPIOA。
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13
选择需要使用的引脚,等号右边的值及对应引脚。此参数有一个命名特性,GPIO_Pin_x,x的值可以是0-15,当要使用哪一号引脚只需将 "x" 更改即可。但当需要同时配置多个引脚的时候应该怎么传参,在stm32f10x_gpio.h的93行附近有对应的代码解释。
uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured.
This parameter can be any value of @ref GPIO_pins_define */
代码段中 “This parameter can be any value ”这句话解释了这个参数可以是一些值,所以当需要同时配置多个端口的时候只需将值进行位或操作 “GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_12 ;”
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz
配置引脚的输出频率,等号的值为对应的输出频率,引脚的输出频率影响着引脚对于数据传输速度的快慢,以及影响芯片的功耗。对于此处的数值传参可参考stm32f10x_gpio.h的96行。
GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins.
This parameter can be a value of @ref GPIOSpeed_TypeDef */
代码段中"This parameter can be a value"明确的指出了此处的参数只能是一个值。
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
定义引脚的模式,此处因为引脚的作用为点亮LED灯,所以为输出模式。关于引脚的输出模式有两种定义,一种是推挽输出"GPIO_Mode_Out_PP",一种是开漏输出"GPIO_Mode_Out_OD"。推挽输出与开漏输出的对比就是前者增加了引脚对于电流的输出能力。同样关于此处的此处的数值传参可参考stm32f10x_gpio.h的99行
GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIOMode_TypeDef */
代码段中"This parameter can be a value"明确的指出了此处的参数只能是一个值。
GPIO_Init(GPIOC, &GPIO_InitStructure)
初始化结构体,在上文中直接给结构体传递了参数,然而并没有指定初始化哪一个外设。这个函数的作用可以理解为 写完一封信,但是没有给这封信填写邮寄的地址。
GPIO_ResetBits(GPIOC,GPIO_Pin_13)
上述配置了一大堆内容,都是为了给这个函数服务。在了解这个函数的作用之前首先要知道实体开发板中LED灯是高电平点亮还是低电平点亮,而LED灯的本质是一个二极管。 此处低电平点亮。所以使用到该函数,当为高电平点亮时应该使用 GIO_SetBits(GPIOC,GPIO_Pin_13)。
关于该函数的传参,第一个参数为LED灯所在的外设端口组,此处为GPIOC,第二参数为对应的外设引脚。
3.花样点灯
3.1闪烁的LED灯
此处因为尚未用到定时器计时,所以使用软件计时。
#include "stm32f10x.h"
#include "led.h"
void ledDelay(uint16_t time)
{
uint16_t i = 0;
while(time--)
{
i = 10000;
while(i--);
}
}
void FlikerLed(uint16_t Flikerms)
{
GPIO_ResetBits(GPIOC,GPIO_Pin_13); //LED灯亮
ledDelay(Flikerms);
GPIO_SetBits(GPIOC,GPIO_Pin_13); //LED灯灭
ledDelay(Flikerms);
}
int main (void)
{
LedConfig();
while(1)
{
FlikerLed(1000); //亮灭1S
}
}