Skip to content

Instantly share code, notes, and snippets.

@je4npw
Created April 28, 2026 21:05
Show Gist options
  • Select an option

  • Save je4npw/7bdc9f9c95cda2e80b1f49028aa1d5b4 to your computer and use it in GitHub Desktop.

Select an option

Save je4npw/7bdc9f9c95cda2e80b1f49028aa1d5b4 to your computer and use it in GitHub Desktop.
Relatório de aula prática - Sistemas Embarcados - 7º semestre - Ciência da Computação - Aluno Jean Patrick Wilhvock
#include <stdio.h>
#include <stdbool.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "driver/ledc.h"
#define LED_PIN GPIO_NUM_2
#define BTN_PIN GPIO_NUM_4
#define LEDC_TIMER LEDC_TIMER_0
#define LEDC_MODE LEDC_LOW_SPEED_MODE
#define LEDC_CHANNEL LEDC_CHANNEL_0
#define LEDC_DUTY_RESOLUTION LEDC_TIMER_10_BIT
#define LEDC_FREQUENCY 5000
#define MAX_DUTY 1023
volatile int nivel_brilho = 0;
int niveis_pwm[6] = {
0, // 0%
205, // 20%
409, // 40%
614, // 60%
818, // 80%
1023 // 100%
};
void configurar_pwm(void)
{
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_MODE,
.timer_num = LEDC_TIMER,
.duty_resolution = LEDC_DUTY_RESOLUTION,
.freq_hz = LEDC_FREQUENCY,
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&ledc_timer);
ledc_channel_config_t ledc_channel = {
.speed_mode = LEDC_MODE,
.channel = LEDC_CHANNEL,
.timer_sel = LEDC_TIMER,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = LED_PIN,
.duty = 0,
.hpoint = 0
};
ledc_channel_config(&ledc_channel);
}
void aplicar_brilho(void)
{
int duty = niveis_pwm[nivel_brilho];
ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, duty);
ledc_update_duty(LEDC_MODE, LEDC_CHANNEL);
printf("Nivel: %d | Duty: %d\n", nivel_brilho, duty);
}
void vTaskBotao(void *pvParameters)
{
int estado_anterior = 1;
while (1) {
int estado_atual = gpio_get_level(BTN_PIN);
if (estado_anterior == 1 && estado_atual == 0) {
nivel_brilho++;
if (nivel_brilho > 5) {
nivel_brilho = 0;
}
aplicar_brilho();
vTaskDelay(pdMS_TO_TICKS(200));
}
estado_anterior = estado_atual;
vTaskDelay(pdMS_TO_TICKS(20));
}
}
void app_main(void)
{
gpio_reset_pin(BTN_PIN);
gpio_set_direction(BTN_PIN, GPIO_MODE_INPUT);
gpio_set_pull_mode(BTN_PIN, GPIO_PULLUP_ONLY);
configurar_pwm();
aplicar_brilho();
xTaskCreate(
vTaskBotao,
"Task_Botao",
2048,
NULL,
5,
NULL
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment