I want to use timers to measure time between two time instances. I tried these two approaches but none of them seem to work. output is always printed as 0.
// lets use timer number 6 (ch1, timer 2), address values are from the datasheet
// addresses are for capture mode.
unsigned int volatile * const TC_Control = (unsigned int *) 0x40084080;
unsigned int volatile * const TC_Mode = (unsigned int *) 0x40084084;
unsigned int volatile * const TC_Value = (unsigned int *) 0x40084090;
static void stop_timer()
{
Serial.println("stopping timer");
// control register - setting this to one will enable timer, set to 2 to disable timer
*TC_Control = TC_CCR_CLKDIS;
}
static void start_timer()
{
Serial.println("starting timer");
// this is used to set the prescalar, set it to 2 to use TIMER_CLOCK3 (MCK/32)
*TC_Mode = 2;
// control register - setting this to one will enable timer, set to 2 to disable timer
*TC_Control = TC_CCR_CLKEN | TC_CCR_SWTRG;
}
void setup()
{
unsigned int t1, t2;
Serial.begin(115200);
start_timer();
t1 = *TC_Value;
Serial.print("time 1 is ");
Serial.println(t1);
// some more code comes here.
t2 = *TC_Value;
Serial.print("time 2 is ");
Serial.println(t2);
Serial.print("time taken by setup: ");
Serial.println(t2-t1);
Serial.println("exiting setup");
}
I also tried the arduino calls. that also printed zero.