Electronic – Using XMEGA timer

ctimerxmega

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.

Best 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.