# 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