????????現(xiàn)在STM32公司主推的是HAL庫的開發(fā),標準庫已經(jīng)不再更新。通過STM32cubeMX的圖形界面生成代碼非常的方便。
一、程序框架的構(gòu)想
1、STM32cubeMX 生成的代碼與添加的應(yīng)用代碼分離;
2、利用 STM32cubeMX 重新生成代碼,不影響應(yīng)用代碼;
3、應(yīng)用代碼的添加,移除與修改,不影響 cube 生成的代碼;
4、代碼架構(gòu)方便閱讀,編輯,修改與移植;
5、代碼架構(gòu)標準化,可以很方便的應(yīng)用到產(chǎn)品開發(fā)中。
二、程序框架的實現(xiàn)
1、新增 MyApplication 文件夾,放置 4 個標準 c 文件,分別是公共文件,回調(diào)文件,系統(tǒng)文件, 用戶初始化文件,后續(xù)應(yīng)用代碼均放在此文件夾;
?2、新增 MyApplication.h 文件,包含所有用戶代碼的頭文件與外設(shè)頭文件,調(diào)整外設(shè)或用戶文件, 只需要調(diào)整此文件內(nèi)的相應(yīng)頭文件即可;
3、main.c 文件標準化。
三 、MyApplication.h
#ifndef __MyApplication_H__
#define __MyApplication_H__
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"
#include "stdio.h"
#include "stdlib.h"
#include "System.h"
#include "Public.h"
#include "MyInit.h"
#endif
/********************************************************
End Of File
********************************************************/
1、此文件放置于 main.c 與應(yīng)用代碼文件中,作為頭文件的集合;
2、更改處理器外設(shè)或應(yīng)用代碼,此文件需要相應(yīng)的增加或刪除相應(yīng)的頭文件。
四 、main.c 文件
1、添加頭文件集合
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "MyApplication.h"
/* USER CODE END Includes */
2、添加用戶初始化函數(shù)
/* USER CODE BEGIN 2 */
MyInit.Peripheral_Set();
/* USER CODE END 2 */
3、標準化主循環(huán)
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
System.Run();
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
4、標準化錯誤處理函數(shù)
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
System.Error_Handler();
/* USER CODE END Error_Handler_Debug */
}
5、標準化斷言失敗處理函數(shù)
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
System.Assert_Failed();
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
五 、system 文件
1、頭文件
//定義結(jié)構(gòu)體類型
typedef struct
{
void (*Run)(void);
void (*Error_Handler)(void);
void (*Assert_Failed)(void);
} System_t;
/* extern variables-----------------------------------------------------------*/
extern System_t System;
/* extern function prototypes-------------------------------------------------*/
主要定義結(jié)構(gòu)體類型 System_t,包含 3 個函數(shù)指針,分別為函數(shù)運行,系統(tǒng)錯誤處理,斷言失敗 處理,被 main.c 文件調(diào)用。
2、源文件
/* Private variables----------------------------------------------------------*/
static void Run(void);
static void Error_Handler(void);
static void Assert_Failed(void);
/* Public variables-----------------------------------------------------------*/
System_t System =
{
Run,
Error_Handler,
Assert_Failed
};
????????主要定義結(jié)構(gòu)體 System 以及 3 個函數(shù),并將 3 個函數(shù)的名稱(首地址)賦值給 System 結(jié)構(gòu)體,完成結(jié)構(gòu)體的初始化。 如此一來,main.c 文件可以通過 System 結(jié)構(gòu)體的函數(shù)指針調(diào)用 System.c 文件的 3 個函數(shù)了。
Run 函數(shù):用戶應(yīng)用代碼;
Error_Hander 函數(shù):系統(tǒng)錯誤處理代碼;
Asset_Failed 函數(shù): 斷言失敗處理代碼
六 、Run 函數(shù)
static void Run()
{
//初始化時,LED1,LED2,LED3均亮燈
//HAL_GPIO_TogglePin -> 取反GPIO輸出狀態(tài)
//HAL_GPIO_WritePin -> 設(shè)置GPIO輸出狀態(tài)
// GPIO_PIN_SET -> 輸出高電平,燈亮
// GPIO_PIN_RESET -> 輸出低電平
//延時500ms,LED1,LED2,LED3均滅燈
HAL_Delay(500);
HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin);
HAL_GPIO_TogglePin(LED3_GPIO_Port,LED3_Pin);
}
作為功能演示,簡單的實現(xiàn)了 LED1 、LED2?、LED3間隔 1s 閃爍。
這樣的全新的程序框架就非常的完美啦,在日后的編寫和程序一直都帶來了極大的便利。文章來源:http://www.zghlxwxcb.cn/news/detail-430285.html
詳細的視頻講解,請關(guān)注B站UP:硬件家園,本博客只是供自己學習,復(fù)習使用。文章來源地址http://www.zghlxwxcb.cn/news/detail-430285.html
到了這里,關(guān)于STM32物聯(lián)網(wǎng)實戰(zhàn)開發(fā)(1)——全新的程序框架的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!