STM32 Timer Clock sources -- External Clock Both Edge

Timers get their clock source from External pins or Internal timer sources.

External

External = pins: TI1 or TI2 or ETR

  1. set pin to be used:
    • in TIMx_CCMR1 reg - set pin to be used by writing CCxS bits
  2. select polarity of input
    • in TIMx_CCER reg - write CCxP and CCxNP to select rising, falling or both edges
  3. select external clock mode1
    • in TIMx_SMCR reg - write SMS=111
  4. optionally configure filter and prescaler
    • in TIMx_CCMR1 - write ICxF[3:0] bits
  5. optionally set trigger input source as Timer TIx
    • in TIMx_SMCR reg - write TS bits
  6. Enable channel
    • in TIMx_CCER reg - set CCEx bit

If ETR is to be input clock:

  1. set clock mode2
    • in TIMx_SMCR reg - set ECE=1
  2. configure prescaler, filter, polarity
    • in TIMx_SMCR reg - set ETPS[1:0], ETF[3:0], ETP

Internal

One Timer can be used as the prescaler for another.

The first timer update_event, or output_compare signal is used as clock for the second. Uses TRGI to map. Counter mode is set using the TIMx_CR1 reg and CMS bits as indicated in the example below. The counter mode sets whether the update_event occurs on overflow and/or underflow of the Timer.

 

Clock source selection

This is one of the difficulties. From the manual, we see that there are three clock sources:

1, the internal clock.

To do is to choose CK_INT clock, this simple, but it must be noted,

the timer clock is not directly from the APB1 or domain (APB2) of but from input APB1 or APB2, a frequency multiplier,

when APB1 prescaler factor of 1, the frequency multiplier does not work, the clock frequency of the timer equal to the APB1 frequency;

When the prescaler APB1 coefficient for the other values (ie prescaler factor of 2, 4, 8, or 16),

the times The frequency function, the timer clock frequency equal to twice the frequency of the APB1.

Such as AHP 72M, APB12 frequency of 36M, then TIMER is APB1 multiplied by 2, that is 72M.

How to select clock mode? As long as the the SMCR SMS [2:0] inflicted 000 just fine

2, the external clock mode 1

This is too much trouble, clock source selection is actually TRGI (trigger input),

but a lot of trigger input selection, a total of eight ......

See block diagram, they by: ITRx TI1F_ED, TI1FP1, TI2FP2, ETRF

ITRx things timer cascade, whether he temporarily.

To enter the This clock mode first set SMS 111, of course, this does not end, unlike the internal clock, anything with a good,

here you have to configure other parameters, such as TI1FP1 choose natural input channel 1 configuration parameters,

so that the clock can come in the way you need. Is equipped with block diagram of this

In the CCMR1 register select the corresponding input the (The CC1S) good and filter (IC1F)

and then configure input polarity the SMCR (CC1P)

and then select the trigger source (TS [2:0]) for TI1 input channel with a good the final choice of SMS is 111, the clock (CR1 CEN).

Now the clock is input from TI1, can count like a clock source.

Similarly, if use it ETR channel with a good on it.

3, the external clock mode 2

Select the external input as the clock, see block diagram:

As can be seen from FIG ETR can be directly used as the clock input can also trigger input (TRGI) as a clock input,

the clock mode 1 trigger source is selected as the ETR, two effects are the same,

it looks like this external clock mode useless, in fact, is not the case,

he can tell from the mode (reset, triggered, gated) combination.

When triggered mode is selected, for example, we can no longer ETR selected trigger source,

because the slave mode controller is accounted for,

fortunately there is the external clock mode 2, we have chosen this mode

after a combination of both in with the completion of some of the features.

Summarize the STM32 clock selection rather special, no longer a clock configuration bits in SFR,

not to say the two together 00, 01, but by the selected

SMCR SMS and ECE to control, to feel this way can the internal clock and the external mode open at the same time

(SMS: 000, ECE: 1) can also be external mode and external mode at the same time open (SMS: 111, ECE: 1 ),

in fact, the above two methods are used in an external clock.

 

 

2.1 Clock input sources

The timer can be synchronized by several clocks simultaneously:
 Internal clock
 External clock
– External mode1 (TI1 or TI2 pins)
– External clock mode2 (ETR pin)
– Internal trigger clock (ITRx)

2.1.1 Internal clock

The timer is clocked by default by the internal clock provided from the RCC.

To select this clock source, the SMCR_SMS (if present) bits should be reset.

2.1.2 External clock

The external clock timer is divided in two categories:
 External clock connected to TI1 or TI2 pins
 External clock connected to ETR pin

In these cases, the clock is provided by an external signal connected to TIx pins or ETR pin.

The maximum external clock frequency should be verified.

Note:

1 In addition to all these clock sources, the timer should be clocked with the APBx clock.
2 The external clocks are not directly feeding the prescaler, but they are first synchronized with the APBx clock through dedicated logical blocks.

External clock mode1 (TI1 or TI2 pins)

In this mode the external clock will be applied on timer input TI1 pin or TI2 pin. To do this:

1.Configure the timers to use the TIx pin as input:

a) Select the pin to be used by writing CCxS bits in the TIMx_CCMR1 register.

b) Select the polarity of the input:
For the STM32F100/101/102/103/105/107 lines: by writing CCxP in the TIMx_CCER register to select the rising or the falling edge
For the STM32F100/101/102/103/105/107 lines, polarity selection for both edges can be achieved by using TI1F_ED, but only for TI1 input.
For the other series & lines: by writing CCxP and CCxNP in the TIMx_CCER register to select the rising/falling edge, or both edges.

c) Enable the corresponding channel by setting the CCEx bit in the TIMx_CCER register.

2. Select the timer TIx as the trigger input source by writing TS bits in the TIMx_SMCR register.
3. Select the external clock mode1 by writing SMS=111 in the TIMx_SMCR register.

CC1NP/CC1P bits 

slave mode : disabled, reset, external clock, trigger, gated or encoder mode( 1,2,3 )

rising or falling edge for capture or slave mdoe in reset, external clock or trigger mode
inverted or not level for slave mdoe in gated mode or encoder mode

CC1NP/CC1P bits select the active polarity of TI1FP1 and TI2FP1 for trigger or capture operations.

00: rising edge  : sensitive to TIxFP1 rising edge 
00: non-inverted : TIxFP1 is not inverted 

01: falling edge : sensitive to TIxFP1 falling edge 
01: inverted     : TIxFP1 is inverted 

10: reserved, do not use this configuration.

11: both edges   : sensitive to both TIxFP1 rising and falling edges 
11: non-inverted : TIxFP1 is not inverted 
This configuration must not be used in encoder mode.

 

External clock mode2 (ETR pin)

The external clock mode2 uses the ETR pin as timer input clock. To use this feature:

1.Select the external clock mode2 by writing ECE = 1 in the TIMx_SMCR register.
2. Configure, if needed, the prescaler, the filter and the polarity by writing ETPS [1:0], ETF [3:0] and ETP in the TIMx_SMCR register.

Internal trigger clock (ITRx)

This is a particular mode of timer synchronization.
When using one timer as a prescaler for another timer, the first timer update event or output compare signal is used as a clock for the second one.

stm32f2xx external clock source for timer

I trying to use a timer with an external clock source on a stm32f207ZE microcontroller. But it isn't working. Here is my code:

/* TIM1 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

  /* GPIOE clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

  /* GPIOE Configuration: PE.11(TIM1 CH2)  */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOE, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);
  GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_TIM1);

  /* TIM1 Input trigger configuration: External Trigger connected to TI2 */
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV2;
  TIM_ICInitStructure.TIM_ICFilter = 0x0;
  TIM_ICInit(TIM1, &TIM_ICInitStructure);

  TIM_TIxExternalClockConfig (TIM1, TIM_TS_TI2FP2, TIM_ICPolarity_Rising, 0) ;
  TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_External1);

  TIM_Cmd (TIM1, ENABLE);
Where can be my mistake
?
I have some misunderstanding on what source pin must be used for Timer1 TI2.
Is it the same that Timer1_CH2? I also tried ETR mode by inizializing ETR pin /* TIM1 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); /* GPIOE clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); /* GPIOE Configuration: PE.7(TIM1 ETR) */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOE, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_TIM1); TIM_TimeBaseStructure.TIM_Period = 0xFFFF; // 5000*20 uS - 100 ms TIM_TimeBaseStructure.TIM_Prescaler = 0; // frequency - 20 MHz TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //for ETR IN RM0033 p. 372 TIM1->SMCR = 0; TIM1->SMCR |= TIM_ExtTRGPSC_DIV4; //2. set prescaller TIM1->SMCR |= TIM_ICPolarity_Rising; //3. rising edge TIM1->SMCR |= 0x4000; // 4. Enable external clock mode 2 by writing ECE=1 TIM_Cmd (TIM1, ENABLE);

But it also doesn't work.

Maybe frequency is to high?

And i saw some strnge behaviour. When i inizialize once more counter:

void inizializeSimpleCounter(){
  NVIC_InitTypeDef                  NVIC_InitStructure;
    TIM_TimeBaseInitTypeDef     TIM_TimeBaseStructure;

/****************Timer4 Inizialization**************************/
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

    TIM_TimeBaseStructure.TIM_Period = 5000;        // 5000*20 uS - 100 ms
    TIM_TimeBaseStructure.TIM_Prescaler = 3-1;  // frequency - 20 MHz
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); 

    TIM4->DIER = TIM_DIER_UIE;

    TIM_Cmd(TIM4, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel =  TIM4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
    NVIC_Init(&NVIC_InitStructure);
/*************************************************************/
}

The timer, which was initialize to count from extrenal counter counts some values, and then stops

Some external source with frequency 20 MHz is connected to this pin.

I expext that counter will begin to count - so the value in TIM1->CNT register will increment. But nothing counts

It seems that error was in schematic. Inizialization is right

I had the same problem but solved it with the code below:

TIM2->SMCR =0x0067;
TIM2->CCMR1=0x0100;
TIM2->CCER=0x0010;
TIM2->CNT = 0x1; 

 

posted @ 2015-09-08 12:03  IAmAProgrammer  阅读(9921)  评论(0编辑  收藏  举报