Functional Description

Up-Counting Mode

The timer is a 16-bit counter with an associated auto-reload register. Both the counter and the auto-reload register can be read and written by software. Read and write operations can be performed even when the counter is running.

The time base unit includes:

  • Counter register ( TIMx_CNT )

  • Auto-reload register ( TIMx_ARR )

  • Prescaler register ( TIMx_PSC )

The timer operates in upcounting mode, where the counter counts from 0 to the auto-reload value (the content of the TIMx_ARR register). When the counter reaches the auto-reload value, a counter overflow event occurs.

Pulse Width Measurement

When the CC0PM bit of the TIMx_CCR0 register is set to 0, the capture timer operates in capture mode 0. In this mode, the width of the TRGI active level can be measured. When TRGI transitions from inactive level to active level, the counter is automatically enabled. When TRGI transitions from active level to inactive level, the counter is automatically disabled, CC0IF is set, and the current count value is copied to the CCR0 field of the TIMx_CCR0 register.

The figure below shows an example of the pulse width measurement mode, where the prescaler division factor is 1 and the rising edge of TRGI is used for capture.

../../../_images/statistic_pulse_width_mode_diagram_positive_edge_of_trgi_is_active_for_capture.svg

Pulse Count Measurement

When the CC0PM bit of the TIMx_CCR0 register is set to 1, the capture timer operates in capture mode 1. In this mode, the number of valid edges of TRGI within a given period can be counted. When the counter overflows, CC0IF is set, and the count is copied to the CCR0 field of the TIMx_CCR0 register.

The diagram below shows an example of the pulse counting mode, where the prescaler divides by 1, the rising edge of TRGI is used for capture, and the ARR field is equal to E6.

../../../_images/statistic_pulse_number_mode_diagram_positive_edge_of_trgi_is_active_for_capture_arr_e6.svg

Interrupt Events

There are two types of interrupt events for the capture timer:

  • Interrupt events in up-counting mode can be enabled by configuring the UIE bit of the TIMx_DIER register. When the UIE bit is 1, if an interrupt event occurs, it triggers a system interrupt. Both hardware overflow and software-triggered updates generate interrupts. The corresponding interrupt status can be checked through UIF in TIMx_SR , and the corresponding interrupt can be cleared by writing to UIF.

  • Interrupt events in capture mode can be enabled by configuring the CC0IE bit of the TIMx_DIER register. Capture mode 0: When TRGI transitions from active level to inactive level, an interrupt event is triggered, and the current count value is copied to the CCR0 field of the TIMx_CCR0 register ; Capture mode 1: When the counter overflows, an interrupt event is triggered, and the value of the counted pulse number is copied to the CCR0 field of the TIMx_CCR0 register.