cancel
Showing results for 
Search instead for 
Did you mean: 

Timer counter clock divide

vault
Associate
Posted on March 22, 2013 at 16:28

Hi,

does some one know how to reduce timer counter clock? I need to measure some pulse width around 1ms using 16bit timer. I have already configured the timer but I get an overflow.


TIM_ICInitStructure_sensor_echo.TIM_Channel = TIM_Channel_1;

TIM_ICInitStructure_sensor_echo.TIM_ICPolarity = TIM_ICPolarity_Rising;

TIM_ICInitStructure_sensor_echo.TIM_ICSelection = TIM_ICSelection_DirectTI;

TIM_ICInitStructure_sensor_echo.TIM_ICPrescaler = TIM_ICPSC_DIV1;

TIM_ICInitStructure_sensor_echo.TIM_ICFilter = 0x0;


TIM_PWMIConfig(TIM9, &TIM_ICInitStructure_sensor_echo);


/* Select the TIM9 Input Trigger: TI1FP1 */

TIM_SelectInputTrigger(TIM9, TIM_TS_TI1FP1);


/* Select the slave Mode: Reset Mode */

TIM_SelectSlaveMode(TIM9, TIM_SlaveMode_Reset);

TIM_SelectMasterSlaveMode(TIM9,TIM_MasterSlaveMode_Enable);


/* TIM enable counter */

TIM_Cmd(TIM9, ENABLE);


/* Enable the CC2 Interrupt Request */

TIM_ITConfig(TIM9, TIM_IT_CC2, ENABLE);

+


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;

GPIO_Init(GPIOA, &GPIO_InitStructure);



/* Connect TIM9 pins to AF2 */ 

GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM5);


NVIC_InitTypeDef NVIC_InitStructure;


/* TIM9 clock enable */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE);


/* Connect TIM pin to AF2 */

GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_TIM9);


/* Enable the TIM9 global Interrupt */

NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_TIM9_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

#stm32-timer-counter
10 REPLIES 10
Posted on March 22, 2013 at 16:57

Typically you'd configure the prescaler used by the time base, so the period and prescaler give you two 16-bit factors, which carefully selected give a range pushing 32-bit, but degrade the granularity.

Alternatively you'd count the slower clock over a period (ie frequency) as aposed to measuring the period in units of a faster clock.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
msanchez
Associate II
Posted on July 28, 2015 at 13:55

Hi,

Does someone know  how to use clock division?

I need to count 3 minutes using clock division but I only count 1 minute because I don't know how to configuire clock división in the tim 2. I write in this field 0, 1, 2 or 4 and TIM_CKD_DIV1, TIM_CKD_DIV2 and TIM_CKD_DIV4 and all the time the timer only count 1 minute (56.65 s)

I have already configured the timer, here is my code:

void RCC_Config()

{

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);                         // Enable GPIOA clock 

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);                          // Enable TIM2 clock

}

void GPIO_Config()

{

  /* Configuración GPIO para poner pin a nivel alto y bajo*/

  GPIO_InitStruct.GPIO_Pin =  GPIO_Pin_1;                                      // We are going to use PA11

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;                                 // Alternate function Push-Pull mode                                  

  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;                                // Set GPIO speed

  GPIO_Init(GPIOA, &GPIO_InitStruct);                                           // GPIOA11 ready

}

void NVIC_Config()

{

  NVIC_SetPriorityGrouping(NVIC_PriorityGroup_0);

  

/* Configure TIM2 IRQ*/

  NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;

  NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;

  NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStruct);

}

void TIM_Config()

{

  TIM_TimeBaseStruct.TIM_Period = 0xFFFF;                                       // Configures the period value to be loaded into the active Auto-Reload Register at the next update event

  TIM_TimeBaseStruct.TIM_Prescaler = 0xFFFF;                                    // Configure the prescaler value used to divide the TIM clock [0x0000 - 0xFFFF] 

  TIM_TimeBaseStruct.TIM_ClockDivision = 1;                                     // Configures the clock division

  TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;                      // TIM Upcounting mode

  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStruct);

}

void TIM2_IRQHandler(void)

{

  if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)

  {

    /*Clear the Update pending bit*/

    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

    

    /*Comprobación del tiempo que tarda en generar la interrupción*/

    GPIO_SetBits(GPIOA, GPIO_Pin_11);

    GPIO_ResetBits(GPIOA, GPIO_Pin_11);

  }

}

/**

  * @brief  Main program

  * @param  None

  * @retval None

  */

void main(void)

{

  RCC_Config(); 

  GPIO_Config();                                                                // Setup GPIO

  TIM_Config();

  NVIC_Config();

  

  /*Enable the TIM Update Interrupt*/

  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

  /*TIM counter enable*/

  TIM_Cmd(TIM2, ENABLE);

  

  NVIC_EnableIRQ(TIM2_IRQn);

  

  while(1);

}

Posted on July 28, 2015 at 15:35

And you're using WHAT part?

TIM_TimeBaseStruct.TIM_ClockDivision doesn't relate to the Period/Prescaler of the timebase.

If you have an F1 part, consider counting off seconds, or milliseconds.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
msanchez
Associate II
Posted on July 29, 2015 at 09:49

I use all the code that I show before. I don't understand what you want to say me. My problem is that I want to count 3 minutes but I only can count 1 minute (59.65 seconds). I think if I use clock division I can divider the clock frequency and I could count more than 1 minute, but changing the clock division the timer count the same. Is there any way to configure that to count more than 1 minute because the prescaler and the period are in the maximum.

Thank you.

Posted on July 29, 2015 at 12:19

I think if I use clock division I can divider the clock frequency...

And I'm telling you that's NOT how it works. It's UNRELATED to the Update rate set by the Prescaler and Period settings.

What you can do is tell the TIM to count off 36000000 ticks (30 secs), then count off 6 entries into the IRQ Handler before toggling the pin.

You could do something similar with TIM1 or TIM8, and set the RepetitionCount to 6-1, as I recall. This WOULD push the Update rate out.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
msanchez
Associate II
Posted on July 29, 2015 at 13:50

Hi,

I've decided to configure TIM1 but I have a new problem, now the interrumpt doesn't occur. And I don't know how would be the configuration of the NVIC. Here is my code if someone could tell me if something is wrong.

Thank you.

void RCC_Config()

{

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);                         // Enable GPIOA clock 

  RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);                          // Enable TIM2 clock

 

  //RCC_PCLK1Config(RCC_HCLK_Div8);

}

void GPIO_Config()

{

  /* Configuración GPIO para poner pin a nivel alto y bajo*/

  GPIO_InitStruct.GPIO_Pin =  GPIO_Pin_11;                                      // We are going to use PA11

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;                                 // Alternate function Push-Pull mode                                  

  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;                                // Set GPIO speed

  GPIO_Init(GPIOA, &GPIO_InitStruct);                                           // GPIOA11 ready

}

void NVIC_Config()

{

  NVIC_SetPriorityGrouping(NVIC_PriorityGroup_0);

  

/* Configure TIM2 IRQ*/

  NVIC_InitStruct.NVIC_IRQChannel = TIM1_CC_IRQn;

  NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;

  NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStruct);

}

void TIM_Config()

{

  TIM_TimeBaseStruct.TIM_Period = 7199;                                       // Configures the period value to be loaded into the active Auto-Reload Register at the next update event

  TIM_TimeBaseStruct.TIM_Prescaler = 9;                                    // Configure the prescaler value used to divide the TIM clock [0x0000 - 0xFFFF] 

  TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;                                     // Configures the clock division

  TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;                      // TIM Upcounting mode

  TIM_TimeBaseStruct.TIM_RepetitionCounter = 0;

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStruct);

}

//void TIM2_IRQHandler(void)

void TIM1_CC_IRQHandler(void)

{

  if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)

  {

    /*Clear the Update pending bit*/

    TIM_ClearITPendingBit(TIM1, TIM_IT_Update);

    

    /*Comprobación del tiempo que tarda en generar la interrupción*/

    GPIO_SetBits(GPIOA, GPIO_Pin_11);

    GPIO_ResetBits(GPIOA, GPIO_Pin_11);

  }

}

/**

  * @brief  Main program

  * @param  None

  * @retval None

  */

void main(void)

{

  RCC_Config(); 

  GPIO_Config();                                                                // Setup GPIO

  TIM_Config();

  NVIC_Config();

  

  /*Enable the TIM Update Interrupt*/

  TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);

  /*TIM counter enable*/

  TIM_Cmd(TIM1, ENABLE);

  

  NVIC_EnableIRQ(TIM1_CC_IRQn);

  

  while(1);

}

Posted on July 29, 2015 at 14:54

You'd want to use TIM1_UP_IRQHandler not the CC one in the NVIC/Handler

For 30 seconds, off a 72 MHz clock you'd need 2,160,000,000 ticks. ie Prescaler = 36000-1, Period = 60000-1;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
msanchez
Associate II
Posted on July 30, 2015 at 09:49

Hi,

I have configured Tim 1 like you told me, but I have the same problem. The interrupt never occur. Am I doing something wrong?

void RCC_Config()

{

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);                         // Enable GPIOA clock 

  RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);                          // Enable TIM2 clock

}

void GPIO_Config()

{

  /* Configuración GPIO para poner pin a nivel alto y bajo*/

  GPIO_InitStruct.GPIO_Pin =  GPIO_Pin_11;                                      // We are going to use PA11

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;                                 // Alternate function Push-Pull mode                                  

  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;                                // Set GPIO speed

  GPIO_Init(GPIOA, &GPIO_InitStruct);                                           // GPIOA11 ready

}

void NVIC_Config()

{

  NVIC_SetPriorityGrouping(NVIC_PriorityGroup_0);

  

/* Configure TIM2 IRQ*/

  NVIC_InitStruct.NVIC_IRQChannel = TIM1_UP_IRQn;

  NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;

  NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStruct);

}

void TIM_Config()

{

  TIM_TimeBaseStruct.TIM_Period = 60000 - 1;                                    // Configures the period value to be loaded into the active Auto-Reload Register at the next update event

  TIM_TimeBaseStruct.TIM_Prescaler = 36000 - 1;                                 // Configure the prescaler value used to divide the TIM clock [0x0000 - 0xFFFF] 

  TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;                          // Configures the clock division

  TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;                      // TIM Upcounting mode

  TIM_TimeBaseStruct.TIM_RepetitionCounter = 2;                                 // TIM counter repetition

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStruct);

}

void TIM1_UP_IRQHandler(void)

{

  if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)

  {

    /*Clear the Update pending bit*/

    TIM_ClearITPendingBit(TIM1, TIM_IT_Update);

    

    /*Comprobación del tiempo que tarda en generar la interrupción*/

    GPIO_SetBits(GPIOA, GPIO_Pin_11);

    GPIO_ResetBits(GPIOA, GPIO_Pin_11);

  }

}

/**

  * @brief  Main program

  * @param  None

  * @retval None

  */

void main(void)

{

  RCC_Config(); 

  GPIO_Config();                                                                // Setup GPIO

  TIM_Config();

  NVIC_Config();

  

  /*Enable the TIM Update Interrupt*/

  TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);

  /*TIM counter enable*/

  TIM_Cmd(TIM1, ENABLE);

  

  NVIC_EnableIRQ(TIM1_UP_IRQn);

  

  while(1);

}

Posted on July 30, 2015 at 15:02

RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); 

*APB2*

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..