I'm a beginner and I wanted to use the ESP32 to drive 16 leds with TLC5927;
You find the datasheet here http://www.ti.com/lit/ds/symlink/tlc5926.pdf
I use the following code t read out the package but it doesn't work , I use SPI bus of ESP to communicate with the TL5927. To start, I just want to run one led
I searched in the technical documentation without success, can anyone help me please ?
Thank you
Code: Select all
#include "driver/periph_ctrl.h"
#include "driver/timer.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "timer.h"
//#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "spi_master.h"
#include "freertos/event_groups.h"
#define LED0 0b0000000000000000;
#define LED1 0b0000000000000001;
#define LED2 0b0000000000000010;
#define LED3 0b0000000000000100;
#define LED4 0b0000000000001000;
#define LED5 0b0000000000010000;
#define LED6 0b0000000000100000;
#define LED7 0b0000000001000000;
#define LED8 0b0000000010000000;
#define LED9 0b0000000100000000;
#define LED10 0b0000001000000000;
#define LED11 0b0000010000000000;
#define LED12 0b0000100000000000;
#define LED13 0b0001000000000000;
#define LED14 0b0010000000000000;
#define LED15 0b0100000000000000;
//define pins for SPI bus
#define PIN_NUM_MOSI 14
#define PIN_NUM_CLK 19
#define PIN_NUM_CS 22
#define PIN_NUM_OE 2
void spi_bus (void *arg){
spi_device_handle_t spi;
esp_err_t ret;
//################ INIT GPIO ################
gpio_set_direction(PIN_NUM_MOSI, GPIO_MODE_OUTPUT);
gpio_set_direction(PIN_NUM_CLK, GPIO_MODE_OUTPUT);
gpio_set_direction(PIN_NUM_CS, GPIO_MODE_OUTPUT);
spi_bus_config_t buscfg={
.miso_io_num=-1,
.mosi_io_num=PIN_NUM_MOSI,
.sclk_io_num=PIN_NUM_CLK,
.quadwp_io_num=-1,
.quadhd_io_num=-1
};
spi_device_interface_config_t devcfg={
.clock_speed_hz=10000000, //Clock out at 30 MHz
.mode=0, //Falling edge
.spics_io_num=PIN_NUM_CS, //CS pin
.queue_size=1, //We want to be able to queue 7 transactions at a time
.pre_cb = NULL,
.post_cb = NULL
};
//Initialize the SPI bus
ret = spi_bus_initialize(1, &buscfg, 1);
ESP_ERROR_CHECK(ret);
//Attach the TLC5927 to the SPI bus
ret = spi_bus_add_device(1, &devcfg, &spi);
ESP_ERROR_CHECK(ret);
uint data= NULL; //16 bit
spi_transaction_t trans_desc={
.address=0,
.command=0,
.flags=0,
.length=0,
.rxlength=NULL,
.tx_buffer=&data,
.rx_buffer=0,
};
while (1) {
gpio_set_level(PIN_NUM_OE, 1); //Output enable when is down
ESP_ERROR_CHECK(spi_device_transmit(spi, &trans_desc));
data = LED1;
spi_transaction_t t;
memset(&t, 0, sizeof(t)); //Zero out the transaction
t.length=16; //Transaction length
t.tx_buffer= &data; //Data
ret=spi_device_transmit(spi, &t); //Transmit!
assert(ret==ESP_OK); //Should have had no issues.
gpio_set_level(PIN_NUM_OE, 0); //Output enable
vTaskDelay(10/portTICK_RATE_MS);
gpio_set_level(PIN_NUM_OE, 1);
vTaskDelay(10000/portTICK_RATE_MS);
}
}
void app_main()
{
//nvs_flash_init();
//tcpip_adapter_init();
//############# GPIO INIT ##############
gpio_config_t gpioConfig;
gpioConfig.pin_bit_mask = (1 << 2);
gpioConfig.mode = GPIO_MODE_OUTPUT;
gpioConfig.pull_up_en = GPIO_PULLUP_DISABLE;
gpioConfig.pull_down_en = GPIO_PULLDOWN_DISABLE;
gpioConfig.intr_type = GPIO_INTR_DISABLE;
gpio_config(&gpioConfig);
xTaskCreate(spi_bus, "BUS SPI", 2048, NULL, 5, NULL);
}