I'd like to generate two PWM signals with SAME duty cycle (50%) but DIFFERENT frequencies on raspberry pi 4B. The difference between the frequencies is within +/-80%. The two GPIOs I'll use are GPIO 18 and 19.
I also would like to have a range of frequencies too while time passing by. For example: f1 is freq of PWM channel 1 and f0 is freq of PWM channel 0.
At t = 0sec, f1=1000Hz, f0=1800Hz
t=1sec, f1 = 30Hz, f0=20Hz
t=2sec, f1 = 10Hz, f0=10Hz
...
I'm using wiringPi lib. So far I'm able to achieve variable frequency by changing the parameter "clock" using pwmSetClock( )
. However, this function changes the PWM frequency of both channel 0 and 1. How can I set the frequencies separately for the two channels? I also thought about changing the "range" parameter using pwmSetRange()
. However that seems changing both channels frequencies too.
I attached my code if that helps. Thank you.
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define N 10
int main (void) {
if (wiringPiSetupGpio() == -1)
exit (1) ;
int clock_scale[N] = {4095, 2000, 1000, 500, 200, 100, 50, 20, 10, 8};
int range = 4095, duty_cycle = 2047;
int pin_HW_PWM = 18;
pinMode(pin_HW_PWM,PWM_OUTPUT);
pwmSetMode(PWM_MODE_MS);
pwmSetRange (range);
pwmWrite(pin_HW_PWM, duty_cycle);
while(1) {
for(int i = 0; i < N; ++i) {
pwmSetClock(clock_scale[i]);
printf("clock_scale[%d] = %d\n", i, clock_scale[i]);
delay(5000);
}
}
return 0;
}