/*
Raspberry Pi Pico SDK PWM Example
Use the logic analyzer to view the signals: https://docs.wokwi.com/guides/logic-analyzer
*/
#include "pico/stdlib.h"
#include "hardware/pwm.h"
int main() {
gpio_set_function(0, GPIO_FUNC_PWM);
gpio_set_function(1, GPIO_FUNC_PWM);
// Find out which PWM slice is connected to GPIO 0 (it's slice 0)
uint slice_num = pwm_gpio_to_slice_num(0);
pwm_set_clkdiv(slice_num, 125); // PWM clock should now be running at 1MHz
pwm_set_wrap(slice_num, 1023); // Set period of 1024 cycles (0 to 1023 inclusive)
pwm_set_enabled(slice_num, true);
int duty = 0;
int direction = 1;
while (1) {
duty += 50 * direction;
if (duty >= 1024) {
duty = 1024;
direction = -1;
}
if (duty <= 0) {
duty = 0;
direction = 1;
}
pwm_set_chan_level(slice_num, PWM_CHAN_A, 1024 - duty);
pwm_set_chan_level(slice_num, PWM_CHAN_B, duty);
sleep_ms(100);
}
}