#include "pico/stdlib.h"
#include "hardware/pwm.h"
#include "hardware/clocks.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define LED_R_PIN 12
#define LED_G_PIN 13
#define LED_B_PIN 11
#define BTN_A_PIN 5
#define BTN_B_PIN 6
void set_leds(bool red, bool green, bool blue);
void set_pwm_pin(uint pin, uint freq, uint duty_c);
int main(){
gpio_init(LED_R_PIN);
gpio_set_dir(LED_R_PIN, GPIO_OUT);
gpio_init(LED_G_PIN);
gpio_set_dir(LED_G_PIN, GPIO_OUT);
gpio_init(LED_B_PIN);
gpio_set_dir(LED_B_PIN, GPIO_OUT);
uint dutyc = 0;
while (1){
set_pwm_pin(LED_R_PIN,1000,dutyc);
set_pwm_pin(LED_B_PIN,10000,dutyc);
sleep_ms(2000);
dutyc=dutyc+500;
}
return 0;
}
void set_leds(bool red, bool green, bool blue){
gpio_put(LED_R_PIN, red);
gpio_put(LED_G_PIN, green);
gpio_put(LED_B_PIN, blue);
}
void set_pwm_pin(uint pin, uint freq, uint duty_c) { // duty_c between 0..10000
gpio_set_function(pin, GPIO_FUNC_PWM);
uint slice_num = pwm_gpio_to_slice_num(pin);
pwm_config config = pwm_get_default_config();
float div = (float)clock_get_hz(clk_sys) / (freq * 10000);
pwm_config_set_clkdiv(&config, div);
pwm_config_set_wrap(&config, 10000);
pwm_init(slice_num, &config, true); // start the pwm running according to the config
pwm_set_gpio_level(pin, duty_c); //connect the pin to the pwm engine and set the on/off level.
};