Skip to main content
jaikrishnamail
Associate
September 22, 2014
Question

STM32F051R8 TMER3 ISSUE

  • September 22, 2014
  • 17 replies
  • 2433 views
Posted on September 22, 2014 at 16:48

Hi..

In STM32F051R8 board, Iam using TIM3. I need to set timer interrupt for every every 10 seconds. Iam unable calculate the prescaler, CCR and time period values.

Can any one explain me please...

    This topic has been closed for replies.

    17 replies

    Tesla DeLorean
    Guru
    September 22, 2014
    Posted on September 22, 2014 at 17:17

    You just need the Prescaler and Period to get an Update interrupt.

    You need to take your source clock frequency, multiply by 10, and then factor into two 16-bit integer numbers (1-65536), and program those as N-1 values for Prescaler/Period

    TIMCLK * 10 = P * Q;

    Prescaler = Q-1;

    Period = P-1;

    This is pretty pedestrian math.
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Montassar BEN ROMDHANE_O
    Visitor II
    September 22, 2014
    Posted on September 22, 2014 at 18:08

    Hi jaikrishna.n,

    You can configure the TIM peripheral to generate a time base of 10 seconds with the corresponding Interrupt request.

    The TIM3 input clock (TIM3CLK)  is set to APB1 clock (PCLK1), since APB1 prescaler is equal to 1.

    • TIM3CLK = PCLK1
    • PCLK1 = HCLK
    • => TIM3CLK = HCLK = SystemCoreClock (Hz)
    To get TIM3 counter clock at 10 KHz, the Prescaler is computed as following:

    • Prescaler = (TIM3CLK / TIM3 counter clock) - 1
    • Prescaler = (SystemCoreClock /10 KHz) - 1
    SystemCoreClock is set to 48 MHz for STM32F0xx Devices.

    • TIM3 ARR register value = 10000 - 1, 
    • Update rate = TIM3 counter clock / (Period + 1) = 0,1 Hz,
    So the TIM3 generates an interrupt each 10s.

    Regards,

    Heisenberg.

    Tesla DeLorean
    Guru
    September 22, 2014
    Posted on September 22, 2014 at 19:00

    SystemCoreClock is set to 48 MHz for STM32F0xx Devices.

     

    • TIM3 ARR register value = 100000 - 1,
    • Update rate = TIM3 counter clock / (Period + 1) = 0,1 Hz,

    So the TIM3 generates an interrupt each 10s.

    Unless I'm mistaken TIM3 is a 16-bit counter, in which case this won't work (100000 does not fit), TIM2 on the other hand might as it's a 32-bit counter.

    0690X0000060MmhQAE.gif

    For a 48 MHz source, workable on a (cheaper) 16-bit timer

    Prescaler = 48000-1;

    Period = 10000-1;

    10 Second period, 0.1 Hz

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Tesla DeLorean
    Guru
    September 22, 2014
    Posted on September 22, 2014 at 19:33

    Prescale to 1 KHz as 10 KHz / 10000 = 1 Hz, not 0.1 Hz

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    jaikrishnamail
    Associate
    September 25, 2014
    Posted on September 25, 2014 at 15:11

    Thanks for your immediate reply, I would like to ask some more doubts.

    Is it possible to set interrupt for every 10 micro seconds? How  to set the value.. If you have any example code or document related to calculation please send to me. jaikrishnamail@gmail.com

    THANK YOU

    Tesla DeLorean
    Guru
    September 25, 2014
    Posted on September 25, 2014 at 17:16

    Ok, but you understand the relationship between frequency and period, right?

    1 MHz = 1 us

    48,000,000 ticks of a 48 MHz clock is 1 second

    48 ticks of a 48 MHz clock is 1 micro second

    480 ticks of a 48 MHz clock is 10 micro seconds

    So factoring 480

    480 = 1 * 480

    Prescaler = 1 - 1;

    Period = 480 - 1;

    I think interrupting at 10 us (100 KHz) is probably excessive for this processor, think about what purpose this serves, and if what you're attempting to do can be kept in the hardware/peripheral domain.
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    munger
    Associate III
    September 25, 2014
    Posted on September 25, 2014 at 21:59

    Right, as clive1 says a 10 usec interrupt is pretty darn frequent for this processor. You'd better get in and out fast if the processor is going to have time to do anything else. Personally I've found it useful to create a timed interrupt of 1 msec (or so) for most every embedded project I've done over the last 30 years. If you need to do something every 10 seconds (and assuming it doesn't have to be real precise) than you can have the 1 ms interrupt increment a counter and the main loop watch for it to count to 10,000. Just a thought.

    Mike

    jaikrishnamail
    Associate
    September 26, 2014
    Posted on September 26, 2014 at 05:50

    Ok Thankyou for your replies..  I will work with receiver timeout in USART1. In RTOR register we need to enter number of baud clocks. My controller clock value is 48MHZ. Can any one explain me how to calculate baud clock value?

    jaikrishnamail
    Associate
    October 6, 2014
    Posted on October 06, 2014 at 15:49

    Hello,

    Iam using USART1 Receiver timeout feature in STM32F051R8. The cod is like below. I did try with differnt values in RTOR register. But interrupt handler was not calling. Please let me know wether there is any mistake in code?

      USART_SetReceiverTimeOut(USART1,0x386);

      USART_ReceiverTimeOutCmd(USART1,ENABLE);

      USART_ITConfig(USART1, USART_IT_RTO, ENABLE);

      USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);

    /*Interrupt Handler*/

    void T1_USART_IRQHandler(void)

    {

    if (USART_GetITStatus(SC_USART, USART_IT_RTO) == SET)

    {

    USART_ClearITPendingBit(SC_USART, USART_IT_RTO);

    USART_ITConfig(SC_USART, USART_IT_RTO, DISABLE);

    }

    Thanks in Advance!

    Tesla DeLorean
    Guru
    October 6, 2014
    Posted on October 06, 2014 at 16:36

    void T1_USART_IRQHandler(void) // ?? Is that in the vector table?

    USART1_IRQHandler(void)
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..