#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/pwm.h"
// Definições de constantes para o controle do PWM
const uint LED_RED = 12; // Pino do LED vermelho
const uint LED_BLUE = 14; // Pino do LED azul
const uint16_t PERIOD_RED = 1000; // Período do PWM do LED vermelho (1kHz)
const uint16_t PERIOD_BLUE = 100; // Período do PWM do LED azul (10kHz)
const float DIVIDER_PWM = 125.0; // Divisor do clock para o PWM
#define BUTTON_A 28
#define BUTTON_B 27
#define DEBOUNCE_DELAY_MS 200
volatile uint16_t duty_cycle_red = 50; // Duty cycle inicial do LED vermelho (5%)
volatile uint32_t last_debounce_time[2] = {0, 0};
volatile bool button_a_pressed = false;
volatile bool button_b_pressed = false;
volatile bool auto_increment_enabled = false;
volatile uint32_t last_increment_time = 0;
void setup_pwm(uint pin, uint16_t period)
{
uint slice = pwm_gpio_to_slice_num(pin);//Obtem slice do pwm associado ao pino
gpio_set_function(pin, GPIO_FUNC_PWM);// seta o pino pra pwm
pwm_set_clkdiv(slice, DIVIDER_PWM);// divisor de clock
pwm_set_wrap(slice, period - 1);// period do pwm
pwm_set_enabled(slice, true);// habilita pwm no slice corresponente
}
/*debounce dos botoes A e B*/
void button_callback(uint gpio, uint32_t events)
{
uint32_t now = to_ms_since_boot(get_absolute_time());
if (gpio == BUTTON_A && ((now - last_debounce_time[0]) > DEBOUNCE_DELAY_MS))
{
last_debounce_time[0] = now;
button_a_pressed = true;
}
if (gpio == BUTTON_B && ((now - last_debounce_time[1]) > DEBOUNCE_DELAY_MS))
{
last_debounce_time[1] = now;
button_b_pressed = true;
}
}
void increment_duty_cycle()
{
duty_cycle_red += 50; // Incremento de 5%
if (duty_cycle_red > PERIOD_RED)
duty_cycle_red = 50; // Retorna a 5%
pwm_set_gpio_level(LED_RED, duty_cycle_red);// Imprime o duty cycle atual do LED vermelho como uma porcentagem
printf("Duty cycle do LED vermelho: %d%%\n", (duty_cycle_red * 100) / PERIOD_RED);
// Calcula a porcentagem dividindo duty_cycle_red por PERIOD_RED e multiplicando por 100
}
int main()
{
stdio_init_all();
// Configurar PWM para os LEDs
setup_pwm(LED_RED, PERIOD_RED);
setup_pwm(LED_BLUE, PERIOD_BLUE);
//LEDs desligados de inicio
pwm_set_gpio_level(LED_RED, 0);
pwm_set_gpio_level(LED_BLUE, 0);
// Configurar botões
gpio_init(BUTTON_A);
gpio_set_dir(BUTTON_A, GPIO_IN);
gpio_pull_up(BUTTON_A);
gpio_set_irq_enabled_with_callback(BUTTON_A, GPIO_IRQ_EDGE_FALL, true, &button_callback);
gpio_init(BUTTON_B);
gpio_set_dir(BUTTON_B, GPIO_IN);
gpio_pull_up(BUTTON_B);
gpio_set_irq_enabled_with_callback(BUTTON_B, GPIO_IRQ_EDGE_FALL, true, &button_callback);
while (true)
{
uint32_t now = to_ms_since_boot(get_absolute_time());
if (button_a_pressed)
{
button_a_pressed = false;
printf("Botão A pressionado\n");
pwm_set_gpio_level(LED_RED, PERIOD_RED / 2); // 50% duty cycle a 1kHz
pwm_set_gpio_level(LED_BLUE, 0); // Desliga o LED azul
auto_increment_enabled = false;
printf("LED vermelho ligado a 1kHz (50%% duty cycle)\n");
}
if (button_b_pressed)
{
button_b_pressed = false;
printf("Botão B pressionado\n");
auto_increment_enabled = true;
last_increment_time = now;
increment_duty_cycle();
pwm_set_gpio_level(LED_BLUE, PERIOD_BLUE / 2); // 50% duty cycle a 10kHz
printf("LED azul ligado a 10kHz (50%% duty cycle)\n");
printf("Incremento automático ativado\n");
}
if (auto_increment_enabled && (now - last_increment_time >= 2000))
{
last_increment_time = now;
increment_duty_cycle();
}
sleep_ms(10);
}
return 0; // Nunca alcançada
}