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.
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);*/
}