STM32-Peripheral’s Timers: Base Timer Interrupt Mode

STM32-Peripheral’s Timers: Base Timer Interrupt Mode

Introduction

Figure 2: Timer Table

The advanced-control timers (TIM1&TIM8) consist of a 16-bit auto-reload counter driven by
a programmable prescaler ,

The general-purpose timers (TIM2 to TIM5) consist of a 16-bit or 32-bit auto-reload counter driven by a
programmable prescaler

The TIM9 to TIM14 general-purpose timers consist of a 16-bit auto-reload counter driven by
a programmable prescaler

It may be used for a variety of purposes, including measuring the pulse lengths of input signals (input capture) or generating output waveforms (output compare, PWM,
complementary PWM with dead-time insertion). Pulse lengths and waveform periods can be modulated from a few microseconds to several milliseconds using the timer prescaler and the RCC clock controller prescalers
The basic timers TIM6 and TIM7 consist of a 16-bit auto-reload counter driven by a
programmable prescaler. They may be used as generic timers for time-base generation but they are also specifically used to drive the digital-to-analog converter (DAC). In fact, the timers are internally
connected to the DAC and are able to drive it through their trigger outputs.
The timers are completely independent, and do not share any resources

TIMER Generic features
The Timer features include:
1. 16-bit up, down, up/down auto-reload counter.
2. 16-bit programmable prescaler allowing dividing (also on the fly) the counter clock frequency either by any
factor between 1 and 65536.
3. Up to 4 independent channels for:
– Input Capture
– Output Compare
– PWM generation (Edge and Center-aligned Mode)
– One-pulse mode output
4. Synchronization circuit to control the timer with external signals and to interconnect several timers together.
5. Supports incremental encoder for positioning purposes

How to use this driver
1. Initialize the TIM low level resources by implementing the following functions depending on the selected
feature:
– Time Base : HAL_TIM_Base_MspInit()
– Input Capture : HAL_TIM_IC_MspInit()
– Output Compare : HAL_TIM_OC_MspInit()
– PWM generation : HAL_TIM_PWM_MspInit()
– One-pulse mode output : HAL_TIM_OnePulse_MspInit()
– Encoder mode output : HAL_TIM_Encoder_MspInit()
2. Initialize the TIM low level resources :
a. Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
b. TIM pins configuration
◦ Enable the clock for the TIM GPIOs using the following function:
__HAL_RCC_GPIOx_CLK_ENABLE();
◦ Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
3. The external Clock can be configured, if needed (the default clock is the internal clock from the APBx), using the following function: HAL_TIM_ConfigClockSource, the clock configuration should be done before any start function.

4. Configure the TIM in the desired functioning mode using one of the Initialization function of this driver:
– HAL_TIM_Base_Init: to use the Timer to generate a simple time base
– HAL_TIM_OC_Init and HAL_TIM_OC_ConfigChannel: to use the Timer to generate an Output
Compare signal.
– HAL_TIM_PWM_Init and HAL_TIM_PWM_ConfigChannel: to use the Timer to generate a PWM signal.
– HAL_TIM_IC_Init and HAL_TIM_IC_ConfigChannel: to use the Timer to measure an external signal.
– HAL_TIM_OnePulse_Init and HAL_TIM_OnePulse_ConfigChannel: to use the Timer in One Pulse
Mode.
– HAL_TIM_Encoder_Init: to use the Timer Encoder Interface.
5. Activate the TIM peripheral using one of the start functions depending from the feature used:
– Time Base : HAL_TIM_Base_Start(), HAL_TIM_Base_Start_DMA(), HAL_TIM_Base_Start_IT()
– Input Capture : HAL_TIM_IC_Start(), HAL_TIM_IC_Start_DMA(), HAL_TIM_IC_Start_IT()
– Output Compare : HAL_TIM_OC_Start(), HAL_TIM_OC_Start_DMA(), HAL_TIM_OC_Start_IT()
– PWM generation : HAL_TIM_PWM_Start(), HAL_TIM_PWM_Start_DMA(), HAL_TIM_PWM_Start_IT()
– One-pulse mode output : HAL_TIM_OnePulse_Start(), HAL_TIM_OnePulse_Start_IT()
– Encoder mode output : HAL_TIM_Encoder_Start(), HAL_TIM_Encoder_Start_DMA(),
HAL_TIM_Encoder_Start_IT().
6. The DMA Burst is managed with the two following functions: HAL_TIM_DMABurst_WriteStart()
HAL_TIM_DMABurst_ReadStart()

For this blog we are going to use Base Timer in Interrupt mode

Creating STM32 executable projects steps are available on this link , please follow steps 1 to 10 as per blog, here we will start from step 11. PA9 & PA10 Pin used for debugging purpose as UART Tx & UART Rx respectively. this blog is specially to demonstrate base timer in interrupt mode, output will be available in serial terminal.


11. Clock configuration

System clock: 16 MHz , APB2 timer & peripheral clock 16 MHz, APB1 timer clock 16 MHz & APB1 peripheral clock 8 MHz

Figure 3: Clock Configuration

12. Timer Configuration

Go to System mode > Timers > TIM2 > Clock Source > Internal Clock

Figure 4: TIM2 Enable Internal clock


13. Parameter settings >

Prescaler : 1024 & Counter period : 15625

APB1 clock : 16MHz, Interrupt time interval after calculation : 1 Second

Figure 5: TIM2 parameter settings

14. Enable TIM2 Interrupt

Figure 6: TIM2 Enable Interrupt


15. Press below icon for code generation

Figure 7Cube IDE Code Generation

16. Code Snippets

for demonstration inside interrupt call back function we are printing RTC time

uint8_t aShowTime[50] = {0};
uint8_t aShowDate[50] = {0};
/* USER CODE BEGIN PV */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if ( htim->Instance == TIM2)
	{
		  RTC_CalendarShow(aShowTime, aShowDate);
		  printf(GRN"aShowTime %s\n",aShowTime);
	}
}

int main(void)
{
  MX_RTC_Init();
  MX_TIM2_Init();
  HAL_TIM_Base_Start_IT(&htim2);

    while (1)
    {
      /* USER CODE END WHILE */

      /* USER CODE BEGIN 3 */
    }
}

static void RTC_CalendarShow(uint8_t *showtime, uint8_t *showdate)
{
  RTC_DateTypeDef sdatestructureget;
  RTC_TimeTypeDef stimestructureget;

  /* Get the RTC current Time */
  HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
  /* Get the RTC current Date */
  HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
  /* Display time Format : hh:mm:ss */
  sprintf((char *)showtime, "%2d:%2d:%2d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
  /* Display date Format : mm-dd-yy */
  sprintf((char *)showdate, "%2d-%2d-%2d", sdatestructureget.Month, sdatestructureget.Date, 2000 + sdatestructureget.Year);
}

17. Tera term output

Figure 8 Output at tera term
Software Tools:
  1. STM32CubeIDE
  2. STM32CubeMx
  3. Teraterm
Hardware Setup:
  1. STM32F429IDISCOVERY board
  2. Mini USB Cable
  3. Jumper wire
Conclusion:

Successfully demonstrated Base timer functionality in interrupt mode.

If you enjoyed this article, share your feedback.

References:
  1. STM32 HAL Library
  2. STM32 UM1718 document
Similar topics:
  1. https://kalapiinfotech.in/stm32-cube-ide-freertos-code-generation-using-cubemx/
  2. STM32 Peripherals: GPIO
  3. STM32 Peripherals: DAC
  4. STM32 Peripherals: ADC using Polling Mode
  5. STM32 Peripherals: UART Interrupt Mode
  6. STM32-Peripheral’s-SPI: Polling Mode



Leave a Reply

Your email address will not be published. Required fields are marked *