Two Variable 20 kHz PWM outputs

Hey Guys, I am currently working to get TWO variable 20 kHz PWM outputs from my Arduino DUE. I am currently using a variable voltage device to control the duty cycle adjustment of my SINGULAR variable 20 kHz waveform. Though, I need to add a second PWM and I am struggling to do so. It will be variable based on its own separate variable voltage source. Both of these variable inputs are being brought through analog input pins. The majority of my code has been pulled from the forum post below.

Due PWM Frequency

Currently, my singular PWM signal is running on DAC1, though I need the second PWM signal to run on DAC0. It needs to run independently of the other.

int feedback = A0; //Adjustable Voltage Device
int val = 0;

void setup() {
  Serial.begin(9600);
  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_PIOD_ABSR |= PIO_ABSR_P15;
  REG_PIOB_PDR |= PIO_PDR_P16;                          // Set PWM pin to an output
  REG_PIOD_PDR  |= PIO_PDR_P15;
  //REG_PIOA_PDR |= PIO_PDR_P15;
  //REG_PIOA_PDR |= PIO_PDR_P15;
  REG_PWM_CLK = PWM_CLK_PREA(0) | PWM_CLK_DIVA(1);      // Set the PWM clock rate to 84MHz (84MHz/1) 
  REG_PWM_CMR0 = PWM_CMR_CPRE_CLKA;                     // Enable single slope PWM and set the clock source as CLKA
  REG_PWM_CPRD0 = 4200;  
  //REG_PWM_CDTY0 = 2100;                                // Set the PWM frequency 84MHz/40kHz = 2100                                // Set the PWM duty cycle 50% (2100/2=1050)
  REG_PWM_ENA = PWM_ENA_CHID0 | PWM_ENA_CHID1;                          // Enable the PWM channel     
  
}

void loop() {
 val = analogRead(feedback);
 Serial.println(val);
 REG_PWM_CDTY0 = (val-710)*13.9; //(val-330)*14;
 /*int leftMotorA2 = analogRead(A2);
 float leftMotorVolts = leftMotorA2 * (3.3/ 1023.0);
 Serial.println(leftMotorVolts);*/

}

Bump

Start by posting an annotated schematic showing how you have wired it, include all power, ground, and power supply connections. Links to technical information on the hardware devices helps a lot.

1 Like

Above is a very rough outline of my current system.

To expand on my original point, I am using the Adafruit Joysticks (Joysticks) to adjust the duty cycle output from the Arduino Due to operate two servo motors. I currently have one fully adjustable PWM signal running from the left joystick. This gives me a DC range from 0-100% just as intended on DAC1 which is P16 in the code above. It runs the servo motor flawlessly.

Above, you can see the duty cycle in Channel 1 and the voltage feedback of the joystick as Channel 2. As the voltage increases on Channel 2, the duty cycle increases proportionally.

Though my issue is initializing another fully adjustable PWM signal. I do not possess the knowledge required to initialize the second channel to utilize the second PWM. I have attempted to write the same duty cycle to the second pin DAC0 (P15 in the code above). Sadly, this did not work. Long story short, I have half the system working, though I do not understand the ATMEL syntax enough to get me further. I was hoping some people could point me in the right direction.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.