-
Notifications
You must be signed in to change notification settings - Fork 0
/
my9221.cc
47 lines (40 loc) · 1.36 KB
/
my9221.cc
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
// This is just cut and paste from my test code; I'll build out something a little better soon.
// This is using the Arduino libraries, but you get the idea.
class MY9221 {
private:
uint8_t clock_pin;
uint8_t data_pin;
public:
MY9221(uint8_t clock, uint8_t data) : clock_pin(clock), data_pin(data) {}
void init(uint8_t chain_len = 1) {
pinMode(clock_pin, OUTPUT);
digitalWrite(clock_pin, LOW);
pinMode(data_pin, OUTPUT);
digitalWrite(data_pin, LOW);
for (;chain_len > 0;chain_len--) {
// clock in zeros for every chip in the chain
for (int i = 0; i < 13; i++) shift_16(0);
}
latch();
}
void shift_16(uint16_t data) {
digitalWrite(clock_pin, LOW);
// 16 bits, both clock edges, LSB first
for (int i = 0; i < 8; i++) {
digitalWrite(data_pin, ((data & 0x8000) == 0) ? LOW:HIGH);
digitalWrite(clock_pin, HIGH);
digitalWrite(data_pin, ((data & 0x4000) == 0) ? LOW:HIGH);
digitalWrite(clock_pin, LOW);
data <<= 2;
}
}
void latch() {
digitalWrite(data_pin, LOW);
delayMicroseconds(240);
for (int i = 0; i < 4; i++) {
digitalWrite(data_pin, HIGH);
digitalWrite(data_pin, LOW);
}
delayMicroseconds(1);
}
};