Go Down

Topic: Implementing Synchronous PWM for Hbridge control (Read 201 times) previous topic - next topic


I'm new to Arduino Due. Ive managed to get PWM working on one channel at 140Hz with a 60usec duty cycle using the registers. But I'm struggling to get much further than that.

My project needs to control a H-bridge using two different PWM channels in synchronous mode (ie using the same clock) so that one channel is on as described above then I need some dead time say 60usec and then the second channel switches on for 600usec.

Can anyone help with implentation of this? The code Ive implemented for the single channel is below and the square wave it outputs is good.

Code: [Select]

void setup () {

  PMC->PMC_PCER1 |= PMC_PCER1_PID36;               // PWM on

  REG_PIOC_ABSR |= PIO_ABSR_P7;                        // Set PWM PC7 pin perhipheral type B

  REG_PIOC_PDR |= PIO_PDR_P7;                           // Set PWM pin to an output

  REG_PWM_ENA = PWM_ENA_CHID2;                     // Enable the PWM channel 2
  REG_PWM_CLK = PWM_CLK_PREA(0) | PWM_CLK_DIVA(8);     // Set the PWM clock rate to 10.5MHz (84MHz/8).
  REG_PWM_CMR2 = PWM_CMR_CALG |PWM_CMR_CPRE_CLKA;  // The period is left aligned, clock source as CLKA on channel 2

  REG_PWM_CPRD2 = 37500;                                // Channel 2 : Set the PWM frequency of 140Hz = 10.5MHz/(2 * CPRD)

  REG_PWM_CDTY2 = 37500;                               // Channel 2: Set the PWM duty cycle to x%= (CDTY/ CPRD)  * 100 % , CDTY = 37500 set to 0usec


void loop() {
  // put your main code here, to run repeatedly:

//REG_PWM_CDTY2 = 37185; //60usec on// Channel 2: Set the PWM duty cycle to 0.84%= (CDTY/ CPRD)  * 100 % , CDTY = 37185 set to 60usec
REG_PWM_CDTY2 = 34350; //600usec on// Channel 2: Set the PWM duty cycle to 8.4%= (CDTY/ CPRD)  * 100 % , CDTY = 34350 set to 600usec



Oct 15, 2018, 02:35 pm Last Edit: Oct 15, 2018, 02:35 pm by MartinL
An example of PWM in synchronous mode together with dead-time insertion is being discussed on the Due forum here: https://forum.arduino.cc/index.php?topic=572069.0.

The example code is for synchronous PWM mode running on all 8-channels running at 100kHz, but is just a applicable to PWM operation at lower frequencies.


Yes thanks martin have seen that since i posted. All good Ive managed to implement it

Go Up