TLC6983驱动程序
时间: 2023-08-24 14:07:11 浏览: 75
TC6963的驱动程序
TLC6983是一种LED驱动器,可用于控制LED灯条或其他LED应用。以下是一个简单的TLC6983驱动程序示例,可以用于Arduino平台:
```
#include <SPI.h>
//定义TLC6983寄存器地址
#define REG_CONTROL 0x00
#define REG_AUTO_INCREMENT 0x80
#define REG_PWM0 0x01
#define REG_PWM1 0x02
#define REG_PWM2 0x03
#define REG_PWM3 0x04
#define REG_PWM4 0x05
#define REG_PWM5 0x06
#define REG_PWM6 0x07
#define REG_PWM7 0x08
//定义TLC6983控制寄存器位
#define CONTROL_EXTCLK 0x80
#define CONTROL_OUTNE 0x40
#define CONTROL_OUTPE 0x20
#define CONTROL_DSPRPT 0x10
#define CONTROL_BLANK 0x08
#define CONTROL_OUTTMG 0x04
#define CONTROL_FUNCT 0x02
#define CONTROL_NOSYNC 0x01
//定义TLC6983预分频器值
#define PRESCALER_1 0x00
#define PRESCALER_2 0x01
#define PRESCALER_4 0x02
#define PRESCALER_8 0x03
#define PRESCALER_16 0x04
#define PRESCALER_32 0x05
#define PRESCALER_64 0x06
#define PRESCALER_128 0x07
//定义TLC6983时钟分频比
#define CLK_DIV_1 0x00
#define CLK_DIV_2 0x01
#define CLK_DIV_3 0x02
#define CLK_DIV_4 0x03
#define CLK_DIV_5 0x04
#define CLK_DIV_6 0x05
#define CLK_DIV_7 0x06
#define CLK_DIV_8 0x07
#define CLK_DIV_9 0x08
#define CLK_DIV_10 0x09
#define CLK_DIV_11 0x0A
#define CLK_DIV_12 0x0B
#define CLK_DIV_13 0x0C
#define CLK_DIV_14 0x0D
#define CLK_DIV_15 0x0E
#define CLK_DIV_16 0x0F
//定义TLC6983引脚定义
#define TLC6983_CS 10
#define TLC6983_BLANK 9
#define TLC6983_XLAT 8
//定义TLC6983参数
#define TLC6983_NUM_CHANNELS 8
#define TLC6983_BRIGHTNESS 4095
//定义TLC6983控制寄存器值
byte control = CONTROL_OUTNE | CONTROL_DSPRPT | CONTROL_FUNCT;
//定义TLC6983 PWM寄存器值
int pwm_values[TLC6983_NUM_CHANNELS] = {0,0,0,0,0,0,0,0};
//定义TLC6983函数
void setup_tlc6983() {
pinMode(TLC6983_CS, OUTPUT);
pinMode(TLC6983_BLANK, OUTPUT);
pinMode(TLC6983_XLAT, OUTPUT);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV4);
digitalWrite(TLC6983_CS, HIGH);
}
void set_tlc6983_pwm(int channel, int value) {
if (value < 0) value = 0;
if (value > TLC6983_BRIGHTNESS) value = TLC6983_BRIGHTNESS;
pwm_values[channel] = value;
}
void update_tlc6983() {
digitalWrite(TLC6983_CS, LOW);
SPI.transfer(REG_CONTROL | REG_AUTO_INCREMENT);
SPI.transfer(control);
for (int i = TLC6983_NUM_CHANNELS-1; i >= 0; i--) {
SPI.transfer(pwm_values[i] >> 8);
SPI.transfer(pwm_values[i] & 0xFF);
}
digitalWrite(TLC6983_XLAT, HIGH);
digitalWrite(TLC6983_XLAT, LOW);
digitalWrite(TLC6983_CS, HIGH);
}
void blank_tlc6983() {
digitalWrite(TLC6983_BLANK, HIGH);
}
void unblank_tlc6983() {
digitalWrite(TLC6983_BLANK, LOW);
}
```
该程序使用SPI接口与TLC6983通信,并实现了设置PWM值和更新TLC6983的函数。在Arduino主程序中,可以使用以下代码初始化TLC6983并设置PWM值:
```
void setup() {
setup_tlc6983();
for (int i = 0; i < TLC6983_NUM_CHANNELS; i++) {
set_tlc6983_pwm(i, TLC6983_BRIGHTNESS/2);
}
update_tlc6983();
}
void loop() {
//将PWM值设置为随机值
for (int i = 0; i < TLC6983_NUM_CHANNELS; i++) {
set_tlc6983_pwm(i, random(TLC6983_BRIGHTNESS));
}
update_tlc6983();
delay(1000);
}
```
在主程序中,可以调用set_tlc6983_pwm函数将PWM值设置为所需的值,然后使用update_tlc6983函数将更改发送到TLC6983。
阅读全文