Show Posts
Pages: [1]
1  Products / Arduino Due / Re: Timer Interrupts on Due on: November 04, 2012, 04:00:30 pm
I understand. Thanks smiley
2  Products / Arduino Due / Re: Timer Interrupts on Due on: November 04, 2012, 10:14:02 am
I think I'm getting somewhere with this... try this:
volatile boolean l;

void TC0_Handler()
    long dummy=REG_TC0_SR0; // vital - reading this clears some flag
                            // otherwise you get infinite interrupts
    l= !l;

void setup(){
  pinMode(2,OUTPUT);    // port B pin 25 
  analogWrite(2,255);   // sets up some other registers I haven't worked out yet
  REG_PIOB_PDR = 1<<25; // disable PIO, enable peripheral
  REG_PIOB_ABSR= 1<<25; // select peripheral B
  REG_TC0_WPMR=0x54494D00; // enable write to registers
  REG_TC0_CMR0=0b00000000000010011100010000000000; // set channel mode register (see datasheet)
  REG_TC0_RC0=100000000; // counter period
  REG_TC0_RA0=30000000;  // PWM value
  REG_TC0_CCR0=0b101;    // start counter
  REG_TC0_IER0=0b00010000; // enable interrupt on counter=rc
  REG_TC0_IDR0=0b11101111; // disable other interrupts

  NVIC_EnableIRQ(TC0_IRQn); // enable TC0 interrupts


void loop(){
The timer controls the output of pin 2 and the interrupt toggles a flag which is output on pin 13.

I'll give this a shot thanks. I am curious though why you didn't do a digitalWrite() inside the handler? Arduino's digitalWrite() function is pretty clock-cycle heavy, but the following code works just fine for writing a digital output and only takes a couple of clock cycles if that was the primary concern.

static inline void pinOutput(int pin, int val)
if (val)
g_APinDescription[pin].pPort->PIO_SODR = g_APinDescription[pin].ulPin;
g_APinDescription[pin].pPort->PIO_CODR = g_APinDescription[pin].ulPin;

On another note, Maple uses a different processor, but gives as good of an explanation / example of how timers work as I've found so far:

I haven't tried to implement any of their code yet, but thought I'd share the links.
3  Products / Arduino Due / Timer Interrupts on Due on: November 02, 2012, 06:02:12 pm
I've been searching through the library code as well as thoroughly searched Google and haven't come across any working timer interrupt code for the Due. I could do this on the Uno in my sleep, but have yet to figure it out on the Due. Any pointers ( or example code would be greatly appreciated. Basically I want to get a function to run once every X microseconds. Thanks.
Pages: [1]