3
\$\begingroup\$

I want to use the timer in XMEGA to know how long it takes for a function to be done:

void timerINI() {
    //cli(); // deactivating the interrupt 
    TCC0.CTRLA = TC_CLKSEL_DIV1024_gc;
    TCC0.CTRLB = TC_BYTEM_NORMAL_gc;
    //TCC0.INTCTRLA= 0x03;
    TCC0.PER =  0x7A12;
}

int main () {
    ....................
    while(1) {
        TCC0_CNT = 0x00;
        printf(" 1THE timer value is : %5x \n ",TCC0_CNT);
        otherFUNCTION();
        printf(" 2 THE timer value is : %5x \n ",TCC0_CNT);
    }
}

The output value is 2323. I have a problem understanding what this value mean in time to me. The MCU is running at 32MHz.

\$\endgroup\$
  • \$\begingroup\$ I think that output value represents number of cycles done for the function to be completed. At 32MHz frequency, period is 31 ns and when you multiply it with number of cycles you get time which function needs to be completed, and that time is 72 uS \$\endgroup\$ – Lazar May 13 '15 at 9:57
  • \$\begingroup\$ @lazar thanks for the help but what'S the role of the prescaler \$\endgroup\$ – Engine May 13 '15 at 10:11
  • \$\begingroup\$ Just read this en.wikipedia.org/wiki/Prescaler, if I'm right just divide 32MHz with prescaler which is TCC0.CTRLA = TC_CLKSEL_DIV1024_gc; and do the same calculation with that new frequency. \$\endgroup\$ – Lazar May 13 '15 at 10:16
  • \$\begingroup\$ "the output value is : 2323" - You output two values. Is 0x2323 supposed to be the difference from the two values? \$\endgroup\$ – Rev1.0 May 13 '15 at 11:08
  • \$\begingroup\$ How does that even compile? Isn't TCC0_CNT supposed to be TCC0.CNT? And the line TCC0.PER = 0x7A12; is not required at all the way you use the timer in this example. \$\endgroup\$ – Rev1.0 May 13 '15 at 11:15
3
\$\begingroup\$

Your XMEGA frequency is 32MHz, with line TCC0.CTRLA = TC_CLKSEL_DIV1024_gc you've selected prescaler to 1024, you need to divide the core frequency 32000000/1024 = 31250 = 31 KHz, then find a period of that 31KHz, which is 32us and multiply it with number of cycles function needs to be completed (2323) and after that you'll getting time you need, which is 74ms.

\$\endgroup\$
  • \$\begingroup\$ thanks a lot for your help. PS 2323 was HEx ;-) \$\endgroup\$ – Engine May 13 '15 at 10:57
  • 1
    \$\begingroup\$ So its 8995 instead of 2323 after conversion and and you're getting there 287ms. \$\endgroup\$ – Lazar May 13 '15 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.