/*
** Raspberry Pi Pico SDK PWM Example
** Originally from Pico PMW Example, simplified
** When you stop the simulation, Wokwi downloads as .vcp
** file that can be viewed in [PulseView](https://sigrok.org/wiki/PulseView)
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/pwm.h"
#define GPIO_SPEAKER 3
#define MAX_DUTY_CYCLE 100 // percent
int main() {
int change = 5;
int dutyPercent = 0;
float clkdiv = 255.0;
float frequencyDividerChange = 30.0;
stdio_init_all(); // UART setup for both input and output
// PWM setup
gpio_set_function(GPIO_SPEAKER, GPIO_FUNC_PWM);
uint slice_num = pwm_gpio_to_slice_num(GPIO_SPEAKER);
pwm_set_clkdiv(slice_num, clkdiv);
pwm_set_wrap(slice_num, MAX_DUTY_CYCLE);
pwm_set_enabled(slice_num, true);
while (1) {
dutyPercent += change;
if (dutyPercent >= MAX_DUTY_CYCLE) {
dutyPercent = 0;
if (frequencyDividerChange > 0) {
clkdiv -= frequencyDividerChange;
pwm_set_clkdiv(slice_num, clkdiv);
}
} // when at max, go directly back to zero
pwm_set_gpio_level(GPIO_SPEAKER, dutyPercent);
printf("LED is now %d %% and freq divider %0.1f\r\n", dutyPercent, clkdiv);
sleep_ms(200);
}
}