四种同步方式:

The TIMx Timers can be synchronized with an external trigger in several modes: Reset mode, Gated mode and Trigger mode.


1 Slave mode: Reset mode

The counter and its prescaler can be reinitialized in response to an event on a trigger input. Moreover, if the URS bit from the TIMx_CR1 register is low, an update event UEV is generated. Then all the preloaded registers (TIMx_ARR, TIMx_CCRx) are updated.

Bit 2 URS: Update request source

This bit is set and cleared by software to select the UEV event sources.

0: Any of the following events generate an update interrupt or DMA request if enabled.

These events can be:

– Counter overflow/underflow

– Setting the UG bit

– Update generation through the slave mode controller3

1: Only counter overflow/underflow generates an update interrupt or DMA request if enabled.


In the following example, the upcounter is cleared in response to a rising edge on TI1 input:

• Configure the channel 1 to detect rising edges on TI1. Configure the input filter duration(设定上升沿触发)

(in this example, we don’t need any filter, so we keep IC1F(input capture channel filter 1 )=0000). The capture prescaler is not used for triggering, so the user does not need to configure it. The CC1S bits select the input capture source only, CC1S = 01 in the TIMx_CCMR1 register.

捕捉比较模式控制:

Bits 1:0 CC1S: Capture/Compare 1 selection

This bit-field defines the direction of the channel (input/output) as well as the used input.

00: CC1 channel is configured as output.

01: CC1 channel is configured as input, IC1 is mapped on TI1.(Input Capture channel 1 Mapping to TI1)

然后,这里有一个通道选择表,可以选定相关的定时器接入:

10: CC1 channel is configured as input, IC1 is mapped on TI2.

11: CC1 channel is configured as input, IC1 is mapped on TRC. This mode is working only if

an internal trigger input is selected through TS bit (TIMx_SMCR register)

Note: CC1S bits are writable only when the channel is OFF (CC1E = 0 in TIMx_CCER).

Bits 7:4 IC1F: Input capture 1 filter

This bit-field defines the frequency used to sample TI1 input and the length of the digital filter

applied to TI1. The digital filter is made of an event counter in which N consecutive events

are needed to validate a transition on the output:

0000: No filter, sampling is done at fDTS

0001: fSAMPLING=fCK_INT, N=2

0010: fSAMPLING=fCK_INT, N=4

0011: fSAMPLING=fCK_INT, N=8

0100: fSAMPLING=fDTS/2, N=6

0101: fSAMPLING=fDTS/2, N=8

0110: fSAMPLING=fDTS/4, N=6

0111: fSAMPLING=fDTS/4, N=8

1000: fSAMPLING=fDTS/8, N=6

1001: fSAMPLING=fDTS/8, N=8

1010: fSAMPLING=fDTS/16, N=5

1011: fSAMPLING=fDTS/16, N=6

1100: fSAMPLING=fDTS/16, N=8

1101: fSAMPLING=fDTS/32, N=5

1110: fSAMPLING=fDTS/32, N=6

1111: fSAMPLING=fDTS/32, N=8


Write CC1P=0 in TIMx_CCER register to validate the polarity (and detect rising edges only).

捕捉比较使能控制

Bit 1 CC1P: Capture/Compare 1 output polarity

CC1 channel configured as output:

0: OC1 active high.

1: OC1 active low.

CC1 channel configured as input:

This bit selects whether IC1 or IC1 is used for trigger or capture operations.

0: non-inverted: capture is done on a rising edge of IC1. When used as external trigger, IC1 is non-inverted.

1: inverted: capture is done on a falling edge of IC1. When used as external trigger, IC1 is inverted.

• Configure the timer in reset mode by writing SMS=100 in TIMx_SMCR register. Select TI1 as the input source by writing TS=101 in TIMx_SMCR register.

定时器主从模式控制

Bits 2:0 SMS: Slave mode selection

When external signals are selected the active edge of the trigger signal (TRGI) is linked to the polarity selected on the external input (see Input Control register and Control Register description.

000: Slave mode disabled - if CEN = ‘1 then the prescaler is clocked directly by the internal clock.

001: Encoder mode 1 - Counter counts up/down on TI2FP1 edge depending on TI1FP2 level.

010: Encoder mode 2 - Counter counts up/down on TI1FP2 edge depending on TI2FP1 level.

011: Encoder mode 3 - Counter counts up/down on both TI1FP1 and TI2FP2 edges depending on the level of the other input.

100: Reset Mode - Rising edge of the selected trigger input (TRGI) reinitializes the counter and generates an update of the registers.

输入边沿触发,重置模式,也就是会重置计数器并产生一个更新事件

101: Gated Mode - The counter clock is enabled when the trigger input (TRGI) is high. The counter stops (but is not reset) as soon as the trigger becomes low. Both start and stop of the counter are controlled.

110: Trigger Mode - The counter starts at a rising edge of the trigger TRGI (but it is not reset). Only the start of the counter is controlled.

111: External Clock Mode 1 - Rising edges of the selected trigger (TRGI) clock the counter.

Note: The gated mode must not be used if TI1F_ED is selected as the trigger input (TS=100). Indeed, TI1F_ED outputs 1 pulse for each transition on TI1F, whereas the gated mode checks the level of the trigger signal. The clock of the slave timer must be enabled prior to receiving events from the master timer, and must not be changed on-the-fly while triggers are received from the master timer.

• Start the counter by writing CEN=1 in the TIMx_CR1 register.

The counter starts counting on the internal clock, then behaves normally until TI1 rising edge. When TI1 rises, the counter is cleared and restarts from 0. In the meantime, the trigger flag is set (TIF bit in the TIMx_SR register) and an interrupt request, or a DMA request can be sent if enabled (depending on the TIE and TDE bits in TIMx_DIER register).

定时器状态寄存器

TIF: Trigger interrupt flag

This flag is set by hardware on trigger event (active edge detected on TRGI input when the slave mode controller is enabled in all modes but gated mode, both edges in case gated mode is selected). It is cleared by software.

0: No trigger event occurred.

1: Trigger interrupt pending.

Figure 136 shows this behavior when the auto-reload register TIMx_ARR=0x36. The delay between the rising edge on TI1 and the actual reset of the counter is due to the resynchronization circuit on TI1 input.

注意上升沿触发到计数器重置,还是有一定延迟,这里ST说的比较含糊,也许

是没有做

小结:这个触发模式,其实就是定时器的计数可以由外部的事件进行重置,同时给出重置的中断条件。这里理解限制,就是所谓重置是硬件自动重置,而重置的仅仅是计数器的当前计数值。 


2 Slave mode: Gated mode

The counter can be enabled depending on the level of a selected input. In the following example, the upcounter counts only when TI1 input is low:

• Configure the channel 1 to detect low levels on TI1. Configure the input filter duration

(in this example, we don’t need any filter, so we keep IC1F=0000). The capture prescaler is not used for triggering, so the user does not need to configure it. The CC1S bits select the input capture source only, CC1S=01 in TIMx_CCMR1 register. Write CC1P=1 in IMx_CCER register to validate the polarity (and detect low level only).


捕捉比较模式控制:

Bits 1:0 CC1S: Capture/Compare 1 selection

This bit-field defines the direction of the channel (input/output) as well as the used input.

00: CC1 channel is configured as output.

01: CC1 channel is configured as input, IC1 is mapped on TI1.(Input Capture channel 1 Mapping to TI1)

捕捉比较使能控制

Bit 1 CC1P: Capture/Compare 1 output polarity

CC1 channel configured as input:

This bit selects whether IC1 or IC1 is used for trigger or capture operations.

0: non-inverted: capture is done on a rising edge of IC1. When used as external trigger, IC1 is non-inverted.

1: inverted: capture is done on a falling edge of IC1. When used as external trigger, IC1 is inverted.


• Configure the timer in gated mode by writing SMS=101 in TIMx_SMCR register. Select TI1 as the input source by writing TS=101 in TIMx_SMCR register.

定时器主从模式控制

Bits 2:0 SMS: Slave mode selection

When external signals are selected the active edge of the trigger signal (TRGI) is linked to the polarity selected on the external input (see Input Control register and Control Register description.

000: Slave mode disabled - if CEN = ‘1 then the prescaler is clocked directly by the internal clock.

001: Encoder mode 1 - Counter counts up/down on TI2FP1 edge depending on TI1FP2 level.

010: Encoder mode 2 - Counter counts up/down on TI1FP2 edge depending on TI2FP1 level.

011: Encoder mode 3 - Counter counts up/down on both TI1FP1 and TI2FP2 edges depending on the level of the other input.

100: Reset Mode - Rising edge of the selected trigger input (TRGI) reinitializes the counter and generates an update of the registers.

输入边沿触发,重置模式,也就是会重置计数器并产生一个更新事件

101: Gated Mode - The counter clock is enabled when the trigger input (TRGI) is high. The counter stops (but is not reset) as soon as the trigger becomes low. Both start and stop of the counter are controlled.

110: Trigger Mode - The counter starts at a rising edge of the trigger TRGI (but it is not reset). Only the start of the counter is controlled.

111: External Clock Mode 1 - Rising edges of the selected trigger (TRGI) clock the counter.

• Enable the counter by writing CEN=1 in the TIMx_CR1 register (in gated mode, the counter doesn’t start if CEN=0, whatever is the trigger input level).


The counter starts counting on the internal clock as long as TI1 is low(由于CC1P已经反转了IC1的输出,那么,TI1的高电平时候为低电平,反之亦然) and stops as soon as TI1 becomes high. The TIF flag in the TIMx_SR register is set both when the counter starts or stops. The delay between the rising edge on TI1 and the actual stop of the counter is due to the resynchronization circuit on TI1 input.


3 Slave mode: Trigger mode

The counter can start in response to an event on a selected input.

In the following example, the upcounter starts in response to a rising edge on TI2 input:

• Configure the channel 2 to detect rising edges on TI2. Configure the input filter duration (in this example, we don’t need any filter, so we keep IC2F=0000 0000: No filter, sampling is done at fDTS). The capture prescaler is not used for triggering, so the user does not need to configure it. CC2S bits are selecting the input capture source only, CC2S=01 in TIMx_CCMR1 register. Write CC2P=1 in TIMx_CCER register to validate the polarity (and detect low level only).这里设定和上面基本一致,设定的是通道2,其他一样,略过详细分析。

110: Trigger Mode - The counter starts at a rising edge of the trigger TRGI (but it is not reset). Only the start of the counter is controlled.

• Configure the timer in trigger mode by writing SMS=110 in TIMx_SMCR register. Select TI2 as the input source by writing TS=110 in TIMx_SMCR register. When a rising edge occurs on TI2, the counter starts counting on the internal clock and the TIF flag is set.


Slave mode: External Clock mode 2 + trigger mode(这个模式其实就是引入多一个ETR的信号,同时也是上升沿触发计数,)

The external clock mode 2 can be used in addition to another slave mode (except external clock mode 1 and encoder mode). In this case, the ETR signal is used as external clock input, and another input can be selected as trigger input when operating in reset mode, gated mode or trigger mode. It is recommended not to select ETR as TRGI through the TS bits of TIMx_SMCR register. In the following example, the upcounter is incremented at each rising edge of the ETR signal as soon as a rising edge of TI1 occurs:

1. Configure the external trigger input circuit by programming the TIMx_SMCR register as follows:(配置外部触发,理解为外部时钟)

– ETF = 0000: no filter

– ETPS = 00: prescaler disabled

– ETP = 0: detection of rising edges on ETR and ECE=1 to enable the external clock mode 2.

2. Configure the channel 1 as follows, to detect rising edges on TI:(配置CC通道为输入,不反转,也就是上升沿)

– IC1F = 0000: no filter.

– The capture prescaler is not used for triggering and does not need to be configured.

– CC1S = 01 in TIMx_CCMR1 register to select only the input capture source

– CC1P = 0 in TIMx_CCER register to validate the polarity (and detect rising edge only).

3. Configure the timer in trigger mode by writing SMS=110 (定时器配置为触发模式)

110: Trigger Mode - The counter starts at a rising edge of the trigger TRGI (but it is not reset). Only the start of the counter is controlled.in TIMx_SMCR register. Select TI1 as the input source by writing TS=101 in TIMx_SMCR register.

Bits 6:4 TS: Trigger selection

This bit-field selects the trigger input to be used to synchronize the counter.

000: Internal Trigger 0 (ITR0).

001: Internal Trigger 1 (ITR1).

010: Internal Trigger 2 (ITR2).

011: Internal Trigger 3 (ITR3).

100: TI1 Edge Detector (TI1F_ED)

101: Filtered Timer Input 1 (TI1FP1)

110: Filtered Timer Input 2 (TI2FP2)

111: External Trigger input (ETRF)

See Table 86: TIMx Internal trigger connection for more details on ITRx meaning for each Timer.

Note: These bits must be changed only when they are not used (e.g. when SMS=000) to avoid wrong edge detections at the transition.


A rising edge on TI1 enables the counter and sets the TIF flag. The counter then counts on ETR rising edges. The delay between the rising edge of the ETR signal and the actual reset of the counter is due to the resynchronization circuit on ETRP input.

STM32 - 定时器的设定 - 基础- 0A - Timers and external trigger synchronization - 定时器和外部触发的同步相关推荐

  1. STM32 - 定时器的设定 - 基础- 07 - 6-step PWM generation - 6步长PWM的产生 - COM Event的解释

    前言:本节主要讲互补输出的步长设定问题,同时引入了一个重要的参数COM,这个再诸多STM32的手册里面都过于分散,在本文有一个比较完备的小结: When complementary outputs a ...

  2. STM32 - 定时器的设定 - 基础- 06 - OCxREF signal - Clearing the OCxREF signal on an external event

    OCxREF signal 作为捕捉比较的定制化参考波形在对输出波形的配置上占有巨大的影响.如何将这个信号和外部输入的事件联系起来,能给系统的设计带来更多的可选择性. The OCxREF signa ...

  3. STM32 - 定时器的设定 - 基础- 02 - Capture/compare channels 和相关设置寄存器 - 和STM32缩写词条解释

    前言:捕捉比较通道: STM32丰富的定时器的变化,就在这个模式,所以,复杂是必然的:当然,也可以简单来理解,就是通过选择定时器的输入的配置同时配置定时器的输出来获取我们需要的各种波形: 几个重要的缩 ...

  4. STM32 - 定时器的设定 - 基础- 0D - Timer synchronization chaining - 主从模式下 - 定时器同步和级联控制 - 级联启动定时器

    和前面两次举例不同的是,前面两次是用CC通道的输出波形来做触发,这个例子,用UEV的发生做触发: In this example, we set the enable of Timer 2 with ...

  5. STM32 - 定时器的设定 - 基础 - 05 - Arbitrary waveform generation using timer DMAburst feature - 任意波形的序列产生

    DMA的功能不说了,如何产生任意序列的波形,我们仔细看看: 本节叙述了同DMA的方法,在不占用MCU资源的情况下,通过提前编辑一组任意定制的波形参数,实现复杂的波形输出. STM32 DMA-burs ...

  6. STM32 - 定时器的设定 - 基础-04 - 输出波形控制 - PWM 模式

    PWM为诸多应用的基本波形,如何实现和如何定制,首先要了解STM32中PWM的实现方法. 在STM32中,PWM的实现主要通过控制 OCxRef (active high).:output compa ...

  7. STM32 - 定时器的设定 - 基础 01 - Timer Base - Prescaler description - Upcounting mode

    前言:时基是定时器的最基本的功能: 本节详细叙述STM32的时基的功能. 1 Time-base unit The main block of the programmable advanced-co ...

  8. STM32 - 定时器的设定 - 基础 01.1 - Repetition counter

    前言:Repetition counter是一个STM32增强的计数器功能,有很多用途,本文尝试进行简单阐述: 我们理解为,就是定时器溢出再增加一个倍率rate 的控制.注意是倍率,也就是如果是定时1 ...

  9. STM32 - 高级定时器的设定 - 基础-05 - 输出波形控制 -Complementary outputs and dead-time insertion 相位调整@互补输出和死区控制

    The advanced-control timers (TIM1 and TIM8) can output two complementary(互补) signals and manage the ...

最新文章

  1. SAP QM执行事务代码QE23为检验批录入结果,报错-No selected set exists for the inspection point 200 or plant NMDC-
  2. Netty堆外内存泄露排查与总结
  3. flume 1.7在windows下的安装与运行
  4. Netty的引用计数对象
  5. 机器学习训练秘籍完整中文版下载(吴恩达老师新作)
  6. GDCM:gdcm::DataSet的测试程序
  7. 14.结构体struct.rs
  8. python 示例_带有示例的Python date isocalendar()方法
  9. 为什么你必须了解云原生?!
  10. 【Objective-C】类与结构体的区别
  11. echarts中的option.legend.data has not been defined.
  12. 如何查看cudnn当前版本_当前版本的花木兰,如何成为边路战神?
  13. html是手机吗,MHTML是什么
  14. 计算机怎样用PS抠婚纱图,用PS应该怎样抠出透明婚纱照片
  15. 如何使用cmd进入打印机选项_运行怎么添加打印机 - 卡饭网
  16. IE主页被2345(782782)篡改解决办法
  17. Oracle计算两个日期的月份
  18. 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\tsc.ps1,是指在此系统上禁止运行脚本
  19. 1_VBA_POWERSHELL病毒分析
  20. 影像分辨率、地面分辨率、比例尺及DPI之间的关系

热门文章

  1. php周日,PHP减去一周周日
  2. Qt Installer Framework翻译(5-2)
  3. java 递归 遍历目录下的所有文件
  4. 【Nginx那些事】nginx配置实例(三)动静分离
  5. linux系统用户登陆时脚本执行顺序
  6. 如何遍历JavaScript中的对象
  7. python系统性能模块笔记
  8. 【机器学习技术】高斯过程初探
  9. 神经网络——BP算法
  10. jQuery框架学习第五天:事件与事件对象