-
Notifications
You must be signed in to change notification settings - Fork 6
/
max7219.h
113 lines (100 loc) · 2.33 KB
/
max7219.h
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
// MAX7219 functions by Pawel A. Hernik
// MAX7219 commands:
#define CMD_NOOP 0
#define CMD_DIGIT0 1
#define CMD_DIGIT1 2
#define CMD_DIGIT2 3
#define CMD_DIGIT3 4
#define CMD_DIGIT4 5
#define CMD_DIGIT5 6
#define CMD_DIGIT6 7
#define CMD_DIGIT7 8
#define CMD_DECODEMODE 9
#define CMD_INTENSITY 10
#define CMD_SCANLIMIT 11
#define CMD_SHUTDOWN 12
#define CMD_DISPLAYTEST 15
byte scr[NUM_MAX*8 + 8]; // +8 for scrolled char
void sendCmdAll(byte cmd, byte data)
{
digitalWrite(CS_PIN, LOW);
for (int i = NUM_MAX-1; i>=0; i--) {
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, cmd);
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, data);
}
digitalWrite(CS_PIN, HIGH);
}
void refreshAllRot270()
{
byte mask = 0x01;
for (int c = 0; c < 8; c++) {
digitalWrite(CS_PIN, LOW);
for(int i=NUM_MAX-1; i>=0; i--) {
byte bt = 0;
for(int b=0; b<8; b++) {
bt<<=1;
if(scr[i * 8 + b] & mask) bt|=0x01;
}
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, CMD_DIGIT0 + c);
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, bt);
}
digitalWrite(CS_PIN, HIGH);
mask<<=1;
}
}
void refreshAllRot90()
{
byte mask = 0x80;
for (int c = 0; c < 8; c++) {
digitalWrite(CS_PIN, LOW);
for(int i=NUM_MAX-1; i>=0; i--) {
byte bt = 0;
for(int b=0; b<8; b++) {
bt>>=1;
if(scr[i * 8 + b] & mask) bt|=0x80;
}
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, CMD_DIGIT0 + c);
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, bt);
}
digitalWrite(CS_PIN, HIGH);
mask>>=1;
}
}
void refreshAll() {
#if ROTATE==270
refreshAllRot270();
#elif ROTATE==90
refreshAllRot90();
#else
for (int c = 0; c < 8; c++) {
digitalWrite(CS_PIN, LOW);
for(int i=NUM_MAX-1; i>=0; i--) {
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, CMD_DIGIT0 + c);
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, scr[i * 8 + c]);
}
digitalWrite(CS_PIN, HIGH);
}
#endif
}
void clr()
{
for (int i = 0; i < NUM_MAX*8; i++) scr[i] = 0;
}
void scrollLeft()
{
for(int i=0; i < NUM_MAX*8+7; i++) scr[i] = scr[i+1];
}
void initMAX7219()
{
pinMode(DIN_PIN, OUTPUT);
pinMode(CLK_PIN, OUTPUT);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
sendCmdAll(CMD_DISPLAYTEST, 0);
sendCmdAll(CMD_SCANLIMIT, 7);
sendCmdAll(CMD_DECODEMODE, 0);
sendCmdAll(CMD_INTENSITY, 1);
sendCmdAll(CMD_SHUTDOWN, 0);
clr();
refreshAll();
}