This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Timer configuration in Tiva launchpad



Hi all,

I am trying to implement routines that allows me to measure frequency of a sine wave.  I am trying to read grid frequency. My system clk is 80Mhz and I want to configure timer to run at 5 Mhz, so prescale timer clk by 16 .. I have checked that I an reading zero crossing correctly. My logic is to read timer value every half cycle and reset the counter. but TimerValueGet() is giving me totally off value. below is my code pls let me know if i am configuring timer right.

//

//  Timer initi()

{

SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);                      // enable peripheral
SysCtlDelay(5);
SysCtlPeripheralReset(SYSCTL_PERIPH_TIMER0);
SysCtlDelay(5);
TimerDisable(TIMER0_BASE, TIMER_A);

TimerConfigure(TIMER0_BASE, TIMER_CFG_A_PERIODIC_UP);        // timer A half width and counting up, periodic

TimerPrescaleSet(TIMER0_BASE, TIMER_A , 16 );                         // scaling timer in clk to 5 Mhz
TimerLoadSet(TIMER0_BASE,TIMER_A , 0xFFFF );
TimerEnable(TIMER0_BASE, TIMER_A);

}

when I detect zero crossing immediately I read timer value and reset it to run again, this way I can know time period of a half cycle

// 

//  below is code for reading timer and reset its counter

read_timer_and_reset()

{

timer_value = TimerValueGet(TIMER0_BASE, TIMER_A);              // timer_value is my measure of frequency

HWREG(TIMER0_BASE + TIMER_O_TAV) = 0;                                //  resetting counter to zero

}

please correct me if I am wrong in above implementation. Again I have checked my zero crossing it is working very well.

Regards,

IHS

  • ISHAN SHAH1 said:
    TimerValueGet() is giving me totally off value.

    While better than famed, "does not work" - might you somewhat detail - "totally off?"

    a) by what amount is it off?

    b) how consistent are your readings?  Gaining consistency should be your 1st order of business - then you can see where (and why) your measurement strategy breaks down...

    c) suspect that your, "initial - for test only" use of an MCU generated, low frequency pulse train - rather than the (likely) analog comparator's zero cross detection - will simplify and better bound your development. 

    d) prescaler use w/most ARM MCUs is not as simple, intuitive as it was with past, 80xx, 68xx et. al.  You likely need to devise some means of testing your understanding of the prescalers - and their impact upon follow-on Timer - to achieve measurement success...

  • Hi cb1_mobile,

    yes I think my understanding of prescaler is not right. 

    a) I see three value of timer_value, (14620, 4628, 9628) where I would expect it to be around 41666, which is timer count for a timer running @ 5Mhz for a half cycle. 

    b) My guess- TimerValueGet() alone will not give me my time period. I might have to combine two registers to obtain exact value. 

    Can you please show me some documentations which explains pre-scalers in better. or an example code would also be better.

    Regards,

    Ishan

  • ISHAN SHAH1 said:

    TimerConfigure(TIMER0_BASE, TIMER_CFG_A_PERIODIC_UP);   // timer A half width, counting up, periodic

    And - when Periodic_Up is chosen - there is a serious implication (believe you've missed): (10.3 our MCU manual)

    "Note that when counting down in one-shot or periodic modes, the prescaler acts as a true prescaler and contains
    the least-significant bits of the count. When counting up in one-shot or periodic modes, the prescaler acts as a timer extension and holds the most-significant bits of the count."

    Might this throw a "monkey wrench" into your read of Timer0, Timer_A, only?  Does not your prescaler contain, "bits of interest?"

    You're smart/motivated enough to examine both your MCU manual & SW-DRL-UGxxxx - and redesign your Timer measurement strategy.  Also - we find it best to place just the minimum value w/in the PreScaler - when learning.  Try to observe & master - in the easiest way possible - proceeding in small steps - as you approach your goal.

    This finding - guidance - should get you on your way...  (that count-up may not be to your advantage...)

  • Hi,

    One more observation, from the user manual:

    The prescaler can only be used when a 16/32-bit timer is configured in 16-bit mode and when a 32/64-bit timer is configured in 32-bit mode. 

    So the missing info in configuring the timer:

    TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PERIODIC_UP);

    Another observation: I have doubts about the method - I would use here timer capture and interrupts, it is hardware managed method and does not have possible software timing gaps.

    Petrei