STM32F1 timer (TIM1~TIM8)

1. Introduction to stm32f1 timer

1.1, timer classification

STM32 has a total of 11 timers, 2 advanced control timers TIM1 and TIM8, 4 general-purpose timers TIM2~TIM5, two basic timers TIM6 and TIM7, two watchdog timers and a system tick timer Systick. The clocks of advanced timers TIM1 and TIM8 are generated by APB1, and the clocks of the other six general-purpose timers are generated by APB2
. Their maximum frequency can be configured as the frequency of the system clock.

Timer type number of digits counting mode capture/compare channel Application Scenario
General timer
TIM2~TIM5
16 up, down, two-way 4 Timing counting, PWM, input capture, output compare
Advanced Timers
TIM1 and TIM8
16 up, down, two-way 4 Timing counting, PWM, input capture, output compare
Basic timers
TIM6 and TIM7
16 up, down, two-way 4 Timing count

1.2. Counting mode

The general-purpose timer can count up, count down, and bidirectional count up and down.

  • Up-counting mode: The counter counts from 0 to the auto-reload value (TIMx_ARR), then restarts counting from 0 and generates a counter overflow event.
  • Down counting mode: The counter counts down from the autoloaded value (TIMx_ARR) to 0, then restarts from the autoloaded value, and generates a counter underflow event.
  • Center-aligned mode (up/down counting): The counter starts counting from 0 to the automatically loaded value -1, generates a counter overflow event, then counts down to 1 and generates a counter overflow event; then restarts counting from 0.

Assuming that the initial value of TIMx_ARR we set is 100, then counting up means counting from 0 to 99, and then counting from 0 again; counting down means counting down from 99 to 0, and then counting down from 99 again; counting up/down means counting up from 0 to 99, then counting down from 99 to 0, and then repeating.

2. Use cubemx to configure the timer

2.1, first configure the clock tree

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-NNVJcub5-1684309146749)(https://note.youdao.com/yws/res/30730/WEBRESOURCE757e276116b98f9806d4df930e62ba9d)]

Here it is configured as the highest frequency, 72MHz. The clocks of the advanced timers TIM1 and TIM8 are generated by APB1, and the clocks of the other six general-purpose timers are generated by APB2. So the base frequency of all timers is 72MHz.

2.2, configure the timer

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-Guup2aeN-1684309146750)(https://note.youdao.com/yws/res/30734/WEBRESOURCEf6a0f221526b86eac13416f28546d89f)]

Here we configure the timer TIM3.

  • Prescaler prescaler factor -1, the clock we configured is 72MHz, here is set to 72-1, the obtained timer clock frequency is 1MHz;
  • Counter Mode counting mode, Up means counting up, Down means counting down,
  • Counter Period counting period, the value of the TIMx_ARR register, the number of timers is 16 bits, so the maximum value is 65535

After using cubemx to configure the timer, the timer will not be turned on automatically. You need to call the following function to start the timer in the code:

HAL_TIM_Base_Start(&htim3);

2.3. Realize us delay

void delay_us(uint16_t us)

{
    uint16_t differ=__HAL_TIM_GET_COUNTER(&htim3);
	uint32_t count = differ+us;
    if(differ+us>65535)
	{
		while(__HAL_TIM_GET_COUNTER(&htim3)>(count-65535));
		while(__HAL_TIM_GET_COUNTER(&htim3)<(count-65535));
	}
	else
	{
	    while(differ+us>__HAL_TIM_GET_COUNTER(&htim3))
		{
			
		}
	}
}

Guess you like

Origin blog.csdn.net/weixin_39270987/article/details/130727485