There are three kinds of timers in STM32F103 single chip microcomputer, including advanced timer, general timer and basic timer.
the advanced control timer (TIM1 and TIM8) consists of a 16 bit automatic loading counter driven by a programmable prescaler. It is suitable for a variety of purposes, including measuring the pulse width of the input signal (input capture), or generating the output waveform (output comparison, PWM, complementary PWM embedded in dead time, etc.). Using timer prescaler and RCC clock to control prescaler, pulse width and waveform period can be adjusted from a few microseconds to a few milliseconds. Advanced control timers (TIM1 and TIM8) and general timer (TIMx) are completely independent and do not share any resources. They can operate synchronously.
The functions of TIM1 and TIM8 timers include:
- 16 bit up, down, up / down auto load counter
- 16 bit programmable (can be modified in real time) prescaler. The frequency division coefficient of counter clock frequency is any value between 1 and 65535
- Up to 4 independent channels:
- Input capture
- Output comparison
- PWM generation (edge or middle alignment mode)
- Single pulse mode output
- Dead time programmable complementary output
- Use external signals to control the timer and the synchronization circuit interconnected with the timer
- Allows the repetition counter of the timer register to be updated after a specified number of counter cycles
- The brake input signal can set the timer output signal to the reset state or a known state
- Interrupt / DMA occurs when the following events occur:
- Update: counter up overflow / down overflow, counter initialization (triggered by software or internal / external)
- Trigger event (counter start, stop, initialization or counting by internal / external trigger)
- Input capture
- Output comparison
- Brake signal input
- Support incremental (quadrature) encoder and Hall sensor circuits for positioning
- The trigger input is used as an external clock or periodic current management
the main part of the programmable advanced control timer is a 16 bit counter and its associated automatic loading register. This count
The counter can count up, count down, or count up and down in both directions. The counter clock is divided by the prescaler. The counter, auto load register and prescaler register can be read and written by software, and the reading and writing is still valid even if the counter is still running.
the initialization method of advanced timer is as follows:
void TIMER1_Init(u16 arr, u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); TIM_DeInit(TIM1); 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_TimeBaseStructure.TIM_RepetitionCounter = 0x00; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); TIM_ClearFlag(TIM1, TIM_FLAG_Update); TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //Allow update interrupt NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; //TIM1 update interrupt NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_Cmd(TIM1, ENABLE); } void TIM1_UP_IRQHandler(void) { if(TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM1, TIM_IT_Update); LED0 = !LED0; } }
timer 1 is used here. Two parameters are passed during initialization, in which arr is used to set the cycle of the timer and PSC is used to set the frequency division coefficient of the clock. The clock frequency of the default timer is 72MHz, so after these two parameters are brought in, the cycle calculation formula of the timer is:
(arr+1)*(psc+1)/72MHz, set the counting mode of the timer to count up. Next, you need to set the value of the repetition timer. TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x00; This is a counter unique to the advanced timer. The default setting is 0, which means that the timer will always cycle and count and will not stop. If it is set to a certain data, the timer will stop running after a certain number of times. If the timer needs to start the interrupt function, it is also necessary to set the NVIC register to set the interrupt priority of the timer.
int main(void) { delay_init(); //Delay function initialization NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); LED_Init(); KEY_Init(); TIMER1_Init(9,71); //50Khz 10us Tout=(9+1)*(71+1)/72M=10us while(1) { } }
initialize the timer in the main program. Here, set the timer cycle value to 9, the frequency division value to 71, and the calculated timer cycle to 10us. In the interrupt function, turn the LED light upside down. You can see through the oscilloscope that the LED level will change every 10us.