我正在编写一个基本的 LED 程序,以使用 STM32L0xx 芯片和 TI TLC59116F 在定制板上点亮 LED,但我在解释数据表时遇到了一些麻烦。
我使用 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);
}
然后在 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
}
编辑:这是一个更完整的代码示例,其中包含 HAL 错误检查等。