m_<:整个程序除去基本配置外由两部分组成,main.c与bsp_led.c。其中,bsp_led.c及.h负责外设LED寄存器的配置,使其能正常工作。main.c则是在LED配置完能正常操作后来实现LED怎么亮灭。
1 main.c分析
#include "stm32f4xx.h" //包含了所有外设的地址映射
#include "bsp_led.h"
void Delay(uint32_t count)
{
for( ; count!=0; count--);
}
int main(void)
{
LED_GPIO_Config(); //核心,bsp_led.c中配置LED
while(1)
{
GPIO_ResetBits(GPIOF, GPIO_Pin_6); //复位置0灯亮
Delay(0xffffff); //函数粗延时,约0.5s(挖个坑,找个时间说明一些机器指令周期)
GPIO_SetBits(GPIOF, GPIO_Pin_6); //置位置1灯灭
Delay(0xffffff);
}
}
2.1 bsp_led.h
#ifndef _BSP_LED_H //防止重定义
#define _BSP_LED_H
#include "stm32f4xx.h"
void LED_GPIO_Config(void); //声明配置函数
#endif
2.2 bsp_led.c
//bsp: board support package(板级支持包)
#include "bsp_led.h"
void LED_GPIO_Config(void)
{
//以下四个步骤适合所有外设的初始化
/* 第一步:开GPIO的时钟 */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); //GPIOF在AHB1总线上,另外注意,系统时钟已经自己设定了
/* 第二步:定义一个GPIO初始化结构体 */
GPIO_InitTypeDef GPIO_InitStruct;
/* 第三步:配置GPIO初始化结构体的成员 */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6; //Pin6红色、Pin7绿色、Pin8蓝色
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //输出模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStruct.GPIO_Speed = GPIO_Low_Speed; //2MHz
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉电阻
/* 第四步:调用GPIO初始化函数,把配置好的结构体的成员的参数写入寄存器 */
GPIO_Init(GPIOF, &GPIO_InitStruct); //将上述参数赋给GPIOF
GPIO_ResetBits(GPIOF,GPIO_Pin_6); //输出置0,点亮红色LED
}
网友评论