Stm32

  
 

Before giving the full program, it is for the convenience of beginners. After putting all the functions used by the main function into the main function, add the function declaration before the main function. This will make the main.c file have no hierarchy. All functions dilute the function code in main.c.


The file organization in a general project is like this: the main.c file only contains code that reflects the functionality.

The main.c header file contains only two

#include "stm32f0xx.h"#include "hw_config.h"


The following are user-defined macros. These macros are also closely related to the function. To put it bluntly, it is used by the main function. If it is not directly related to the function, it can be defined in hw_config.h, such as the definition of GPIO pins.

#define OE GPIO_Pin_5


The following is the definition of the global variable, which is also defined here in the main function.



The following is the main function, its structure is

void main(void){ //defining the local variables used by the main function //Initialize global variables and local variables //initialization hardware, such as RCC, NVIC, GPIO, TIM, USART, Systick, IWWDG, etc. //The following is the implementation of the function code, usually while (1) structure. while (1) { }}


The main.c file contains these codes. The functions called by the main function, such as RCC_Configuration, NVIC_Configuration, GPIO_Configuration, SysTick_Configuration, USART1_Configuration, etc., are declared in hw_config.h, in hw_config Defined in .c.

Note: Because all functions called by the main function are defined in hw_config.c, some variables are bound to be defined in hw_config.c, and these variables are also used in the main.c file. These variables need to be extern declared in hw_config.h. So even if stm32f0xx_it.c uses these variables defined in hw_config.c, you only need to include the hw_config.h header file.

Take a count variable usart1_invalidframe_time as an example, defined in hw_config.c, declared with extern in hw_config.h, cleared in main.c (usart1 receives data to clear the variable), in stm32f0xx_it The .s SysTick_Handler function is added every 1 ms.


Copyright © Windows knowledge All Rights Reserved