第一步,首先获取标准外设库,可以从官网进行下载。
https://www.st.com.cn/zh/embedded-software/stm32-standard-peripheral-libraries.html
根据自己的型号选择不同的系列,我这里选择是STM32F1系列

下载最新版本V3.6,点击获取版本后,需要进行登录账号,没有账号的话,可以填写邮箱,ST官方会通过邮件的方式自动发送下载链接,一般点击之后几分钟之内就会收到邮件,邮件里面点击链接进行获取软件。

在标准库模块搭建之前,需要做好下面工作,keill安装不做过多介绍,pack包可以在keill在线安装,也可以通过官网下载安装,标准库第一步已经完成。

下载官方标准固件库之后,得到是是一个zip压缩文件,将文件解压。

然后将en.stsw-stm32054_v3-6-0\STM32F10x_StdPeriph_Lib_V3.6.0\Libraries 文件夹下的内容全部拷贝到新建目录的Firmware 下



找到我们的固件库的下载目录,将en.stsw-stm32054_v3-6-0\STM32F10x_StdPeriph_Lib_V3.6.0\Project\STM32F10x_StdPeriph_Template中的如下文件进行拷贝

将这些文件全部都拷贝到我们的新建目录User文件下

打开 keil,点击最上面的 Project 选项卡,选择 New uVision Project 选项新建一个工程

选择保存路径为我们刚才创建的文件夹下的 Project,文件名为 STM32Template,然后点击保存

现在我们工程创建好了,下一步选择对应芯片型号

确定所需芯片之后,弹出 RTE 的环境配置对话框,选择工程所需的组件,不用配置,点击取消

下一步分组创建,将相应文件添加到分组中

创建完分组左边项目览 如下图所示,Firmare中存放着我们的标准外设文件。根据需要进行添加,

需要注意,不同103的启动文件不一样,根据自己的型号进行选择,我这边是STMF103C6T6,32k内存,属于小容量芯片,选择startup_stm32f10x_ld.s

如何选择可以看这下面两个图片,了解自己的芯片内存大小选择合适的启定文件导入。


导入之后下一步进入工程配置
点击魔法棒,进入配置,在Target中选择V6.21。

output中勾选Browse infomation,勾选之后编译的时候自动生成hex文件。

在C/C++中按照如下图进行配置,include Path为文件路径,根据自己分组进行添加。

在Debug选择自己的烧录方式

在User中增加systick.h和systick.c
- /*!
- \file systick.h
- \brief the header file of systick
- \version 2016-08-15, V1.0.0, firmware for GD32F4xx
- \version 2018-12-12, V2.0.0, firmware for GD32F4xx
- \version 2020-09-30, V2.1.0, firmware for GD32F4xx
- \version 2022-03-09, V3.0.0, firmware for GD32F4xx
- */
-
- /*
- Copyright (c) 2022, GigaDevice Semiconductor Inc.
- Redistribution and use in source and binary forms, with or without modification,
- are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
- 3. Neither the name of the copyright holder nor the names of its contributors
- may be used to endorse or promote products derived from this software without
- specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
- OF SUCH DAMAGE.
- */
-
- #ifndef SYS_TICK_H
- #define SYS_TICK_H
-
- #include
-
- /* configure systick */
- void systick_config(void);
- /* delay a time in milliseconds */
- void delay_1ms(uint32_t count);
- /* delay decrement */
- void delay_decrement(void);
-
- void delay_1us(uint32_t count);
-
- #endif /* SYS_TICK_H */
-
- /*!
- \file systick.c
- \brief the systick configuration file
- \version 2024-01-15, V3.2.0, firmware for GD32F4xx
- */
-
- /*
- Copyright (c) 2024, GigaDevice Semiconductor Inc.
- Redistribution and use in source and binary forms, with or without modification,
- are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
- 3. Neither the name of the copyright holder nor the names of its contributors
- may be used to endorse or promote products derived from this software without
- specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
- OF SUCH DAMAGE.
- */
-
- #include "stm32f10x.h"
- #include "systick.h"
-
- volatile static uint32_t delay;
-
- /*!
- \brief configure systick
- \param[in] none
- \param[out] none
- \retval none
- */
- void systick_config(void)
- {
- /* setup systick timer for 1000000Hz interrupts */
- if(SysTick_Config(SystemCoreClock / 1000000U)) {
- /* capture error */
- while(1) {
- }
- }
- /* configure the systick handler priority */
- NVIC_SetPriority(SysTick_IRQn, 0x00U);
- }
-
- /*!
- \brief delay a time in milliseconds
- \param[in] count: count in milliseconds
- \param[out] none
- \retval none
- */
- void delay_1ms(uint32_t count)
- {
- delay = count*1000;
-
- while(0U != delay) {
- }
- }
-
- //睡眠1us=1/1000ms
- void delay_1us(uint32_t count)
- {
- delay = count;
-
- while(0U != delay) {
- }
- }
-
- /*!
- \brief delay decrement
- \param[in] none
- \param[out] none
- \retval none
- */
- void delay_decrement(void)
- {
- if(0U != delay) {
- delay--;
- }
- }
打开"stm32f10x_it.c"拉到最后,然后删除

打开 main.c 文件,删除一些不必要的代码,加上系统时钟,剩余部分如下:
- #include "stm32f10x.h"
- #include
- #include "systick.h"
-
- int main(void)
- {
- //系统滴答时钟初始化
- systick_config();
-
- while (1)
- {
-
- }
- }
在stm32f10x_it.h中添加头文件systick.h
点亮PC13,每秒进行亮灭闪烁。
- #include "stm32f10x.h"
- #include
- #include "systick.h"
-
-
- //PC13 GPIO初始化
-
- void gpio_config() {
-
- //开启时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //开启GPIOC的时钟
- //使用各个外设前必须开启时钟,否则对外设的操作无效
- /*GPIO初始化*/
- //定义结构体变量
- GPIO_InitTypeDef GPIO_InitStruct;
- //GPIO引脚,赋值为第13号引脚
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
- //GPIO速度,赋值为50MHz
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- //GPIO模式,赋值为推挽输出模式
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
-
- //将赋值后的构体变量传递给GPIO_Init函数
- GPIO_Init(GPIOC,&GPIO_InitStruct);
-
- }
-
- int main(void)
- {
- //系统滴答时钟初始化
- systick_config();
- //GPIO初始化
- gpio_config();
-
- while (1)
- {
- //引脚设置为低电平
- GPIO_ResetBits(GPIOC,GPIO_Pin_13);
- //延时1000ms
- delay_1ms(1000);
- //引脚设置为高电平
- GPIO_SetBits(GPIOC,GPIO_Pin_13);
- //延时1000ms
- delay_1ms(1000);
- }
- }
-
-
1、STM32报错Error: L6218E: Undefined symbol assert_param

.\Objects\STMF103FC6T6.axf: Error: L6218E: Undefined symbol assert_param (referred from stm32f10x_gpio.o).
在C\C++设置一栏中输入USE_STDPERIPH_DRIVER使宏生效即可解决问题


2、../User/stm32f10x_it.c(136): error: call to undeclared function 'delay_decreme'
delay_decreme未定义,需要在stm32f10x_it.h中添加头文件systick.h
- ../User/stm32f10x_it.c(136): error: call to undeclared function 'delay_decrement'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
- 136 | delay_decrement();

- #ifndef __STM32F10x_IT_H
- #define __STM32F10x_IT_H
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- #include "systick.h"
- /* Exported types ------------------------------------------------------------*/
- /* Exported constants --------------------------------------------------------*/
- /* Exported macro ------------------------------------------------------------*/
- /* Exported functions ------------------------------------------------------- */
-
- void NMI_Handler(void);
- void HardFault_Handler(void);
- void MemManage_Handler(void);
- void BusFault_Handler(void);
- void UsageFault_Handler(void);
- void SVC_Handler(void);
- void DebugMon_Handler(void);
- void PendSV_Handler(void);
- void SysTick_Handler(void);
-
- #ifdef __cplusplus
- }
- #endif
-
- #endif /* __STM32F10x_IT_H */
