- The last:【STM32Cube_10】 Use ADC to read gas sensor data (MQ-2)
For a better reading experience, please go:Mculover666’s personal blog.
This article describes how to configure STM32L431RCT6’s universal timer peripherals with STM32CubeMX to make the LED blink in interrupt mode.
1. Preparation
Hardware preparation
- Development board First needs to prepare a development board, here I prepared the development board of STM32L4 (BearPi) :
- To test the LED
Here I directly use the onboard LED, the schematic diagram is as follows:
Software to prepare
- Keil-mdk and the corresponding chip package need to be installed in order to compile and download the generated code;
Keil MDK and Serial Port Utility installation packages are available at the end of this article.
2. Generate MDK projects
Select the chip model
Open STM32CubeMX, open MCU selector:
Search for and select the chipSTM32L431RCT6
:
Configuring the Clock Source
- If you choose to use an external high speed clock (HSE), you need to configure RCC in the System Core;
- If you use the default internal clock (HSI), you can skip this step.
Here I use the external clock:
GPIO pin for LED
Check out the schematic of the Little Bear development board as follows:
So next we choose to configure PC13 pins:
The universal timer TIM2 is configured
Knowledge small card – STM32L431 timer
STM32L431xx series has one advanced timer (TIM1), three universal timers (TIM2, TIM15, TIM16), two basic timers (TIM6, TIM7), and two low-power timers (LPTIM1, LPTIM2).
STM32L431 universal TIMx (TIM2, TIM15, TIM16) timer functions include:
-
16 bit (TIM15,TIM16)/32 bit (TIM2) up, down, up/down automatic loading counter, note: TIM15,TIM16 only support up (increasing) counting mode;
-
16 bit programmable (can be modified in real time) predivider, the counter clock frequency of the frequency of the division coefficient between 1 and 65535 any value.
-
4 independent channels (TIMx_CH1~4, where TIM15 has a maximum of 2 channels and TIM16 has a maximum of 1 channel). These channels can be used as:
- The input capture
- The output is
- PWM generation (Edge or middle alignment mode)
- Single pulse mode output
-
An external signal can be used to control the timer and the timer interconnect synchronization circuit.
-
Interrupt /DMA occurs when:
- Update: Counter overflow up/down, counter initialization (via software or internal/external trigger)
- Trigger event (counter starts, stops, initializes, or internally/externally triggers count)
- The input capture
- The output is
The end of the little knowledge card ~
Configure timer TIM2
First chooseTIM2
, clock source select internal clock:
Next is the parameter setting of TIM2, referring to the RCC clock tree in the data manual. The internal clock source of TIM2 isPCLK1 = 80Mhz
, our goal is to generate 2 interrupts per second, so the predivision coefficient is set to40000-1
, the automatic overload value is1000-1
, the timer update interrupt frequency obtained is80000000/40000/1000=2Hz
:
Leave the rest of the Settings as default, and finally turn on TIM2 interrupt:
Configuring the Clock Tree
The maximum main frequency of STM32L4 is 80M, so PLL is configured and finally enabledHCLK = 80Mhz
You can:
Build project Settings
Code generation Settings
Finally set to generate a separate initialization file:
The generated code
Click on theGENERATE CODE
Mdk-v5 project can be generated:
3. Write, compile and download user code in MDK
Write the interrupt callback function
Stm32l4xx_it. C generates the following interrupt handler, TIM2_IRQHandler for all interrupts:
The HAL_TIM_IRQHandler(&htim2) code is automatically generated in the interrupt handler, which will automatically call the corresponding function based on the interrupt event. Here we need to handle the update interrupt event. The callback function is defined by __weak by default, so in tim.c we will redefine the callback function. When adding a function to this function, it is necessary to determine which timer is calling because the callback function is shared by all timers:
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance == htim2.Instance)
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
}
Copy the code
Start the timer and enable the interrupt
Finally, turn on TIM2 and enable mid-break in main (after TIM2 initializes the code, before while) :
HAL_TIM_Base_Start_IT(&htim2);
Copy the code
The test results
After compiling and downloading, you can see that the LED flashes at a frequency of 2 Hz.
Now that we have learned how to use the universal timer to flash the LED, the next section describes how to use the universal timer to generate PWM to drive the buzzer.
- The last:【STM32Cube_10】 Use ADC to read gas sensor data (MQ-2)
- Next up:[STM32Cube_12] Use universal timer to generate PWM drive buzzer.
For more exciting articles and resources, please pay attention to my wechat official number: “McUlover666”.