-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTLC59731.ino
138 lines (114 loc) · 2.71 KB
/
TLC59731.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
/*
Copyright 2014 Jason Campbell, [email protected]
Licensed under GPL v2 or later
Date: 7/11/14
Purpose: TLC5973 LED Driver Test
Status: works
*/
byte ledPin=8;
int pTime = 1; // in useconds
int tCycle = pTime * 6; // time for one bit. Per datasheet tCycle is between 0.33 useconds and 10 useconds
int numLeds = 5; //number of LEDs
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
digitalWrite(A3, HIGH);
}
void loop() {
for(byte i=0; i<255; i++) {
writeLED(1,0,i,0);
delay(15);
}
for(byte i=0; i<255; i++) {
writeLED(1,0,0,i);
delay(5);
}
for(byte i=255; i>0; i--) {
writeLED(1,1,0,0);
delay(10);
}
}
void writeLED(byte ledNum, byte redValue, byte greenValue, byte blueValue) {
for (int i = 0; i< numLeds; i++) {
writeCommTimer();
if (i = ledNum){
writeCommand();
writeData(redValue);
writeData(greenValue);
writeData(blueValue);
}
waitEOS();
}
waitGSLAT();
}
void writeZero() {
PORTB |= B1;
delayMicroseconds(pTime);
PORTB &= B111110;
delayMicroseconds(pTime);
delayMicroseconds(pTime);
delayMicroseconds(pTime);
delayMicroseconds(pTime);
delayMicroseconds(pTime);
}
void writeOne() {
PORTB |= B1;
delayMicroseconds(pTime);
PORTB &= B111110;
delayMicroseconds(pTime);
PORTB |= B1; //rising edge of second pulse has to be within 0.5 * tCycle
delayMicroseconds(pTime);
PORTB &= B111110;
delayMicroseconds(pTime);
delayMicroseconds(pTime);
delayMicroseconds(pTime);
}
void writeCommTimer() {
//first two zeroes determine the timing (tcycle) after device is powered up or after a GSLAT
writeZero();
writeZero();
}
void writeCommand() {
//write command in hex is 3AA, binary is 11 1010 1010
writeOne();
writeOne();
writeOne();
writeZero();
writeOne();
writeZero();
writeOne();
writeZero();
writeOne();
writeZero();
}
//end of sequence (for a single driver chip)
void waitEOS() {
PORTB &= B111110;
delayMicroseconds(tCycle*4); // min 3.5 to max 5.5 times tCycle
}
//grayscale latch (for the end of a chain of driver chips)
void waitGSLAT() {
PORTB &= B111110;
delayMicroseconds(tCycle*10); // minimum 8 time tCycle
}
void writeData(byte data) {
for (byte i = 0; i<8; i++) {
if(data & B10000000) {writeOne();}
else{writeZero();}
data <<= 1;
}
//pad the end of each 8 bit number with four more
//bits (use 1010 because that's the end of the
//write command so it doesn't have to have an entirely
//separate function) to make a 12 bit number
writeZero();
writeZero();
writeZero();
writeZero();
}