STM32学习笔记1——LED流水灯
学习视频:B站野火STM32F103教学视频
1固件库认识
1-汇编编写的启动文件
路径:…\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\startup\arm
包含启动文件如下:
启动文件 | 区别 |
---|---|
starup_stm32f10x_ld.s | ld:low-density小容量,闪存容量16-32K |
starup_stm32f10x_md.s | md:medium-density中容量,闪存容量64-128K |
starup_stm32f10x_hd.s | hd:high-density大容量,闪存容量256-512K |
starup_stm32f10x_xl.s | xl:超大容量,闪存容量512-1024K |
starup_stm32f10x_cl.s | cl:·connectivity line devices互联型,特指STM32F105XX和STM32F107XX系列 |
starup_stm32f10x_ld_vl.s | |
starup_stm32f10x_md_vl.s | vl:value line devices超值型系列,特指 STM32F100XX |
starup_stm32f10x_hd_vl.s |
开发板单片机型号为STM32F103VET6,闪存512K,选择starup_stm32f10x_hd.s。
作用:设置堆栈指针、PC指针、初始化中断向量表、配置系统时钟、从汇编语言转到C语言。
2-时钟配置文件
system_stm32f10x.c
把高速外部时钟HSE=8MHz,经PLL(锁相环倍频输出)配置系统时钟为72MHz。
3-外设相关
stm32f10x.h:实现内核外的外设寄存器映射,给每个寄存器地址命名,方便调用;
stm32f10x_xx.c:外设的驱动函数库文件,xx为外设名,内有实现各外设功能的函数与函数用法;
stm32f10x_xx.h:存放外设的初始化结构体,即外设所处的位置信息(地址),以及结构体成员列表,外设固件库函数的声明;
4-内核相关(暂不清楚)
core_cm3.h:实现了内核里外设的寄存器映射
core_cm3.c
实现CMSIS - Cortex 微控制器软件接口标准
ST公司对ARM内核进行裁剪(?)
NVIC(嵌套向量中断控制器)、SysTick(系统滴答定时器)
misc.h
misc.c
5-头文件的配置文件
stm32f10x_conf.h:头文件的头文件(暂不清楚)
管理stm32f10x_xx.h等头文件,一般不需要写出
6-存放中断服务函数的C文件(暂不清楚)
stm32f10x_it.c
stm32f10x_it.h
管理中断服务函数,但不规定中断服务函数的位置。
2流水灯代码
main.c
#include "stm32f10x.h"
#include "bsp_led.h"
void Delay( uint32_t count ) //软件延迟
{
for(; count!=0; count-- );
}
int main(void)
{
LED_GPIO_Config();
//初始化后引脚全部置0,RGB全亮,故需把它们关闭
LED_R(OFF);
Delay(0xFFFFF);
LED_G(OFF);
Delay(0xFFFFF);
LED_B(OFF);
Delay(0xFFFFF);
//流水灯循环
while(1)
{
//红
LED_R(ON);
Delay(0xFFFFFF);
LED_R(OFF);
//绿
LED_G(ON);
Delay(0xFFFFFF);
LED_G(OFF);
//蓝
LED_B(ON);
Delay(0xFFFFFF);
LED_B(OFF);
//黄
LED_R(ON);
LED_G(ON);
Delay(0xFFFFFF);
LED_R(OFF);
LED_G(OFF);
//淡蓝
LED_G(ON);
LED_B(ON);
Delay(0xFFFFFF);
LED_G(OFF);
LED_B(OFF);
//紫
LED_R(ON);
LED_B(ON);
Delay(0xFFFFFF);
LED_R(OFF);
LED_B(OFF);
//白
LED_R(ON);
LED_G(ON);
LED_B(ON);
Delay(0xFFFFFF);
//关闭
LED_R(OFF);
LED_G(OFF);
LED_B(OFF);
Delay(0xFFFFFF);
}
}
bsp_led.h
#ifndef __BSP_LED_H
#define __BSP_LED_H
#include "stm32f10x.h"
#define LED_GPIO_PORT GPIOB //GPIOB外设宏定义
#define LED_GPIO_CLK RCC_APB2Periph_GPIOB //外设GPIOB在总线APB2上,GPIOB外设时钟使能位宏定义
#define LED_R_GPIO_PIN GPIO_Pin_5 //红灯引脚PB5
#define LED_G_GPIO_PIN GPIO_Pin_0 //绿灯引脚PB0
#define LED_B_GPIO_PIN GPIO_Pin_1 //蓝灯引脚PB1
//控制RGB灯的亮灭
#define ON 1
#define OFF 0
#define LED_R(a) if(a) \
GPIO_ResetBits(LED_GPIO_PORT , LED_R_GPIO_PIN); \
else \
GPIO_SetBits(LED_GPIO_PORT , LED_R_GPIO_PIN);
#define LED_G(a) if(a) \
GPIO_ResetBits(LED_GPIO_PORT , LED_G_GPIO_PIN); \
else \
GPIO_SetBits(LED_GPIO_PORT , LED_G_GPIO_PIN);
#define LED_B(a) if(a) \
GPIO_ResetBits(LED_GPIO_PORT , LED_B_GPIO_PIN); \
else \
GPIO_SetBits(LED_GPIO_PORT , LED_B_GPIO_PIN);
void LED_GPIO_Config(void);
#endif /* __BSP_LED_H */
bsp_led.c
//bsp: board support package 板级支持包(针对开发板)
#include "bsp_led.h" //记得在Option for Target中指定头文件路径
void LED_GPIO_Config(void)
{
//定义GPIO_InitStruct为GPIO_InitTypeDef结构体类型,以便下面用GPIO配置的存放
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(LED_GPIO_CLK, ENABLE); //APB2外设时钟使能库函数,打开GPIOB时钟使能
GPIO_InitStruct.GPIO_Pin = LED_R_GPIO_PIN | LED_G_GPIO_PIN | LED_B_GPIO_PIN; //选择控制的GPIO引脚,使用宏定义
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct); //调用库函数初始化GPIOB,&表示取结构体的地址
}
bsp_led.h与bsp_led.c是用户根据需要编写的代码,本例中用于led灯的控制。