Go Down

Topic: delay using timer (Read 878 times) previous topic - next topic

doors666

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: [Select]


// 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: [Select]



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);
}



doors666

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

   pmc_enable_periph_clk(ID_TC5);

THOR-00

#2
Jul 14, 2014, 12:47 pm Last Edit: Jul 14, 2014, 01:01 pm by THOR-00 Reason: 1
Hi, I'am trying to get time between two events but I also get only zeros. My code looks like this:
Code: [Select]
#include "avr/io.h"
#include "Arduino.h"
#include "../../Atmel/Atmel Toolchain/AVR8 GCC/Native/3.4.1056/avr8-gnu-toolchain/avr/include/avr/iomxx0_1.h"
#include "wiring.h"
#include "C:\Program Files (x86)\Arduino\hardware\arduino\sam\system\libsam\include\tc.h"
#include "C:\Program Files (x86)\Arduino\hardware\arduino\sam\system\libsam\include\pmc.h"
#include "time.h"
#include "sys\types.h"

//ports:
const int zero_signal = A0;
const int return_signal = A1;
const int sync_signal = 24;
const int clock_signal = 22;

//variables:
boolean clk, sync;
unsigned long int zero_value = 0, return_value = 0;
double zero_rise[3], return_rise[3], tim;
int i = 0, j = 0;

void setup()
{
//put your setup code here, to run once:
Serial.begin(115200);
pinMode(zero_signal, INPUT);
pinMode(return_signal, INPUT);
pinMode(sync_signal, INPUT);
pinMode(clock_signal, INPUT);

//MCK = 84MHz
//timer tuning:
//long dummy=REG_TC0_SR0; //vital - reading this clears some flag, otherwise you get infinite interrupts
//NVIC_EnableIRQ(TC0_IRQn); //enable TC0 interrupts
//REG_PIOA_PDR = 1<<25; //disable PIO, enable peripheral
//REG_PIOA_ABSR= 1<<25; //select peripheral A

analogReadResolution(12);
}

void loop()
{
//put your main code here, to run repeatedly:
clk = digitalRead(clock_signal);
sync = digitalRead(sync_signal);

//if (clk) == 1 & sync == 0)
//{
zero_signal_1:
zero_rise[i] = analogRead(zero_signal);
if (i == 1)
{
zero_rise[2] = ((zero_rise[1] - zero_rise[0]) / 4095);
if (((atan(zero_rise[2]))*180)/PI >= 2)
{
//start timer
Serial.println("Start timer");
Serial.flush();
pmc_enable_periph_clk(ID_TC1);
TC_Configure(TC1, 2, TC_CMR_WAVE | TC_CMR_TCCLKS_TIMER_CLOCK3);
TC_Start(TC1, 2);
}
else
{
i = 0;
goto zero_signal_1;
}
}
else
{
i++;
goto zero_signal_1;
}

return_signal_1:
return_rise[j] = analogRead(return_signal);
if (j == 1)
{
return_rise[2] = ((return_rise[1] - return_rise[0]) / 4095);
if (((atan(return_rise[2]))*180)/PI >= 2)
{
//stop counter
tim = TC_ReadCV(TC1, 2);
TC_Stop(TC1, 2);
Serial.println(tim);
Serial.println("Stop timer");
Serial.flush();
}
else
{
j = 0;
goto return_signal_1;
}
}
else
{
j++;
goto return_signal_1;
}
//}
}

Could you help my to configure clock correctly?

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy