//Nome:Estrela José Marcolino
#include "pico/stdlib.h"
#include "hardware/pwm.h"
#include "hardware/irq.h"
#define LED_R_PIN 12//red
#define LED_G_PIN 13//green
#define LED_B_PIN 11//blue
#define BTN_1_PIN 5
#define BTN_2_PIN 6
volatile uint16_t duty_cycle_red = 5; // Duty cycle inicial do LED vermelho
volatile bool red_enabled = true; // Estado do LED vermelho
volatile bool blue_enabled = true; // Estado do LED azul
// Função para configurar o PWM em um pino
void setup_pwm(uint pin, uint freq_hz) {
gpio_set_function(pin, GPIO_FUNC_PWM);
uint slice_num = pwm_gpio_to_slice_num(pin);
uint clk_div = 125000000 / (freq_hz * 65536);
pwm_config config = pwm_get_default_config();
pwm_config_set_clkdiv(&config, clk_div);
pwm_init(slice_num, &config, true);
}
// Callback para atualizar o duty cycle do LED vermelho
bool update_red_pwm(struct repeating_timer *t) {
if (red_enabled) {
uint slice_num = pwm_gpio_to_slice_num(LED_R_PIN);
pwm_set_gpio_level(LED_R_PIN, (duty_cycle_red * 65536) / 100);
duty_cycle_red += 5;
if (duty_cycle_red > 100) {
duty_cycle_red = 5;
}
} else {
pwm_set_gpio_level(LED_R_PIN, 0); // Desliga o LED vermelho
}
return true;
}
// Função para tratar interrupções dos botões
void gpio_callback(uint gpio, uint32_t events) {
if (gpio == BTN_1_PIN) {
red_enabled = !red_enabled; // Alterna o LED vermelho
} else if (gpio == BTN_2_PIN) {
blue_enabled = !blue_enabled; // Alterna o LED azul
uint slice_num_blue = pwm_gpio_to_slice_num(LED_B_PIN);
pwm_set_gpio_level(LED_B_PIN, blue_enabled ? (50 * 65536) / 100 : 0); // Liga/desliga LED azul
}
}
int main() {
stdio_init_all();
// Configura PWM para LEDs
setup_pwm(LED_R_PIN, 1000); // LED vermelho: 1 kHz
setup_pwm(LED_B_PIN, 10000); // LED azul: 10 kHz
// Configura duty cycle inicial dos LEDs
pwm_set_gpio_level(LED_R_PIN, (duty_cycle_red * 65536) / 100);
pwm_set_gpio_level(LED_B_PIN, (50 * 65536) / 100); // 50% inicial
// Configura um timer para atualizar o PWM do LED vermelho
struct repeating_timer timer;
add_repeating_timer_ms(2000, update_red_pwm, NULL, &timer);
// Configura os botões como entrada com pull-up
gpio_init(BTN_1_PIN);
gpio_set_dir(BTN_1_PIN, GPIO_IN);
gpio_pull_up(BTN_1_PIN);
gpio_init(BTN_2_PIN);
gpio_set_dir(BTN_2_PIN, GPIO_IN);
gpio_pull_up(BTN_2_PIN);
// Configura interrupções para os botões
gpio_set_irq_enabled_with_callback(BTN_1_PIN, GPIO_IRQ_EDGE_FALL, true, gpio_callback);
gpio_set_irq_enabled_with_callback(BTN_2_PIN, GPIO_IRQ_EDGE_FALL, true, gpio_callback);
// Loop principal
while (1) {
tight_loop_contents();
}
}