Zero base HAL library – GPIO lighting
3.1 introduction
We have a general understanding of Cubemx software interface, know how to create projects and use steps, next we officially enter the journey of using Hal library development, this is a thrilling moment, why? You will feel the advantage of using Hal library, the pleasure of developing more efficiently, come on let’s light the lights first.
preparation
- STM32 various types of boards (I use F103Rc and F407ZE).
- CubeMx, Keil_IDE.
3.2 LED flow light
Let’s first take a look at the schematic diagram of the board corresponding to the water lamp:
The common cathode connection LED lamp, only when the pin output high level, LED positive guide light.
Now that we know the circuitry of the board hardware, let’s look at the software configuration.
1. New Project:
After searching or filtering chips, click Start Project to successfully create a Project
2. Configure the clock
Click RCC to enter the clock configuration and configure the high-speed clock as external crystal oscillator. The software automatically configures the pins of two crystal oscillators as shown in PD0 and PD1
Note the pin color:
- Green: The function is configured and the mode is selected
- Yellow: The function is configured but the mode is not configured
- Gray: Not configured
- Off-white: Pins are not available
3. GPIO pin configuration
- ① Find the corresponding pin PC14(PC15) of the water lamp and set it as output.
- ② Find the mode corresponding to the GPIO module setting
- ③ The overview page of the pins configured for the current project
- ④ Specific configuration of GPIO mode (including initialization output level, output mode, pull-down mode, pin speed, pin function user label)
4. Specific configuration of GPIO mode
- ① The current pin is PC15
- (2) Initialize the output level. Since our LED lamp is common cathode connection method, the lamp will light up when the output is high
- ③ Output mode (push pull, open leakage output), here use the default push pull output.
- ④ Whether to pull pins up and down (pull up, pull down), use the default.
- (5) pin speed (low, medium, high speed), water lamp has no special requirements for pin speed, low speed can.
- ⑥ User tags, after initialization, corresponding pins and ports will have corresponding macro definitions generated in main.h.
5. Download mode configuration (must be configured)
- DAP-Link
- JTAG
According to their own use of download (mode) for the corresponding configuration, I am using DAP so configured as Serial Wire, if not configured after a download, the next program may not enter, pay attention to be configured.
6. Clock configuration
Here, we adopt the simplest, most convenient and fastest configuration mode, as shown in the picture below:
Click Clock Configuration, enter 72 in the corresponding ② (the maximum frequency of F407 is 168, and the maximum frequency can be configured for different chips), and press Enter.
The clock is successfully configured in the following format:
7. Project management configuration and code generation
In the familiar Cubemx software framework has been carefully talked about this part of the content, we need to configure the project name, project save location, IDE configuration
After selecting the following two options, click the GNEERATE CODE button in the upper right corner to generate the CODE.
When writing large projects, modular programming and file size constraints are often required, and in general, choosing these two options can greatly optimize programming. Optimized code size and code readability.
8. Write business code
Open the main.c file, we can see the following interface, when we use Cubemx and Keil development, all user code is written in
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
Copy the code
Between such comments, and often in different sections of the code.
API
Let’s look at the API functions corresponding to GPIO module:
/* HAL_GPIO_Init: cubemx automatically calls HAL_GPIO_DeInit after generating code: */ void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init); */ void HAL_GPIO_Init(GPIO_TypeDef *GPIO_Init); void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin); HAL_GPIO_ReadPin: reads pin level HAL_GPIO_WritePin: writes pin level HAL_GPIO_TogglePin: writes pin level HAL_GPIO_TogglePin: Switch pin level HAL_GPIO_LockPin: so the current pin level will not change HAL_GPIO_EXTI_IRQHandler: */ GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin); void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState); void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin); HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin); void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin); void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin);Copy the code
The main () function
We can see the user tags defined earlier in main.h as follows:
#define LED1_Pin GPIO_PIN_14
#define LED1_GPIO_Port GPIOC
#define LED2_Pin GPIO_PIN_15
#define LED2_GPIO_Port GPIOC
Copy the code
Here is the mian() function:
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * / / * reset all of the peripherals, and initialize the Flash interface and tick timer * / HAL_Init (); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* The Clock configured in the Clock Configuration is generated for this function */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ * USER CODE END SysInit */ /* Initializes all peripherals, GPIO peripherals initializes */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { HAL_Delay(500); HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin); HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }Copy the code
So, once you’ve generated the code, Cubemx has all the necessary initialization functions in place, all you have to do is use it, write the corresponding code into the corresponding code area, download the code into the board, and we can see that the board lights have started flashing at half a second each time.
Well, running water lamp is so simple to end, how is not feeling very simple ah!