Synchronous PWM on Arduino Due

Hey,

For a project, I need three PWM signals to be synchronous and in phase-correct mode. Furthermore, the frequency should be greater than 20khz (not in the audible range) and it should be 8 or 10 bit. Unfortunately, I have no experience with ARM based processors. Can somebody explain to me, how I can setup my Arduino Due to produce this kind of output?

Secondly I wonder if somebody can tell me how many PWM pins the Due has. I found different numbers varying between 4 and 27.

Thanks in advance
Josua

PS: I've got a working example on an Arduino Mega, but I need the better performance of the Due for my project.

Secondly I wonder if somebody can tell me how many PWM pins the Due has. I found different numbers varying between 4 and 27.

There are 2 types of PWM ... Timer/Counter based PWM available on pins D2-D13 (12 pins).
The Arduino IDE accesses these pins.

Then there's the PWM Controller (PWMC) ... very flexible and very powerful. However this requires directly programming the registers or using a custom library. There are 16 pins here PWMH 0-7 and PWML 0-7 (8-channels), but one or two of these pins are not broken on the Due. The first 4-channels have all pins broken out. Refer to Table 39-2 (pg 987) of the datasheet and to the Due Pinout Diagram for reference.

At 25kHz PWM, you could control the duty cycle from 0-3360 which is better than 11-bit. (84MHz/25kHz=3360).

Perhaps if you attached your Mega's working example, it would be a good reference and aid in providing help.

Thanks for your answer :slight_smile:

This is my code reduced to the relevant parts:

#define L6234_IN1 6 //Arduino Mega Timer 4 A
#define L6234_IN2 7 //Arduino Mega Timer 4 B
#define L6234_IN3 8 //Arduino Mega Timer 4 C

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

uint8_t mstepA = 0; //current steps
uint8_t mstepB = 102;
uint8_t mstepC = 204;
uint8_t pwm[256]; //array containing a sine wave

void setup(){
  TCCR4A = _BV(COM4A1) | _BV(COM4B1) | _BV(WGM40); //phase-correct pwm
  TCCR4B = _BV(CS40); //no prescaler

  pinMode(L6234_IN1, OUTPUT);
  pinMode(L6234_IN2, OUTPUT);
  pinMode(L6234_IN3, OUTPUT);
}

void loop(){
  sbi(TCCR4A, COM4A1);
  OCR4A = (int16_t) pwm[mstepA]; //write duty cycle to OCR
  
  sbi(TCCR4A, COM4B1);
  OCR4B = (int16_t) pwm[mstepB]; //write duty cycle to OCR
  
  sbi(TCCR4A, COM4C1);
  OCR4C = (int16_t) pwm[mstepC]; //write duty cycle to OCR
   
  mstepA++; mstepB++; mstepC++;
  delay(10);
}

How do I use and setup the Timer/Counter based pins? When my prototype works, I'm probably going to need 3*3 PWM outputs, thus it would be nice if I had those 12 pins.

Edit: I just read a little in the sam3x8e's datasheet and I discovered that you have to assign the pwm channels to the GPIOs. So I could assign the PWMC channels to the pins myself?