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.

Ricardo
6,22420 gold badges56 silver badges90 bronze badges
asked May 13, 2015 at 9:36
\$\endgroup\$
5
  • \$\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\$ Commented May 13, 2015 at 9:57
  • \$\begingroup\$ @lazar thanks for the help but what'S the role of the prescaler \$\endgroup\$ Commented May 13, 2015 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\$ Commented May 13, 2015 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\$ Commented May 13, 2015 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\$ Commented May 13, 2015 at 11:15

1 Answer 1

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.

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.