Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

STM32F103 MCU timer is divided into three kinds, advanced timer, general timer, basic timer.

The universal timer is a 16-bit auto-loading counter unit driven by a programmable predivider. It is suitable for a variety of applications, including measuring the pulse length of the input signal (input capture) or generating the output waveform (output comparison and PWM). Using timer predividers and RCC clock controller predividers, the pulse length and waveform period can be adjusted from a few microseconds to a few milliseconds. Each timer is completely independent and does not share any resources with each other. They can operate synchronously together.

Universal TIMx (TIM2, TIM3, TIM4 and TIM5) timer features include:

  • 16 bit up, down, up/down automatic loading counters
  • 16-bit programmable (can be modified in real time) predivider, the frequency division coefficient of the counter clock frequency is any value between 1 and 65536
  • 4 independent channels:
    • The input capture
    • The output is
    • PWM generation (Edge or middle alignment mode)
    • Single pulse mode output
  • Synchronous circuits that use external signals to control timers and interconnect timers
  • 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
  • Supports incremental (orthogonal) encoders and Hall sensor circuits for positioning
  • Trigger input as external clock or current management on a periodic basis

The main part of the programmable universal timer is a 16 – bit counter and its associated auto – loading registers. This counter can count up, down, or up and down both ways. The counter clock is divided by a predivider. Counters, auto-load registers, and predivider registers can be read and written by software and can still be read and written while the counter is running.

void TIMER3_Init(u16 arr, u16 psc)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
	
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    TIM_TimeBaseStructure.TIM_Period = arr;
    TIM_TimeBaseStructure.TIM_Prescaler = psc;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
	
    TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);	// Allow update interrupts
	
    NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x02;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x02;
    NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
    NVIC_Init(&NVIC_InitStructure);
	
    TIM_Cmd(TIM3,ENABLE);
}
void TIM3_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM3,TIM_IT_Update)! =RESET) { TIM_ClearITPendingBit(TIM3,TIM_IT_Update); LED0=! LED0; }}Copy the code

Timer 3 is used here, and two parameters are passed during initialization, where ARR is used to set the timer period and PSC is used to set the clock’s frequency division coefficient. The default clock frequency of the timer is 72MHz. After setting these two parameters, the timer period is calculated as (arr+1) x (PSC +1)/72MHz. Set the timer counting mode to upward. The interrupt function of timer is enabled here, so the NVIC register needs to be set, that is, the interrupt priority of timer needs to be set. After the timer initialization is complete, you also need to provide a timer interrupt function. When the timer timer time expires, it will automatically jump to the timer interrupt function.

int main(void)
{
    u8 key = 0;
    delay_init();       // Delay function initialization
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    LED_Init();
    KEY_Init();  
    TIMER3_Init(9.71);		 //50Khz 10us
    while(1) {}}Copy the code

Initialize the timer in the main function, set the timer frequency to 71, the default crystal frequency is 72MHz, the frequency value after 71+1 frequency is 1Mhz, then set the timer period value to 9+1, so the timer timing frequency is 1Mhz/(9+1)=100KHz, the timing period is 10us. By observing the level of the LED pin through an oscilloscope, you can see that the level of the LED pin will flip every 10us.