[英]Does anyone have an STM32L0xx with TLC59116 I2C Example code?
I am writing a basic LED program to light LEDs on a custom board using an STM32L0xx chip and the TI TLC59116F, and I'm having some trouble interpreting the data sheet. 我正在编写一个基本的LED程序,以使用STM32L0xx芯片和TI TLC59116F在定制板上点亮LED,并且在解释数据表时遇到了一些麻烦。
I used STM32CubeMX to set up the initial pins and init I2C, and have come up with the following based on some examples I found. 我使用STM32CubeMX设置了初始引脚和初始化I2C,并根据我发现的一些示例提出了以下内容。
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "led.h"
/* Private variables ---------------------------------------------------------*/
void TLC59116_Init(void)
{
// Set default register values
TLC59116_WriteReg(TLC59116_MODE1, TLC59116_MODE1_DEFAULT);
TLC59116_WriteReg(TLC59116_MODE2, TLC59116_MODE2_DEFAULT);
// Set all PWM values to 0x00 (OFF)
uint8_t aTXBuffer[] = { TLC59116_PWM0_AUTOINCR, TLC59116_LEDOUT_OFF, TLC59116_LEDOUT_OFF,
TLC59116_LEDOUT_OFF, TLC59116_LEDOUT_OFF, TLC59116_LEDOUT_OFF, TLC59116_LEDOUT_OFF,
TLC59116_LEDOUT_OFF, TLC59116_LEDOUT_OFF, TLC59116_LEDOUT_OFF, TLC59116_LEDOUT_OFF,
TLC59116_LEDOUT_OFF, TLC59116_LEDOUT_OFF, TLC59116_LEDOUT_OFF, TLC59116_LEDOUT_OFF,
TLC59116_LEDOUT_OFF, TLC59116_LEDOUT_OFF };
TLC59116_WriteStream(sizeof(aTXBuffer), aTXBuffer);
// Set all LEDs to PWM Control
TLC59116_WriteReg(TLC59116_LEDOUT0, TLC59116_LEDOUT_ON);
TLC59116_WriteReg(TLC59116_LEDOUT1, TLC59116_LEDOUT_ON);
TLC59116_WriteReg(TLC59116_LEDOUT2, TLC59116_LEDOUT_ON);
TLC59116_WriteReg(TLC59116_LEDOUT3, TLC59116_LEDOUT_ON);
}
static void TLC59116_WriteReg(uint8_t reg, uint8_t val)
{
uint8_t aTXBuffer[] = { reg, val };
while(HAL_I2C_Master_Transmit(&hi2c1, TLC59116_ADDR, aTXBuffer, sizeof(aTXBuffer), 100) != HAL_OK)
{
if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF)
{
Error_Handler();
}
}
}
static void TLC59116_WriteStream(uint8_t len, uint8_t *pData)
{
if (len == 0)
{
Error_Handler();
}
while(HAL_I2C_Master_Transmit(&hi2c1, TLC59116_ADDR, pData, len, 100) != HAL_OK)
{
if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF)
{
Error_Handler();
}
}
}
void TLC59116_LED(uint8_t led, uint8_t state)
{
if ((led < 0) || (led > 15)) {
Error_Handler();
} else {
TLC59116_WriteReg(led + TLC59116_PWM0, state);
}
}
void TLC59116_SLEEP(void)
{
TLC59116_WriteReg(TLC59116_MODE1, TLC59116_MODE1_SLEEP);
}
void TLC59116_WAKE(void)
{
TLC59116_WriteReg(TLC59116_MODE1, TLC59116_MODE1_DEFAULT);
HAL_Delay(5);
}
Then in main.c 然后在main.c中
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
MX_USB_PCD_Init();
TLC59116_Init();
TLC59116_LED(TLC59116_PWM5, 0x01); // Sets LED 5 to ON
}
Edit: Here is a more complete code example with HAL error checking and such. 编辑:这是带有HAL错误检查等的更完整的代码示例。
Assuming you have STM32CubeMX generate the correct initialization for your I2C bus, then you can assess the TLC59116 using the polling method you referenced: 假设STM32CubeMX为I2C总线生成了正确的初始化,那么您可以使用引用的轮询方法评估TLC59116:
HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
Value for setting DevAddress is defined on page 23 of the datasheet, you will have to review your schematic and set bit 0 to 0 when writing a register. 数据表的第23页上定义了用于设置DevAddress的值,您必须查看原理图并在写入寄存器时将位0设置为0。 Assuming A[3:0] = 0x0 on the board, DevAddress would become: 假设板上的A [3:0] = 0x0,DevAddress将变为:
Next, you need to setup the payload which contains the register address and register value to be set. 接下来,您需要设置包含要设置的寄存器地址和寄存器值的有效负载。 For example (see page 18 of the datasheet): 例如(参见数据表第18页):
uint8_t aTxBuffer[2] = {};
aTxBuffer[0] = 0x14; /* LEDOUT0 register */
aTxBuffer[1] = 0x05; /* Turn on LED 0 and LED 1 */
HAL_I2C_Master_Transmit(hi2c1, writeAddr, aTxBuffer, sizeof(aTxBuffer), 100);
However, be sure to review MODE1 register before implementing the above. 但是,在执行上述操作之前,请务必检查MODE1寄存器。 You need to clear the OSC bit or you will never light an LED. 您需要清除OSC位,否则将永远不会点亮LED。
uint8_t aTxBuffer[2] = {};
aTxBuffer[0] = 0x00; /* MODE1 register */
aTxBuffer[1] = 0x00; /* Example only, review each bit */
HAL_I2C_Master_Transmit(hi2c1, writeAddr, aTxBuffer, sizeof(aTxBuffer), 100);
The above illustrates writing to a single register, to stream data into multiple register locations: 上面说明了写入单个寄存器以将数据流式传输到多个寄存器位置的过程:
void TLC59116_WriteStream(uint8_t len, uint8_t *pData)
{
if (len == 0)
{
ErrorHandler();
}
HAL_I2C_Master_Transmit(&h12c1, TLC59116_ADDR, pData, len, 100);
}
Where TLC59116_ADDR = 0xC0
and pData points to the base address of your message to be sent to TI device. 其中TLC59116_ADDR = 0xC0
,pData指向要发送到TI设备的消息的基址。
To set all 16 PWM registers to a 0x00 or off: 要将所有16个PWM寄存器设置为0x00或关闭:
uint8_t aTXBuffer[] = { TLC59116_PWM0_AUTOINCR, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
TLC59116_WriteStream(sizeof(aTXBuffer), aTXBuffer);
Where TLC59116_PWM0_AUTOINCR = 0x82;
其中TLC59116_PWM0_AUTOINCR = 0x82;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.