#include <stdio.h>
#include <stdint.h>
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define DATA_PIN 23 // SI (MOSI)
#define CLOCK_PIN 18 // SCK
#define LATCH_PIN 5 // RCK
spi_device_handle_t spi;
void init_pins() {
gpio_set_direction(LATCH_PIN, GPIO_MODE_OUTPUT);
}
void init_spi() {
spi_bus_config_t buscfg = {
.miso_io_num = -1,
.mosi_io_num = DATA_PIN,
.sclk_io_num = CLOCK_PIN,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
};
spi_device_interface_config_t devcfg = {
.clock_speed_hz = 1000000, // 1 MHz
.mode = 0, // SPI mode 0
.spics_io_num = -1, // No CS pin
.queue_size = 1,
};
spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO);
spi_bus_add_device(SPI2_HOST, &devcfg, &spi);
}
void send_color(uint8_t color) {
spi_transaction_t trans = {
.length = 8, // Transaction length is in bits
.tx_buffer = &color,
};
gpio_set_level(LATCH_PIN, 0); // Set latch pin low
spi_device_transmit(spi, &trans); // Transmit data via SPI
gpio_set_level(LATCH_PIN, 1); // Set latch pin high
}
void app_main() {
init_pins();
init_spi();
while (1) {
// Example colors to send (lighting up LEDs connected to the outputs)
uint8_t blue = 0b00000011; // Turn on blue
uint8_t red = 0b00000110; // Turn on red
uint8_t green = 0b00000101; // Turn on green
// Turn on blue
send_color(blue);
printf("Blue color ON\n");
vTaskDelay(1000 / portTICK_PERIOD_MS); // Wait for 1 second
// Turn on red
send_color(red);
printf("Red color ON\n");
vTaskDelay(1000 / portTICK_PERIOD_MS); // Wait for 1 second
// Turn on green
send_color(green);
printf("Green color ON\n");
vTaskDelay(1000 / portTICK_PERIOD_MS); // Wait for 1 second
// Turn on all colors (white)
send_color(red | green | blue);
printf("White ON\n");
vTaskDelay(1000 / portTICK_PERIOD_MS); // Wait for 1 second
// Turn off all colors
send_color(0);
printf("All colors OFF\n");
vTaskDelay(1000 / portTICK_PERIOD_MS); // Wait for 1 second
}
}
Loading
esp32-c6-devkitc-1
esp32-c6-devkitc-1