【STM32】STM32F103C6T6尺度外设库
1、尺度外设库获取第一步,起首获取尺度外设库,可以从官网进行下载。
https://www.st.com.cn/zh/embedded-software/stm32-standard-peripheral-libraries.html
根据自己的型号选择差别的系列,我这里选择是STM32F1系列
https://img-blog.csdnimg.cn/img_convert/60339f5cd5ecaaac7ce4f30e2c7e3fa5.png
下载最新版本V3.6,点击获取版本后,必要进行登录账号,没有账号的话,可以填写邮箱,ST官方会通过邮件的方式主动发送下载链接,一般点击之后几分钟之内就会收到邮件,邮件里面点击链接进行获取软件。
https://img-blog.csdnimg.cn/img_convert/25336e12333818dd9cf2f22180c3ec96.png
2、尺度库模板搭建
在尺度库模块搭建之前,必要做好下面工作,keill安装不做过多先容,pack包可以在keill在线安装,也可以通过官网下载安装,尺度库第一步已经完成。
[*]已经安装好 Keil 软件
[*]已经安装好 STM32F1xx 的 Pack 包
[*]已经下载好 STM32F1xx 尺度固件库
2.1、工程文件目次创建
[*]Project:放工程文件,编译文件等。
[*]Firmware:放 ARM 内核文件,尺度外设库文件等。
[*]Hardware:放开发板的硬件驱动文件。
[*]User:放 main 函数,stm32f4xx_it 文件,systick 文件。
[*]Doc: 放 readme.txt 文件,工程说明文件。
https://img-blog.csdnimg.cn/img_convert/f5b2a4733670f380f59e7544098c077e.png
2.2、固件库移植
下载官方尺度固件库之后,得到是是一个zip压缩文件,将文件解压。
https://img-blog.csdnimg.cn/img_convert/df395541ab62eb32b19296a7ab2b89db.png
然后将en.stsw-stm32054_v3-6-0\STM32F10x_StdPeriph_Lib_V3.6.0\Libraries 文件夹下的内容全部拷贝到新建目次的Firmware 下
https://img-blog.csdnimg.cn/img_convert/290cdc93851979b38976da1e93d6c04e.png
https://img-blog.csdnimg.cn/img_convert/95c9d1873926854cb50699f6e83f89a5.png
https://img-blog.csdnimg.cn/img_convert/6990ced98c9d878d1974d65210796162.png
2.3、程序入口移植
找到我们的固件库的下载目次,将en.stsw-stm32054_v3-6-0\STM32F10x_StdPeriph_Lib_V3.6.0\Project\STM32F10x_StdPeriph_Template中的如下文件进行拷贝
https://img-blog.csdnimg.cn/img_convert/33485c9eed86b3699183af7ad7578131.png
将这些文件全部都拷贝到我们的新建目次User文件下
https://img-blog.csdnimg.cn/img_convert/d64195aef0880b5126e33b94409add17.png
2.4、Keil工程相关设置
打开 keil,点击最上面的 Project 选项卡,选择 New uVision Project 选项新建一个工程
https://img-blog.csdnimg.cn/img_convert/cd917ff4c4e98fb60bb0f428bb0afe2c.png
选择保存路径为我们刚才创建的文件夹下的 Project,文件名为 STM32Template,然后点击保存
https://img-blog.csdnimg.cn/img_convert/5b6670cc7fced281d9d72fa7f8a5181e.png
现在我们工程创建好了,下一步选择对应芯片型号
https://img-blog.csdnimg.cn/img_convert/db4bc35825dccb08a1d309130957ca34.png
确定所需芯片之后,弹出 RTE 的环境设置对话框,选择工程所需的组件,不用设置,点击取消
https://img-blog.csdnimg.cn/img_convert/e4af7d8e1fcd4e6d10c4a89a5cac7041.png
下一步分组创建,将相应文件添加到分组中
https://img-blog.csdnimg.cn/img_convert/7274612e38d11c523541484b8ed93dd2.png
创建完分组左边项目览 如下图所示,Firmare中存放着我们的尺度外设文件。根据必要进行添加,
https://img-blog.csdnimg.cn/img_convert/cf97747e032cbfa6b5a3ecc3035e3520.png
必要留意,差别103的启动文件不一样,根据自己的型号进行选择,我这边是STMF103C6T6,32k内存,属于小容量芯片,选择startup_stm32f10x_ld.s
https://img-blog.csdnimg.cn/img_convert/b7ddec9ac61e7bbb5edb485d59e337ef.png
如何选择可以看这下面两个图片,相识自己的芯片内存巨细选择合适的启定文件导入。
https://img-blog.csdnimg.cn/img_convert/1dfcb485909812c29fcb1d1956cf2657.png
https://img-blog.csdnimg.cn/img_convert/7b191395947b8d5ef79d07ff73a72280.png
导入之后下一步进入工程设置
点击魔法棒,进入设置,在Target中选择V6.21。
https://img-blog.csdnimg.cn/img_convert/aa6af83d2dc1a5a029057e74cad147ac.png
output中勾选Browse infomation,勾选之后编译的时候主动生成hex文件。
https://img-blog.csdnimg.cn/img_convert/0c798ef2f65c8d0a2df854757fc7c62f.png
在C/C++中按照如下图进行设置,include Path为文件路径,根据自己分组进行添加。
https://img-blog.csdnimg.cn/img_convert/1979af2b5341f86dcf9dc19eaf3d59ad.png
在Debug选择自己的烧录方式
https://img-blog.csdnimg.cn/img_convert/5dc74ce90fe7c0fed1a3a3f3530f035d.png
2.5、增加延时函数
在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 <stdint.h>
/* 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
\paramnone
\param 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
\paramcount: count in milliseconds
\param 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
\paramnone
\param none
\retval none
*/
void delay_decrement(void)
{
if(0U != delay) {
delay--;
}
}
打开"stm32f10x_it.c"拉到最后,然后删除
https://img-blog.csdnimg.cn/img_convert/75bb61c72380cb092e11e1e5866bf345.png
2.6、程序代码修改
打开 main.c 文件,删除一些不必要的代码,加上系统时钟,剩余部分如下:
#include "stm32f10x.h"
#include <stdio.h>
#include "systick.h"
int main(void)
{
//系统滴答时钟初始化
systick_config();
while (1)
{
}
}
在stm32f10x_it.h中添加头文件systick.h
3、测试程序
点亮PC13,每秒进行亮灭闪烁。
#include "stm32f10x.h"
#include <stdio.h>
#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);
}
}
4、问题与办理
1、STM32报错Error: L6218E: Undefined symbol assert_param
https://img-blog.csdnimg.cn/img_convert/ef90c462616079415c5990fe7b601a52.png
.\Objects\STMF103FC6T6.axf: Error: L6218E: Undefined symbol assert_param (referred from stm32f10x_gpio.o). 在C\C++设置一栏中输入USE_STDPERIPH_DRIVER使宏生效即可办理问题
https://img-blog.csdnimg.cn/img_convert/acb7f97267c72a2da55e991d77e431a2.png
https://img-blog.csdnimg.cn/img_convert/91d455e13431c949cbec879328fa4102.png
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();
https://img-blog.csdnimg.cn/img_convert/e4c9bc4344e6b1b0f43e2f0d22f30c52.png
#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 */
https://img-blog.csdnimg.cn/img_convert/897f69e4954b1df2ae91a05ec52dba20.png
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]