-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino-ws2812-direct-spi-control.ino
94 lines (74 loc) · 1.74 KB
/
arduino-ws2812-direct-spi-control.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
#include<SPI.h>
/**
MCU pin D11 MOSI = LED STRIP DATA pin
Tested on Arduino Nano
8Mhz SPI_CLOCK_DIV2
one 0xF8
zero 0xC0
4Mhz SPI_CLOCK_DIV4
one 1 or 4
zero 2 or 6
you can use hex dec binary converter online
one
0b1 1
0b10 2
0b11 3
0b100 4
0b110 6
0b1000 8
0b1110 14
0b11000000 192 0xC0
0b11111000 248 0xF8
**/
#define number_of_LEDs 9
#define number_of_bits 24
#define one 0xF8
#define zero 0xC0
void setup (void) {
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1);
SPI.setClockDivider(SPI_CLOCK_DIV2); //Sets clock for SPI communication at 2 (16/2=8Mhz)
delay(10);
WS2812_reset();
}
void loop(void) {
//All RED
WS2812_set_common_colour_on_all_LEDs(200, 0, 0);
delay(600);
//All GREEN
WS2812_set_common_colour_on_all_LEDs(0, 200, 0);
delay(600);
//All BLUE
WS2812_set_common_colour_on_all_LEDs(0, 0, 200);
delay(600);
}
void WS2812_send_data(unsigned char r, unsigned char g, unsigned char b) {
unsigned char s = number_of_bits;
unsigned long value = 0x00000000;
value = (((unsigned long)g << 16) | ((unsigned long)r << 8) | ((unsigned long)b));
while (s > 0) {
if ((value & 0x800000) != LOW) {
SPI.transfer(one);
asm("nop");
asm("nop");
} else {
SPI.transfer(zero);
}
value <<= 1;
s--;
}
}
void WS2812_set_common_colour_on_all_LEDs(unsigned char r, unsigned char g, unsigned char b) {
unsigned int n = 0x0000;
for (n = 0; n < number_of_LEDs; n++) {
WS2812_send_data(r, g, b);
}
WS2812_update();
}
void WS2812_update() {
delayMicroseconds(60);
}
void WS2812_reset() {
WS2812_set_common_colour_on_all_LEDs(0, 0, 0);
}