Go Down

Topic: [SOLVED] Multiple synchronous PWM outputs with different frequencies (Read 152 times) previous topic - next topic

Crameroni

Hello,
In my project I have to generate 5 different frequencies with a duty Cycle of 50%.
But all of the Frequencies have to be synchronous ( Its okay if they differ in ns, but not much)
my actual code does produce 1 frequence on 5 outputs, but I cant do the tricks do do different frequencies on each PWM-Channel.

Can you help me how to di this?

Here is my code so far:

Quote
void setup() {
  // PWM Set-up on pin: DAC1
  REG_PMC_PCER1 |= PMC_PCER1_PID36;                     // Enable PWM
  REG_PIOB_ABSR |= PIO_ABSR_P16;                        // Set PWM pin perhipheral type A or B, in this case B
  REG_PIOB_PDR |= PIO_PDR_P16;                          // Set PWM pin to an output
  REG_PWM_CLK = PWM_CLK_PREA(0) | PWM_CLK_DIVA(1);     // Set the PWM clock rate to 2MHz (84MHz/42)
  REG_PWM_CMR0 = PWM_CMR_CALG | PWM_CMR_CPRE_CLKA;      // Enable dual slope PWM and set the clock source as CLKA
  REG_PWM_CPRD0 = 608;                                // Set the PWM frequency 2MHz/(2 * 20000) = 50Hz
  REG_PWM_CDTY0 = 304;                                 // Set the PWM duty cycle to 1500 - centre the servo

  // PWM Set-up on pin: A8
  REG_PIOB_ABSR |= PIO_ABSR_P17;                        // Set PWM pin perhipheral type A or B, in this case B
  REG_PIOB_PDR |= PIO_PDR_P17;                          // Set PWM pin to an output
  REG_PWM_CPRD0 = 304;                                // Set the PWM frequency 2MHz/(2 * 20000) = 50Hz
  REG_PWM_CDTY0 = 152;                                 // Set the PWM duty cycle to 1500 - centre the servo


  // PWM Set-up on pin: A9
  REG_PIOB_ABSR |= PIO_ABSR_P18;                        // Set PWM pin perhipheral type A or B, in this case B
  REG_PIOB_PDR |= PIO_PDR_P18;                          // Set PWM pin to an output
  REG_PWM_CPRD0 = 1216;                                // Set the PWM frequency 2MHz/(2 * 20000) = 50Hz
  REG_PWM_CDTY0 = 608;                                 // Set the PWM duty cycle to 1500 - centre the servo


  // PWM Set-up on pin: A10
  REG_PIOB_ABSR |= PIO_ABSR_P19;                        // Set PWM pin perhipheral type A or B, in this case B
  REG_PIOB_PDR |= PIO_PDR_P19;                          // Set PWM pin to an output
  REG_PWM_CPRD0 = 608;                                // Set the PWM frequency 2MHz/(2 * 20000) = 50Hz
  REG_PWM_CDTY0 = 304;                                 // Set the PWM duty cycle to 1500 - centre the servo



  // PWM Set-up on pin:
  REG_PIOC_ABSR |= PIO_ABSR_P22;                        // Set PWM pin perhipheral type A or B, in this case B
  REG_PIOC_PDR |= PIO_PDR_P22;                          // Set PWM pin to an output
  REG_PWM_CPRD0 = 608;                                // Set the PWM frequency 2MHz/(2 * 20000) = 50Hz
  REG_PWM_CDTY0 = 304;                                 // Set the PWM duty cycle to 1500 - centre the servo


  REG_PWM_ENA = PWM_ENA_CHID0;                          // Enable the PWM channel
}

void loop() {}

ard_newbie


In your code, you are programming the frequency and the duty cycle of PWM channel 0 several times...

If you want to start 5 PWM pulses at the same time(e.g. PWM channels 0, 1, 2, 3, 4), once you have selected the right values for each PWM_CPRD and PWM_CDTY, you enable all 5 channels:

PWM->PWM_ENA = PWM_ENA_CHID0 | PWM_ENA_CHID1 | .... | PWM_ENA_CHID4;

Crameroni

Thank you a lot!!!
I was unable to see it.
Thanks.

Now I have 5 independent frequencies =)

Go Up