本文章只讲干货
STM32如果进行点灯操作的话,第一步需要GPIO初始化配置
下面是GPIO初始化的代码:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructrue;
GPIO_InitStructrue.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructrue.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructrue.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructrue);
如果直接C/V的同学,记得看一下引脚配置。如是不是GPIOA,是不是GPIO_Mode_Out_PP,是不是GPIO_Pin_3,对于GPIO_Speed的配置,不做具体要求。
建议以后初始化代码的模块全都放在一个.c和.h文件里面,这样一来方便管理,二来在文件的移植方面也有益处。
进行完GPIO初始化配置之后,这就可以对GPIO的一些函数进行调用了,下面是GPIO常用到的一些函数:
//此函数全都是在stm32f103x_gpio.h里面的
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState);
void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);
下面来到我们的GPIO点灯操作,在下面是main.c函数里的代码:
#include "stm32f10x.h" // Device header
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructrue;
GPIO_InitStructrue.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructrue.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructrue.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructrue);
GPIO_SetBits(GPIOA,GPIO_Pin_3);
while(1)
{
}
}