Question: Delay Function using TIM 2 in Count up Overflow Mode Configure TIM 2 to overflow every 1 ms in count up mode to create your

Delay Function using TIM2 in Count up Overflow Mode
Configure TIM2 to overflow every 1ms in count up mode to create your own HAL delay
function. This is done by enabling TIM2 and setting the prescaler (PSC) and auto-reload
value (ARR). It is recommended to read up on Chapter 22.3.2 of the datasheet to
understand the principles of different counter modes.
The instructions to do so are as follows.
1. Enable TIM2 in the Reset and Clock Control.
2. Set TIM2 in count up mode.
3. Set the Prescaler (PSC) and Auto-Reload Register (ARR) values.
4. Enable TIM2 on the Control Register.
5. Use TIM2 to generate your own HAL delay function.
To set the PSC and ARR values, the following relationship needs to be used.
F_event =(Fclk)/((PSC +1)(ARR +1))
Where the Fclk is the frequency of the system clock defined by the APB Timer Clocks in
CubeIDEs clock configuration. The overflow value is set by the ARR.
The delay function should have a similar format to what is shown. Call this function in
the main while loop and set a delay value (in milliseconds).
void my_delay_ms(uint32_t ms)
{
// Read from TIM2 counter and increment a variable.
// Clear the status register and repeat until the millis_val.
TIM2->SR &= ~(1<<0);
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!