Arduino due / ppm

Victory!

Stimmer your code was awesome, I modified it to generate 8 channel ppm with 0.5ms pulse and 22ms total length. It is working perfectly ( I'll click a few times on your name to increase your karma lol)

Here is the code 8 CHANNELS PPM FOR THE ARDUINO DUE:

// timer is clocked at 42MHz, so 42 ticks per us

uint32_t periods[]={1000*42,1000*42,1000*42,1000*42,1000*42,1000*42,1000*42,1000*42,1000*42};
uint32_t num_periods=8+1;      // number of channels +1

int ppm_channels[8];
long Frame;
long Sum;
int val = 1;

void TC0_Handler()
{
    long dummy=REG_TC0_SR0;    // vital - reading this clears some flag
                               // otherwise you get infinite interrupts
    static int i=0;
    REG_TC0_RC0=periods[i++];
    if (i>=num_periods)i=0;
}

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_CMR0  = 0b00000000000001101100010000000000; // alternative CMR for inverted output
  REG_TC0_RC0   = 100000000;   // counter period
  REG_TC0_CCR0  = 0b101;       // start counter
  REG_TC0_IER0  = 0b00010000;  // enable interrupt on counter = rc
  REG_TC0_IDR0  = 0b11101111;  // disable other interrupts
   
  REG_TC0_RA0   = 0.5*1000*42; // Pulse lenght     = .5ms
  Frame         = 22 *1000*42; // ppm frame lenght = 22ms  
  
  ppm_channels[0]= 0;          // channel 1 from 0 to 255
  ppm_channels[1]= 0;          // channel 2 from 0 to 255
  ppm_channels[2]= 255;        // channel 3 from 0 to 255
  ppm_channels[3]= 0;          // channel 4 from 0 to 255
  ppm_channels[4]= 0;          // channel 5 from 0 to 255
  ppm_channels[5]= 0;          // channel 6 from 0 to 255
  ppm_channels[6]= 0;          // channel 7 from 0 to 255
  ppm_channels[7]= 0;          // channel 8 from 0 to 255
  
  
  NVIC_EnableIRQ(TC0_IRQn);    // enable TC0 interrupts
}

void loop(){
  
  // 1. Setup 8 channels (changing only channel 1 here just for fun)
  ppm_channels[0]= ppm_channels[0] + val; 
  if(ppm_channels[0] >= 255){ val = -1; }
  if(ppm_channels[0] <= 0)  { val = 1;  }
  delay(25);
  
  // 2. Calculate the 8 channels
  Sum = 0;
  for (int i = 0; i < 8; i++)  {
    periods[i] = map(ppm_channels[i], 0, 255, 1000*42, 2000*42);
    Sum = Sum + periods[i]; 
  }
  // 3. Calculate the sync frame
  periods[8] = Frame - Sum;
}

And a picture (worth a 1000 words):

mantoui , I did not comment your code because it use delays to generate ppm. It's OK if your mc does only one thing but it is a problem if you run something else at the same time. Thank you for your help, I'll click a few times on your karma too lol.

Francois