硬件准备(STM32F407ZGT6)
1.初始准备
1.1打开Template模板,在工程目录下新建HARDWARE文件夹
1.2 新建在HARDWARE路径中新建led.c , led.h两个文件,在工程中新建HARDWARE Groups,并将led.c添加进去,将led.h添加进头文件目录中。
1.3硬件连接
2.程序编写
2.1 库函数
头文件:stm32f4xx_gpio.h
源文件:stm32f4xx_gpio.c
1个初始化函数:
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
作用:
初始化一个或者多个IO口(同一组)的工作模式,输出类型,速度以及上下拉方式。也就是一组IO口的4个配置寄存器。 (GPIOx->MODER, GPIOx->OSPEEDR,GPIOx->OTYPER,GPIOx->PUPDR)
typedef struct
{
uint32_t GPIO_Pin//指定要初始化的端口
GPIOMode_TypeDef GPIO_Mode;//端口模式
GPIOSpeed_TypeDef GPIO_Speed;//速度
GPIOOType_TypeDef GPIO_OType; //输出类型
GPIOPuPd_TypeDef GPIO_PuPd;//上拉或者下拉 }
GPIO_InitTypeDef;
(这里设置成推挽输出,上拉,速度100MHZ,使用PIN_9,PIN_10两个端口)
样例:(可以一次初始化一个IO组下的多个IO,前提是这些IO口的配置方式一样。)
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//使能GPIOF时钟
//GPIOF9,F10初始化设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;//LED0和LED1对应IO口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化GPIOF9,F10
2个读取输入电平函数:
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
作用:读取某个GPIO的输入电平。实际操作的是GPIOx_IDR寄存器。
例如: GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5);//读取GPIOA.5的输入电平
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
作用:读取某组GPIO的输入电平。实际操作的是GPIOx_IDR寄存器。
例如: GPIO_ReadInputData(GPIOA);//读取GPIOA组中所有io口输入电平
2个读取输出电平函数:
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
作用:读取某个GPIO的输出电平。实际操作的是GPIO_ODR寄存器。
例如: GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_5);//读取GPIOA.5的输出电平
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
作用:读取某组GPIO的输出电平。实际操作的是GPIO_ODR寄存器。
例如: GPIO_ReadOutputData(GPIOA);//读取GPIOA组中所有io口输出电平
4个设置输出电平函数:
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
作用:设置某个IO口输出为高电平(1)。实际操作BSRRL寄存器
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
作用:设置某个IO口输出为低电平(0)。实际操作的BSRRH寄存器。
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);(这俩个函数不常用)
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
使能IO口时钟。调用函数RCC_AHB1PeriphClockCmd();
不同的外设调用的时钟使能函数可能不一样 初始化IO口模式。
调用函数GPIO_Init(); 操作IO口,输出高低电平。
GPIO_SetBits(); GPIO_ResetBits();
3.函数编写(基于库函数)
led.c文件初始化函数
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);
}
main函数
int main(void)
{
delay_init(168);
LED_Init();
while(1)
{
GPIO_SetBits(GPIOF,GPIO_Pin_9);
GPIO_ResetBits(GPIOF,GPIO_Pin_10);
delay_ms(1000);
GPIO_SetBits(GPIOF,GPIO_Pin_9);
GPIO_ResetBits(GPIOF,GPIO_Pin_10);
delay_ms(100);
}
}
4.烧入芯片试运行
效果:led灯以500ms为间隔闪烁