Go Down

Topic: Problem getting Arduino DUE timer works (Read 248 times) previous topic - next topic

DUEDUE

Hi there

I have a question getting my Arduino DUE timer works correctly

I will have a function later (header file) which contains a variable "Sampling_time" to set the sampling time

So I just tried to test my code  and to get sure that the timer works I added a variable in the Timer handler which increments its vlaue each time the programm calls the Handler but somehow it dosent work

Here is my code it just about 10 lines

rhanks in advance
Code: [Select]

// Global Variabels
int Interrupt_test; //  incrementer to test the Interrupt

int Sampling_time = 20;// Sampling time for the function

// End global Variabels


// Timer Setup
void startTimer(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t frequency) {
        pmc_set_writeprotect(false);
        pmc_enable_periph_clk((uint32_t)irq);
        TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4);
        uint32_t rc = VARIANT_MCK/128/frequency; //128 because we selected TIMER_CLOCK4 above
        TC_SetRA(tc, channel, rc/2); //50% high, 50% low
        TC_SetRC(tc, channel, rc);
        TC_Start(tc, channel);
        tc->TC_CHANNEL[channel].TC_IER=TC_IER_CPCS;
        tc->TC_CHANNEL[channel].TC_IDR=~TC_IER_CPCS;
        NVIC_EnableIRQ(irq);
}

// End Timer Setup


// Timer Handler
void TC3_Handler()
{
 // My function will come here if the timer code worked
 Interrupt_test ++;
}


void setup() {
 
  Serial.begin(9600);
 
//Start timer
 startTimer(TC2, 0, TC3_IRQn, Sampling_time);
}

void loop() {
 
Serial.print("Interrupt_test "); // Interrupt Test Variable increments the interuppts value
Serial.println(Interrupt_test);

}

Palliser

Hello DUEDUE,

I think I understand what you are facing. I tried myself to make a similar code to run with DUE but
ivanseidel made very good progress with DUE and timers and now we have available his library with examples.

Timer Library fully implemented for Arduino DUE

Here what you want to do using ivan DueTimer library:

Code: [Select]
#include <DueTimer.h>

int Interrupt_test;
int Sampling_time = 5000000; // 5 sec

void myHandler(){
  Interrupt_test ++; 
}

void setup(){
  Serial.begin(9600);
  Timer3.attachInterrupt(myHandler);
  Timer3.start(Sampling_time);
}

void loop(){
  Serial.print("Interrupt_test ");
  Serial.println(Interrupt_test);
}



Thus, I recommend you to use it. Otherwise, good luck.

Regards,

p

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