STM32Timer + basic timer

1. Basic overview of timers

1. Principle of software timer

It turns out that when we use the 51 microcontroller, we use a __nop()__ to delay

The delay function we use through software is inaccurate and subject to many uncertain factors.

2. Timer principle: ratio between counts

Because the use of software delay is affected, wetry to use hardware for timing. The simultaneous execution efficiency of hardware is also higher than that of software.

3.STM32 timer classification

4.STM32 timer characteristics table

be careful:

1) The basic timer is an up counter (incrementing)

2)The prescaler coefficient is from 1-65536 (but the actual stored value range is 0-65535)–“So there is a problem of + 1 and -1

5.The overall difference between STM32 basic, general and advanced timer functions

2. Basic timer (TIM6/TIM7)

1.Basic concepts

“Trigger DAC” —” refers to the conversion between data circuits and analog circuits

2. Basic timer block diagram

1. Notes

1) The clock source of the basic timer is: internal clock

2) The auto-reload register and the prescalerhave corresponding shadow registers

3) Determine whether the auto-reload register (ARR) and the prescaler (PSC) immediately update the value (with or without buffering) to the shadow register depends on the ARPE bit

With buffering–“Reduce time error

No buffering—“There is a large amount of time error

4) The value update in the shadow register must also be triggered by an event

5) What actually works is: shadow register, which cannot be accessed directly

6) The overflow condition is CNT==ARR shadow register, not ARR itself

7) When overflow occurs, two results will be generated: interrupt and DMA output interrupt event

2. Internal clock frequency problem

1) The final clock frequency of the timer depends not only on the external clock frequency but also on whether frequency division is performed.

Give an example

4. Master mode triggers DAC function

Map the update event to TRGO through the main mode, and then TRGO will directly trigger the DAC, so that the timer does not need to trigger the DAC conversion through interrupts

3.STM32 timer counting mode and overflow conditions

Instance description of incremental counting mode

Explanation of down-counting mode examples

Explanation of center alignment mode

3. Timer register

1.TIM6 and TIM7 control register 1 (TIMx_CR1)

This register is used to set whether the ARR register has buffering and enable/disable the counter

The difference between whether there is buffering: application scenarios

1. Suppose we now let the LED light up for 1 second, then turn off, and then light for 2 seconds. Assume that the corresponding ARR for 1 second is 99, and the corresponding ARR for 2 seconds is 199.

a) There is no buffering at this time. It will light up for 1 second and then turn off. Then when it starts to light up again, we need to modify the value of CNT to 199. This modification process takes time

b) If there is buffering at this time, we can modify ARR=199 during this period of 1 second. Because there is buffering, it will not take effect until the next cycle.

2. Suppose we now let the LED light up for 1 second, then turn off, and then light for 1 second. Assume that the corresponding ARR for 1 second is 99

a) There is no buffering at this time. It will light up for 1 second and then turn off. Then when it starts to light up again, we do not need to modify it at this time, so no time is needed.

b) If there is buffering at this time, it will light up for 1 second, and then when it starts to light up again, we do not need to modify it at this time, so no time is needed.

2.TIM6 and TIM7 DMA/interrupt enable register (TIMx_DIER)

Used to enable update interrupt

3. TIM6 and TIM7 status register (TIMx_SR)

Used to determine whether an update interruption occurs, set to 1 by hardware and cleared by software

4. TIM6 and TIM7 counters (TIMx_CNT)

The counter is readable and writable, and can be modified duringrunning.

5. TIM6 and TIM7 prescaler (TIMx_PSC)

6. TIM6 and TIM7 automatic reload register (TIMx_ARR)

4. Calculation method of timer overflow time

1) Here ARR + 1 can be understood like this. For example, if the value of ARR is 2, then the counter will go from 0 to 1, and from 1 to 2. At this time, it will not overflow immediately, but will overflow after another clock pulse. So the actual overflow time is three clock pulses, that isARR + 1 clock pulse

2) The value in ARR starts from zero

Derivation process

Overflow time = count number * f

For example: we have a 16-bit counter, and then set the values of the prescaler and auto-reload register to 65536

Then the divided frequency T =72 000 000/65536 = 1,098.6328125

f (representing the number that can be counted in 1s)=1/T=1/1,098.6328125=9.1022222222222222222222222222222e-4

Overflow time = f * value of automatic reload register (indicating the time required to calculate all register values) = 9.102222*65536 = 59.65s [Maximum overflow time]

5. Timer interrupt configuration steps

1. Specific steps

Note:

1) Remember to turn on the counter

2) Remember to enable interrupts (timers, NVIC)

3) Write an interrupt service function

4) Set up NVIC, clock and other basics

5) Set basic parameters related to timer

6) Timer overflow interrupt interrupt callback function

1) Timer initialization–》HAL_TIM_Base_Init

2) Set the interrupt priority and enable the interrupt—》HAL_NVIC_SetPriority & amp; & amp; HAL_NVIC_EnableIRQ

3) Write an interrupt service function—“Go to the start.s file to find the corresponding name [TIM6_IRQHandler] and then call [HAL_TIM_IRQHandler] in this function (this is hal_tim. c)】

3) Write the timer’s Callback function–“This function is called in the interrupt handler–“HAL_TIM_PeriodElapsedCallback

4) Start timer (counter) and interrupt—》HAL_TIM_Base_Start_IT

2. Related HAL library functions

1.HAL_TIM_OC_Init: timer initialization

In stm32f1xx_hal_tim.c

HAL_StatusTypeDef HAL_TIM_OC_Init(TIM_HandleTypeDef *htim)
{
  /* Check the TIM handle allocation */
  if (htim == NULL)
  {
    return HAL_ERROR;
  }

  /* Check the parameters */
  assert_param(IS_TIM_INSTANCE(htim->Instance));
  assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));

  if (htim->State == HAL_TIM_STATE_RESET)
  {
    /* Allocate lock resource and initialize it */
    htim->Lock = HAL_UNLOCKED;

    /* Init the low level hardware: GPIO, CLOCK, NVIC and DMA */
    HAL_TIM_OC_MspInit(htim);

  }

  /* Set the TIM state */
  htim->State = HAL_TIM_STATE_BUSY;

  /* Init the base time for the Output Compare */
  TIM_Base_SetConfig(htim->Instance, & amp;htim->Init);

  /* Initialize the TIM state*/
  htim->State = HAL_TIM_STATE_READY;

  return HAL_OK;
}

Introduction to key structures

2.HAL_TIM_Base_MspInit

The role of this function: MSP refers to the initialization related to MCU

3.HAL_TIM_Base_Start

Start counter

4.HAL_TIM_Base_Start_IT

Start timer interrupt

5. TIM6_IRQHandler

1) This function name is found in start.s according to the type of timer used.

2) Call “HAL_TIM_IRQHandler” in this function

6.HAL_TIM_PeriodElapsedCallback

Write a timer overflow callback function. This is written by the user according to relevant requirements

3. Interrupt service function VS interrupt callback function

What is the difference between interrupts in the HAL library being handled in callback functions and interrupt functions (amobbs.com Amo Electronics Forum – Dongguan Amo Electronics Website)

Interrupt service function: TIM6_IRQHandler—>This actually does not write business code, but just provides an interrupt entry

Interrupt callback function: HAL_TIM_PeriodElapsedCallback—>Relevant business code is written in this callback function, the real processing

  1. Interrupt service function (ISR): The interrupt service function is a function triggered by hardware and executed by the operating system or embedded system. When an interrupt event (for example, timer overflow, external trigger, etc.) occurs, the CPU will jump to the corresponding interrupt service function to perform related operations. In this context, an ISR is system-level and responds to underlying hardware events.

  2. Interrupt callback function: An interrupt callback function is usually a user- or application-level defined function that executes in the context of an interrupt service function. In some cases, the interrupt service function may call a user-defined callback function. This callback function is defined by the user and is used to handle interrupt events and execute interrupt-related application logic. In this context, callback functions are application-level and are used to customize behavior when an interrupt occurs.

6. Practical Programming: Timer Interrupt Experiment

STM32CubeMX study notes (5) – basic timer interface usage_counter mode mx-CSDN blog

1. Conditional analysis

2. Calculation of relevant frequency and overflow time

3. Code writing

Operation code

TIM_HandleTypeDef g_timx_handle;

//Timer interrupt initialization function
void btim_timx_int_init(uint16_t arr,uint16_t psc){
\t
g_timx_handle.Instance=TIM6;
g_timx_handle.Init.Prescaler=psc;//Frequency division parameters
g_timx_handle.Init.Period=arr;//preload value
\t
//Basic parameters for timer initialization
HAL_TIM_Base_Init( & amp;g_timx_handle);
\t
//Open the interrupt of timer 6
HAL_TIM_Base_Start_IT( & amp;g_timx_handle);
\t
}
//Timer basic MSP initialization function (that is, initializing NVIC, CLOCK, etc.)
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim){
\t
//Determine whether the address is timer 6
if(htim->Instance==TIM6){
//Open the clock of timer 6
__HAL_RCC_TIM6_CLK_ENABLE();
\t\t
//Initialize NVIC
HAL_NVIC_SetPriority(TIM6_IRQn,1,3);//Set priority
HAL_NVIC_EnableIRQ(TIM6_IRQn);//Open timer 6 interrupt
\t\t
}
\t
}

//Write interrupt service function
//Go and search in the start.s file
//Code function: In fact, this function provides an entrance to the timer interrupt
//Does not perform business processing
void TIM6_IRQHandler(){
\t
HAL_TIM_IRQHandler( & amp;g_timx_handle);
}

//Write timer overflow callback function
//Code function: write user-related business code
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
//Determine whether the address is timer 6
if(htim->Instance==TIM6){
//Flip the LED to make it flash
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7);
}
}

main function

int main(){
    HAL_Init();
    sys_stm32_clock_init(RCC_PLL_MUL9);
    delay_init();
    btim_timx_int_init(5000-1,7200-1);
    while(1){
    
    }

}

Universal timer

syntaxbug.com © 2021 All Rights Reserved.