STM32-Basic Timer TIM6-TIM7 Basic Timing Function

1. Introduction to TIM of STM32

STM32 microcontrollers feature a total of 11 timers, which include 2 advanced control timers, 4 general-purpose timers, 2 basic timers, and 2 watchdog timers along with a system timer (SysTick). The SysTick timer was covered in the previous section, while the watchdog timer will be discussed in detail later. Today’s focus is on the remaining 8 timers, excluding the watchdog and system timer.

Timer Counter resolution Counter type Prescaler coefficient Generate DMA request Capture/compare channel Complementary output
TIM1
TIM8
16 bits Up, down, up/down Any number between 1-65536 Yes 4 Yes
TIM2
TIM3
TIM4
TIM5
16 bits Up, down, up/down Any number between 1-65536 Yes 4 No
TIM6
TIM7
16 bits Up Any number between 1-65536 Yes 0 No

TIM1 and TIM8 are advanced timers capable of generating three pairs of PWM complementary outputs, commonly used for driving three-phase motors. Their clock source comes from the APB2 bus. TIM2 to TIM5 are general-purpose timers, while TIM6 and TIM7 are basic timers that operate on the APB1 clock. Since the TIM functionality in STM32 is quite complex, we’ll start by learning the basics—specifically, the timing function of the TIM2–TIM5 general-purpose timers.

2. Basic Timer TIM6-TIM7

2.1 Clock Characteristics

The basic timers TIM6 and TIM7 each contain a 16-bit auto-reload counter driven by a programmable prescaler. They provide a time reference for general-purpose use, particularly for triggering digital-to-analog converters (DACs). These timers are directly connected to the DAC inside the chip and can drive it through a trigger output. TIM6 and TIM7 are independent of each other and do not share any resources.

2.2 Main Features of TIM6-7

The main features of TIM6 and TIM7 include:

  • 16-bit auto-reload counter
  • 16-bit programmable prescaler (values from 1 to 65536)
  • Synchronous circuit for DAC triggering
  • Interrupt/DMA request on update event (counter overflow)

STM32-Basic Timer TIM6-TIM7 Basic Timing Function

2.3 Counter Mode

TIM6 and TIM7 support only the up-counting mode. In this mode, the counter counts from 0 to the value stored in the auto-reload register (TIMx_ARR), then resets to 0 and generates an overflow event.

2.4 Registers of TIM6-TIM7 Basic Timer

1. TIMx_CR1 (Control Register 1)

  • ARPE: Auto-reload preload enable
  • URS: Update request source
  • UDIS: Update disabled
  • CEN: Counter enable

2. TIMx_CR2 (Control Register 2)

3. TIMx_DIER (DMA/Interrupt Enable Register)

  • UDE: Update DMA request enable
  • UIE: Update interrupt enable

4. TIMx_SR (Status Register)

  • UIF: Update interrupt flag

5. TIMx_EGR (Event Generation Register)

  • UG: Generate update event

6. TIMx_CNT (Counter Register)

  • CNT[15:0]: Current counter value

7. TIMx_PSC (Prescaler Register)

  • PSC[15:0]: Prescaler value

8. TIMx_ARR (Auto-reload Register)

  • ARR[15:0]: Auto-reload value

2.5 Programming Steps

  1. Configure priority
  2. Enable clock
  3. Configure GPIO
  4. Configure TIM
  5. Enable counter
  6. Enable interrupt
  7. Clear flag

The specific configuration steps are as follows:

  1. NVIC_Configuration()
  2. RCC_APB2PeriphClockCmd()
  3. GPIO_Init()
  4. TIM_Configuration()
  5. TIM_Cmd(TIM7, ENABLE)
  6. TIM_ITConfig(TIM7, TIM_IT_Update, ENABLE)
  7. TIM_ClearFlag(TIM7, TIM_FLAG_Update)

In step (4), the prescaler coefficient determines the clock frequency used by TIMx. The calculation is: CK_INT / (TIM_Prescaler + 1). CK_INT refers to the internal clock source, which is derived from the APB1 bus. The prescaler value ranges from 0 to 65535.

The clock division setting in step (4) defines the timer clock frequency and the sampling rate for digital filters. The possible values for TIM_ClockDivision are:

TIM_ClockDivision Description Binary Value
TIM_CKD_DIV1 tDTS = Tck_tim 0x00
TIM_CKD_DIV2 tDTS = 2 * Tck_tim 0x01
TIM_CKD_DIV4 tDTS = 4 * Tck_tim 0x10

It is important to disable the preload buffer in step (4). When the preload buffer is disabled, the value written to the auto-reload register (TIMx_ARR) is immediately transferred to the shadow register. If the preload buffer is enabled, the value is updated when an update event occurs.

In ARM processors, some registers have two physical registers: one is the preload register that programmers can write or read, and the other is the shadow register that works internally. This design allows for synchronized updates across multiple channels, ensuring accurate timing operations.

3. Program Source Code

This example uses the TIM7 timer to make LEDs flash every second.

STM32-Basic Timer TIM6-TIM7 Basic Timing Function

/*

* File name: main.c

* Description: Create a project from scratch, trigger interrupt 1 via button 1 to make LED 1 blink, then trigger interrupt preemption 1 via button 2 to make LED 2 blink.

*

* 2 LEDs, corresponding GPIOs: PC3 and PC1

* Output 0 turns on the LED

* Output 1 turns off the LED

* 2 buttons: PB7 and PA11

*/

#include "stm32f10x.h"

/* Delay function */

void Delay(__IO uint32_t nCount)

{

for(;nCount != 0; nCount--);

}

/* GPIO Configuration Function */

void GPIO_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOC, &GPIO_InitStructure);

}

/* Configure Priority */

void NVIC_Configuration(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_Init(&NVIC_InitStructure);

}

/* Timer Initialization */

void TIM_Configuration(void)

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_TimeBaseStructure.TIM_Prescaler = 36000 - 1;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_Period = 2000 - 1;

TIM_TimeBaseInit(TIM7, &TIM_TimeBaseStructure);

TIM_Cmd(TIM7, ENABLE);

TIM_ITConfig(TIM7, TIM_IT_Update, ENABLE);

}

/* Main Function */

int main(void)

{

GPIO_Configuration();

NVIC_Configuration();

TIM_Configuration();

while(1);

}

/* TIM7 Interrupt Handler */

void TIM7_IRQHandler(void)

{

if(TIM_GetITStatus(TIM7, TIM_IT_Update) == SET)

{

GPIOC->ODR ^= GPIO_Pin_3;

TIM_ClearITPendingBit(TIM7, TIM_FLAG_Update);

}

}

Programming Experience:

1. Remember to enable the timer with TIM_Cmd(TIM7, ENABLE), enable the interrupt with TIM_ITConfig(), and enable the clock with RCC_APB1PeriphClockCmd().

2. Always clear the interrupt flag after entering the interrupt using TIM_ClearITPendingBit().

ESD TVS Diode

Esd Tvs Diode,Esd Protection Diode,Esd Suppressor Diode,Usb Esd Protection Diode

Shenzhen Kaixuanye Technology Co., Ltd. , https://www.icoilne.com