LED_shit/main/display.c

68 lines
1.6 KiB
C
Raw Normal View History

2025-11-23 18:15:56 +00:00
#include "driver/i2c.h"
#include "esp_log.h"
#include "display.h"
#define DISP_ADDR 0x70
#define I2C_PORT I2C_NUM_0
// Tabela de segmentos para 09
static const uint8_t segtbl[10] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
void display_init(void)
{
// Oscillator ON
uint8_t cmd1 = 0x21;
i2c_master_write_to_device(I2C_PORT, DISP_ADDR, &cmd1, 1, 20 / portTICK_PERIOD_MS);
// Display ON, no blink
uint8_t cmd2 = 0x81;
i2c_master_write_to_device(I2C_PORT, DISP_ADDR, &cmd2, 1, 20 / portTICK_PERIOD_MS);
// Brilho máximo
uint8_t cmd3 = 0xEF;
i2c_master_write_to_device(I2C_PORT, DISP_ADDR, &cmd3, 1, 20 / portTICK_PERIOD_MS);
display_clear();
}
void display_clear(void)
{
uint8_t buf[17] = {0};
buf[0] = 0x00;
i2c_master_write_to_device(I2C_PORT, DISP_ADDR, buf, sizeof(buf), 20 / portTICK_PERIOD_MS);
}
void display_digit(int pos, uint8_t val)
{
if (pos < 0 || pos > 3) return;
if (val > 9) val = 0;
uint8_t buf[2];
buf[0] = pos * 2; // endereço interno do dígito
buf[1] = segtbl[val]; // segmentos da tabela
i2c_master_write_to_device(I2C_PORT, DISP_ADDR, buf, 2, 20 / portTICK_PERIOD_MS);
}
void display_number(int num)
{
if (num < 0) num = 0;
if (num > 9999) num = 9999;
display_digit(3, num % 10);
display_digit(2, (num / 10) % 10);
display_digit(1, (num / 100) % 10);
display_digit(0, (num / 1000) % 10);
}