# Using XMEGA timer

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.

• 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 – Lazar May 13 '15 at 9:57
• @lazar thanks for the help but what'S the role of the prescaler – Engine May 13 '15 at 10:11
• 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. – Lazar May 13 '15 at 10:16
• "the output value is : 2323" - You output two values. Is 0x2323 supposed to be the difference from the two values? – Rev1.0 May 13 '15 at 11:08
• 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. – Rev1.0 May 13 '15 at 11:15

## 1 Answer

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.

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