STM32
使用固件库点亮LED
时间:2023-03-13
1-1.新建工程—库函数版
1-2.
GPIO
输出—使用固件库点亮LED
1-3.
STM32F10x
standard peripheral library1-4.
GPIO
输出—使用固件库点亮LED
1-5.
gitee
代码
$ mkdir Doc Libraries Listings Output Project User
后续手动添加,这里直接关掉。
下载主页:
STM32
标准外设软件库 - 意法半导体STMicroelectronics
STM32F10x
固件库文件分析1-汇编编写的启动文件
startup_stm32f10x_hd.s
:设置堆栈指针、设置PC
指针、初始化中断向量表、配置系统时钟、对用C库函数_main
最终去到C
的世界2-时钟配置文件
system_stm32f10x.c
:把外部时钟HSE=8M
,经过PLL
倍频为72M
。3-外设相关的
stm32f10x.h
:实现了内核之外的外设的寄存器映射
xxx
:GPIO
、USRAT
、I2C
、SPI
、FSMC
stm32f10x_xx.c
:外设的驱动函数库文件
stm32f10x_xx.h
:存放外设的初始化结构体,外设初始化结构体成员的参数列表,外设固件库函数的声明4-内核相关的
CMSIS - Cortex
微控制器软件接口标准
core_cm3.h
:实现了内核里面外设的寄存器映射
core_cm3.c
:内核外设的驱动固件库
NVIC
(嵌套向量中断控制器)、SysTick
(系统滴答定时器)
misc.h
misc.c
5-头文件的配置文件
stm32f10x_conf.h
:头文件的头文件
//stm32f10x_usart.h
//stm32f10x_i2c.h
//stm32f10x_spi.h
//stm32f10x_adc.h
//stm32f10x_fsmc.h
…6-专门存放中断服务函数的C文件
stm32f10x_it.c
stm32f10x_it.h
中断服务函数你可以随意放在其他的地方,并不是一定要放在
stm32f10x_it.c
#include "stm32f10x.h" // 相当于51单片机中的 #include
int main(void) {// 来到这里的时候,系统的时钟已经被配置成72M。 }
[FLY@fly-vm Fwlib-Template]$ tree -d
.
├── Doc
├── Libraries
│ ├── CMSIS
│ │ └── startup
│ └── STM32F10x_StdPeriph_Driver
│ ├── inc
│ └── src
├── Project
│ └── Objects
└── User10 directories
[FLY@fly-vm Fwlib-Template]$ tree
.
├── Doc
│ └── readme.txt
├── Libraries
│ ├── CMSIS
│ │ ├── core_cm3.c
│ │ ├── core_cm3.h
│ │ ├── startup
│ │ │ ├── startup_stm32f10x_cl.s
│ │ │ ├── startup_stm32f10x_hd.s
│ │ │ ├── startup_stm32f10x_hd_vl.s
│ │ │ ├── startup_stm32f10x_ld.s
│ │ │ ├── startup_stm32f10x_ld_vl.s
│ │ │ ├── startup_stm32f10x_md.s
│ │ │ ├── startup_stm32f10x_md_vl.s
│ │ │ └── startup_stm32f10x_xl.s
│ │ ├── stm32f10x.h
│ │ ├── system_stm32f10x.c
│ │ └── system_stm32f10x.h
│ └── STM32F10x_StdPeriph_Driver
│ ├── inc
│ │ ├── misc.h
│ │ ├── stm32f10x_adc.h
│ │ ├── stm32f10x_bkp.h
│ │ ├── stm32f10x_can.h
│ │ ├── stm32f10x_cec.h
│ │ ├── stm32f10x_crc.h
│ │ ├── stm32f10x_dac.h
│ │ ├── stm32f10x_dbgmcu.h
│ │ ├── stm32f10x_dma.h
│ │ ├── stm32f10x_exti.h
│ │ ├── stm32f10x_flash.h
│ │ ├── stm32f10x_fsmc.h
│ │ ├── stm32f10x_gpio.h
│ │ ├── stm32f10x_i2c.h
│ │ ├── stm32f10x_iwdg.h
│ │ ├── stm32f10x_pwr.h
│ │ ├── stm32f10x_rcc.h
│ │ ├── stm32f10x_rtc.h
│ │ ├── stm32f10x_sdio.h
│ │ ├── stm32f10x_spi.h
│ │ ├── stm32f10x_tim.h
│ │ ├── stm32f10x_usart.h
│ │ └── stm32f10x_wwdg.h
│ └── src
│ ├── misc.c
│ ├── stm32f10x_adc.c
│ ├── stm32f10x_bkp.c
│ ├── stm32f10x_can.c
│ ├── stm32f10x_cec.c
│ ├── stm32f10x_crc.c
│ ├── stm32f10x_dac.c
│ ├── stm32f10x_dbgmcu.c
│ ├── stm32f10x_dma.c
│ ├── stm32f10x_exti.c
│ ├── stm32f10x_flash.c
│ ├── stm32f10x_fsmc.c
│ ├── stm32f10x_gpio.c
│ ├── stm32f10x_i2c.c
│ ├── stm32f10x_iwdg.c
│ ├── stm32f10x_pwr.c
│ ├── stm32f10x_rcc.c
│ ├── stm32f10x_rtc.c
│ ├── stm32f10x_sdio.c
│ ├── stm32f10x_spi.c
│ ├── stm32f10x_tim.c
│ ├── stm32f10x_usart.c
│ └── stm32f10x_wwdg.c
├── Project
│ ├── BH-F103.uvoptx
│ ├── BH-F103.uvprojx
│ └── Objects
│ └── BH-F103.sct
└── User├── main.c├── stm32f10x_conf.h├── stm32f10x_it.c└── stm32f10x_it.h10 directories, 67 files
flyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/User (master)
$ vim main.cflyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/User (master)
$ vim stm32f10x_it.cflyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/User (master)
$ cp stm32f10x_it.c stm32f10x_it.hflyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/User (master)
$ vim stm32f10x_it.hflyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/User (master)
$ vim stm32f10x_conf.cflyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/User (master)
$ mv stm32f10x_conf.c stm32f10x_conf.hflyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/User (master)
$ ls
main.c stm32f10x_conf.h stm32f10x_it.c stm32f10x_it.h
flyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED (master)
$ cd Libraries/flyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/Libraries (master)
$ ls
STM32F10x_StdPeriph_Driver/flyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/Libraries (master)
$ mkdir CMSISflyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/Libraries (master)
$ cd CMSIS/flyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/Libraries/CMSIS (master)
$ mkdir startup
在这个选项中添加宏,就相当于我们在文件中使用“
#define
”语句定义宏一样。在编译器中添加宏的好处就是,只要用了这个模版,就不用源文件中修改代码。
STM32F10X_HD
宏:为了告诉STM32
标准库,我们使用的芯片类型是大容量的,使STM32
标准库根据我们选定的芯片型号来配置。
USE_STDPERIPH_DRIVER
宏:为了让stm32f10x.h
包含stm32f10x_conf.h
这个头文件。
- 使能
GPIO
端口时钟;- 初始化
GPIO
目标引脚为推挽输出模式;- 编写简单测试程序,控制
GPIO
引脚输出高、低电平。
flyer@ThinkPad-FLY MINGW64 /d/05_study/STM32/stm32-f103rct6-mini-prj/code/02-FWLib_LED/User/Led (master)
$ ls
bsp_led.c bsp_led.h
stm32-f103rct6-mini-prj\code\02-FWLib_LED\User\Led\bsp_led.h
#ifndef __BSP_LED_H__
#define __BSP_LED_H__#include "stm32f10x.h"#define ON 0
#define OFF 1#define LED1_GPIO_PORT GPIOC
#define LED1_GPIO_CLK RCC_APB2Periph_GPIOC
#define LED1_GPIO_PIN GPIO_Pin_2#define LED2_GPIO_PORT GPIOC
#define LED2_GPIO_CLK RCC_APB2Periph_GPIOC
#define LED2_GPIO_PIN GPIO_Pin_3#define LED1(a) if (a) \GPIO_SetBits(LED1_GPIO_PORT, LED1_GPIO_PIN);\else \GPIO_ResetBits(LED1_GPIO_PORT, LED1_GPIO_PIN)#define LED2(a) if (a) \GPIO_SetBits(LED2_GPIO_PORT, LED2_GPIO_PIN);\else \GPIO_ResetBits(LED2_GPIO_PORT, LED2_GPIO_PIN)extern void LED_GPIO_Config(void);#endif
stm32-f103rct6-mini-prj\code\02-FWLib_LED\User\Led\bsp_led.c
/******************************************************************** > File Name: bsp_led.c* > Create Time: Sun Mar 19 17:29:23 2023******************************************************************/
#include "bsp_led.h"void LED_GPIO_Config(void)
{GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(LED1_GPIO_CLK | LED2_GPIO_CLK, ENABLE);GPIO_InitStructure.GPIO_Pin = LED1_GPIO_PIN;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin = LED2_GPIO_PIN;GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStructure);GPIO_SetBits(LED1_GPIO_PORT, LED1_GPIO_PIN);GPIO_SetBits(LED2_GPIO_PORT, LED2_GPIO_PIN);
}
stm32-f103rct6-mini-prj\code\02-FWLib_LED\User\main.c
/******************************************************************** > File Name: main.c* > Create Time: Sun Mar 19 15:37:45 2023******************************************************************/#include "stm32f10x.h" // �൱��51��Ƭ���е� #include
#include "bsp_led.h"#define SOFT_DELAY Delay(0xFFFFF);void Delay(__IO uint32_t nCount)
{for(;nCount != 0; nCount --);
}int main(void)
{LED_GPIO_Config();while(1){LED1(ON);SOFT_DELAY;LED1(OFF);LED2(ON);SOFT_DELAY;LED2(OFF);}return 0;
}