Due Interrupt code from Uno

First of all, I've been working with Arduino for just about 1-2 weeks so please excuse my lack of knowledge. I'm working on a project in which I'm trying to create a DC/AC converter from scratch, controlled by an Arduino.

Anyway, someone at school that worked on a similar project gave me this code for an interrupt. However, it's for an Arduino Uno and I cannot for the life of me figure out which Due register equals each Uno register. I'm comparing between the datasheets for each model, looking after the registers that serve the same purpose, but I must be doing something wrong. For example, from what I understand, Timer/Counter Control Register in Uno would equal TC Channel Mode Register in Due. Yet, whenever I try to verify my code, TC_CMR0 appears undeclared.

Anyway, here's the code I was given:

void interrupt_setup()

{

noInterrupts(); //disable interrupts

TCCR4A = 0;

TCCR4B = 0;

TCNT4 = 0;

TCCR4B |= (1 << CS42);

TCCR4B |= (1 << CS40); //1024 prescaler => 16MHz/1024=15625Hz

TCCR4B |= (1 <<WGM42); //CTC mode

OCR4A = 1560; // for 6.4e-5 seconds -> 1; for 1 second -> 15625 we choose 15625-1

TIMSK4 |= (1 << OCIE4A); //enable timer compare interrupt

interrupts(); //enable interrupts

}

ISR(TIMER4_COMPA_vect) // timer compare interrupt service routine

{

if (HIGH == digitalRead(22)) digitalWrite(22, LOW);

else digitalWrite(22, HIGH);

flag1 = 1; //leads to PID activation

timer = timer + 1;

float power = U_real*I_out;

communication(U_real2,U_real,10,I_out,10,dutycycle,timer,setpoint_U,power,I_out2);

}

Please tell me if I'm approaching this totally wrong or something else that I should do.

There are example sketches in the DUE sub forum for Timer Counter programming, plus you can use TC_lib from antodom.

Here is an example sketch with TC8 (Timer Counter 2 Channel 2):

/******************************************************************/
/*       Timer Counter 2 Channel 2, namely TC8, 1 MHz frequency   */
/******************************************************************/
void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  tc_setup();

}

void loop() {

}

void tc_setup() {

  PMC->PMC_PCER1 |= PMC_PCER1_PID35;                        // TC8 power ON : Timer Counter 2 channel 2 IS TC8 - see page 38

  //PIOD->PIO_PDR |= PIO_PDR_P7;                            // Set the pin to the peripheral
  //PIOD->PIO_ABSR |= PIO_PD7B_TIOA8;                       // Peripheral type B

  TC2->TC_CHANNEL[2].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK1  // MCK/2 = 42 M Hz, clk on rising edge
                              | TC_CMR_WAVE               // Waveform mode
                              | TC_CMR_WAVSEL_UP_RC;       // UP mode with automatic trigger on RC Compare
  //  | TC_CMR_ACPA_CLEAR         // Clear TIOA2 on RA compare match
  //  | TC_CMR_ACPC_SET;          // Set TIOA2 on RC compare match


  TC2->TC_CHANNEL[2].TC_RC = 42;  //<*********************  Frequency = (Mck/2)/TC_RC  Hz
  //TC2->TC_CHANNEL[2].TC_RA = 21;  //<********************   Any Duty cycle in between 1 and TC_RC


  TC2->TC_CHANNEL[2].TC_IER = TC_IER_CPCS;//TC_IER_CPAS;
  NVIC_EnableIRQ(TC8_IRQn);
  TC2->TC_CHANNEL[2].TC_CCR = TC_CCR_SWTRG | TC_CCR_CLKEN; // Software trigger TC2 counter and enable
}

void TC8_Handler() {

  static uint32_t Count;

  TC2->TC_CHANNEL[2].TC_SR;                               // Read and clear status register
  if (Count++ == 1000000) {
    Count = 0;
    PIOB->PIO_ODSR ^= PIO_ODSR_P27;                       // Toggle LED every 1 Hz
  }
}

I cannot for the life of me figure out which Due register equals each Uno register.

The Due Timers are not at all like the Uno timers, so you're not going to be able to find simple equivalents.
It is probably better to go back to "first principles" and understand what the Uno code is trying to DO, and then figure out how to do that with the Due timers or libraries...

TC_CMR0 appears undeclared

In addition to the differences in function, the Due also has a completely different naming scheme for registers. Instead of something like TCCR4B (capture compare register B for TC4, right?), you'd have something more like:

TC4->TC_CHANNEL[1].TC_CCR

("ard_newbies" code has actual code; this is just an example of style that is probably wrong in its details.)