/*
Raspberry Pi Pico SDK PWM Example
https://wokwi.com/projects/326847710095213139
Generates two 250khZ PWM signal (1 MHz clock, 4 cycle period) on pins 0, 1
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() {
// Tell GPIO 0 and 1 they are allocated to the PWM
gpio_set_function(0, GPIO_FUNC_PWM);
gpio_set_function(2, GPIO_FUNC_PWM);
// Find out which PWM slice is connected to GPIO 0 (it's slice 0)
uint slice_num_0 = pwm_gpio_to_slice_num(0);
//pwm_set_clkdiv(slice_num_0, 125); // pwm clock should now be running at 1MHz
pwm_set_clkdiv(slice_num_0, 125);
// Set period N+1
pwm_set_wrap(slice_num_0, 1023);
// Set channel A output high for M cycle before dropping
pwm_set_chan_level(slice_num_0, PWM_CHAN_A, 128);
// Find out which PWM slice is connected to GPIO 3 (it's slice 1)
uint slice_num_1 = pwm_gpio_to_slice_num(3);
pwm_set_clkdiv(slice_num_1, 125); // pwm clock should now be running at 1MHz
// Set period N+1
pwm_set_wrap(slice_num_1, 1023);
// Set channel A output high for M cycle before dropping
pwm_set_chan_level(slice_num_1, PWM_CHAN_A, 256);
// Set the PWM running
pwm_set_enabled(slice_num_0, true);
pwm_set_enabled(slice_num_1, true);
// Note we could also use pwm_set_gpio_level(gpio, x) which looks up the
// correct slice and channel for a given GPIO.
while (1) {
sleep_ms(250);
pwm_advance_count(slice_num_1); // НЕ РАБОТАЕТ. Наверно не реализовано в симуляторе.
}
}