Pages: [1]   Go Down
Author Topic: delay using timer  (Read 233 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:

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

Code:


void setup()
{
unsigned int t1, t2;
Serial.begin(115200);
start_timer();

t1 = TC_ReadCV(TC1, 2);
Serial.print("time 1 is ");
Serial.println(t1);
    // some code here..

t2 = TC_ReadCV(TC1, 2);
Serial.print("time 2 is ");
Serial.println(t2);
Serial.print("time taken by setup: ");
Serial.println(t2-t1);

Serial.println("exiting setup");
}


static void stop_timer()
{
Serial.println("stopping timer");

TC_Stop(TC1, 2);
}
static void start_timer()
{
Serial.println("starting timer");

TC_Configure(TC1, 2, TC_CMR_WAVE | TC_CMR_TCCLKS_TIMER_CLOCK3);
TC_Start(TC1, 2);
}

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

got it... i need to start the clock before doing the TC_Configure, like this -

   pmc_enable_periph_clk(ID_TC5);
Logged

Pages: [1]   Go Up
Jump to: