#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/pwm.h"
#include "hardware/irq.h"
const uint LED_RED = 13;
const uint LED_GREEN = 11;
const uint LED_BLUE = 12;
volatile uint led_level = 50;
const uint PERIOD = 1000;
const float DIVIDER = 125;
bool callback_function(struct repeating_timer *timer) {
printf("Level %d, Duty Cycle: %.0f%% \n", led_level, (1.0*led_level/PERIOD)*100.0);
pwm_set_gpio_level(LED_RED, led_level);
pwm_set_gpio_level(LED_GREEN, led_level);
led_level += 50;
if (led_level >= PERIOD) {
led_level = 50;
}
return true;
}
void configure_pwm_frequency(uint gpio, uint multiplier) {
gpio_set_function(gpio, GPIO_FUNC_PWM);
uint slice = pwm_gpio_to_slice_num(gpio);
pwm_set_clkdiv(slice, DIVIDER/multiplier);
pwm_set_wrap(slice, PERIOD);
pwm_set_enabled(slice, true);
}
int main() {
stdio_init_all();
uint another_level = 0;
bool direction = true;
configure_pwm_frequency(LED_BLUE, 100);
configure_pwm_frequency(LED_RED, 1);
configure_pwm_frequency(LED_GREEN, 10);
struct repeating_timer timer;
add_repeating_timer_ms(2000, callback_function, NULL, &timer);
while (true) {
pwm_set_gpio_level(LED_BLUE, another_level);
if (direction){
another_level+= 100;
}else {
another_level-= 100;
}
if (another_level >= PERIOD) {
direction = false;
}
if (another_level <= 1) {
direction = true;
}
sleep_ms(50);
}
}Loading
pi-pico-w
pi-pico-w