做公司網站 找誰做網絡營銷主要學什么
1、標準外設庫獲取
第一步,首先獲取標準外設庫,可以從官網進行下載。
https://www.st.com.cn/zh/embedded-software/stm32-standard-peripheral-libraries.html
根據自己的型號選擇不同的系列,我這里選擇是STM32F1系列
下載最新版本V3.6,點擊獲取版本后,需要進行登錄賬號,沒有賬號的話,可以填寫郵箱,ST官方會通過郵件的方式自動發(fā)送下載鏈接,一般點擊之后幾分鐘之內就會收到郵件,郵件里面點擊鏈接進行獲取軟件。
2、標準庫模板搭建
在標準庫模塊搭建之前,需要做好下面工作,keill安裝不做過多介紹,pack包可以在keill在線安裝,也可以通過官網下載安裝,標準庫第一步已經完成。
- 已經安裝好 Keil 軟件
- 已經安裝好 STM32F1xx 的 Pack 包
- 已經下載好 STM32F1xx 標準固件庫
2.1、工程文件目錄創(chuàng)建
- Project:放工程文件,編譯文件等。
- Firmware:放 ARM 內核文件,標準外設庫文件等。
- Hardware:放開發(fā)板的硬件驅動文件。
- User:放 main 函數,stm32f4xx_it 文件,systick 文件。
- Doc: 放 readme.txt 文件,工程說明文件。
2.2、固件庫移植
下載官方標準固件庫之后,得到是是一個zip壓縮文件,將文件解壓。
然后將en.stsw-stm32054_v3-6-0\STM32F10x_StdPeriph_Lib_V3.6.0\Libraries
文件夾下的內容全部拷貝到新建目錄的Firmware 下
2.3、程序入口移植
找到我們的固件庫的下載目錄,將en.stsw-stm32054_v3-6-0\STM32F10x_StdPeriph_Lib_V3.6.0\Project\STM32F10x_StdPeriph_Template
中的如下文件進行拷貝
將這些文件全部都拷貝到我們的新建目錄User文件下
2.4、Keil工程相關配置
打開 keil,點擊最上面的 Project 選項卡,選擇 New uVision Project 選項新建一個工程
選擇保存路徑為我們剛才創(chuàng)建的文件夾下的 Project,文件名為 STM32Template,然后點擊保存
現在我們工程創(chuàng)建好了,下一步選擇對應芯片型號
確定所需芯片之后,彈出 RTE 的環(huán)境配置對話框,選擇工程所需的組件,不用配置,點擊取消
下一步分組創(chuàng)建,將相應文件添加到分組中
創(chuàng)建完分組左邊項目覽 如下圖所示,Firmare中存放著我們的標準外設文件。根據需要進行添加,
需要注意,不同103的啟動文件不一樣,根據自己的型號進行選擇,我這邊是STMF103C6T6,32k內存,屬于小容量芯片,選擇startup_stm32f10x_ld.s
如何選擇可以看這下面兩個圖片,了解自己的芯片內存大小選擇合適的啟定文件導入。
導入之后下一步進入工程配置
點擊魔法棒,進入配置,在Target中選擇V6.21。
output中勾選Browse infomation,勾選之后編譯的時候自動生成hex文件。
在C/C++中按照如下圖進行配置,include Path為文件路徑,根據自己分組進行添加。
在Debug選擇自己的燒錄方式
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, thislist 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 documentationand/or other materials provided with the distribution.3. Neither the name of the copyright holder nor the names of its contributorsmay be used to endorse or promote products derived from this software withoutspecific 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, thislist 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 documentationand/or other materials provided with the distribution.3. Neither the name of the copyright holder nor the names of its contributorsmay be used to endorse or promote products derived from this software withoutspecific 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"拉到最后,然后刪除
2.6、程序代碼修改
打開 main.c 文件,刪除一些不必要的代碼,加上系統(tǒng)時鐘,剩余部分如下:
#include "stm32f10x.h"
#include <stdio.h>
#include "systick.h"int main(void)
{
//系統(tǒng)滴答時鐘初始化
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速度,賦值為50MHzGPIO_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)
{//系統(tǒng)滴答時鐘初始化systick_config();//GPIO初始化gpio_config();while (1){//引腳設置為低電平GPIO_ResetBits(GPIOC,GPIO_Pin_13);//延時1000msdelay_1ms(1000);//引腳設置為高電平GPIO_SetBits(GPIOC,GPIO_Pin_13);//延時1000msdelay_1ms(1000);}
}
4、問題與解決
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 __cplusplusextern "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 */