-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmax7219.c
207 lines (193 loc) · 6.51 KB
/
max7219.c
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <max7219.h>
void max7219_TurnOnAllDisplays(unsigned int16 loadPin){
unsigned int8 counter = 0;
output_low(loadPin);
for(counter = 0; counter < MAX7219_DISPLAYS_AMOUNT; counter++){
_max7219_ShiftDataOut(MAX7219_SHUTDOWN_REG, MAX7219_NOSHUTDOWN);
}
output_high(loadPin);
output_low(loadPin);
}
int1 max7219_TurnOnDisplay(unsigned int8 displayNumber, unsigned int16 loadPin){
if(displayNumber >= MAX7219_DISPLAYS_AMOUNT) return FALSE;
unsigned int8 counter = 0;
output_low(loadPin);
for(counter = 0; counter < MAX7219_DISPLAYS_AMOUNT; counter++){
if(counter == displayNumber){
_max7219_ShiftDataOut(MAX7219_SHUTDOWN_REG, MAX7219_NOSHUTDOWN);
}
else{
_max7219_ShiftDataOut(MAX7219_NOOP, MAX7219_NOOP);
}
}
output_high(loadPin);
output_low(loadPin);
return TRUE;
}
void max7219_TestAllDisplays(int1 test, unsigned int16 loadPin){
unsigned int8 counter = 0;
output_low(loadPin);
for(counter = 0; counter < MAX7219_DISPLAYS_AMOUNT; counter++){
_max7219_ShiftDataOut(MAX7219_TESTDISPLAY_REG, test);
}
output_high(loadPin);
output_low(loadPin);
}
int1 max7219_TestDisplay(unsigned int8 displayNumber, int1 test, unsigned int16 loadPin){
if(displayNumber >= MAX7219_DISPLAYS_AMOUNT) return FALSE;
unsigned int8 counter = 0;
output_low(loadPin);
for(counter = 0; counter < MAX7219_DISPLAYS_AMOUNT; counter++){
if(counter == displayNumber){
_max7219_ShiftDataOut(MAX7219_TESTDISPLAY_REG, test);
}
else{
_max7219_ShiftDataOut(MAX7219_NOOP, MAX7219_NOOP);
}
}
output_high(loadPin);
output_low(loadPin);
return TRUE;
}
int1 max7219_SetDigitsDecode(unsigned int8 displayNumber, unsigned int8 digitSetup, unsigned int16 loadPin){
if(displayNumber >= MAX7219_DISPLAYS_AMOUNT) return FALSE;
unsigned int8 counter = 0;
output_low(loadPin);
for(counter = 0; counter < MAX7219_DISPLAYS_AMOUNT; counter++){
if(counter == displayNumber){
_max7219_ShiftDataOut(MAX7219_DECODEMODE_REG, digitSetup);
}
else{
_max7219_ShiftDataOut(MAX7219_NOOP, MAX7219_NOOP);
}
}
output_high(loadPin);
output_low(loadPin);
return TRUE;
}
int1 max7219_SetDigitValue(unsigned int8 displayNumber, unsigned int8 digitRegister, unsigned int8 digitValue, unsigned int16 loadPin){
if(displayNumber >= MAX7219_DISPLAYS_AMOUNT) return FALSE;
unsigned int8 counter = 0;
output_low(loadPin);
for(counter = 0; counter < MAX7219_DISPLAYS_AMOUNT; counter++){
if(counter == displayNumber){
_max7219_ShiftDataOut(digitRegister, digitValue);
}
else{
_max7219_ShiftDataOut(MAX7219_NOOP, MAX7219_NOOP);
}
}
output_high(loadPin);
output_low(loadPin);
return TRUE;
}
void max7219_ClearAllDisplays(unsigned int16 loadPin){
unsigned int8 counter = 0;
unsigned int8 digit = 0;
output_low(loadPin);
for(digit = MAX7219_D0_REG; digit <= MAX7219_D7_REG; digit++){
for(counter = 0; counter < MAX7219_DISPLAYS_AMOUNT; counter++){
_max7219_ShiftDataOut(digit, MAX7219_NOOP);
}
output_high(loadPin);
output_low(loadPin);
}
}
int1 max7219_ClearDisplay(unsigned int8 displayNumber, unsigned int16 loadPin){
if(displayNumber >= MAX7219_DISPLAYS_AMOUNT) return FALSE;
unsigned int8 counter = 0;
unsigned int8 digit = 0;
output_low(loadPin);
for(digit = MAX7219_D0_REG; digit <= MAX7219_D7_REG; digit++){
for(counter = 0; counter < MAX7219_DISPLAYS_AMOUNT; counter++){
if(counter == displayNumber){
_max7219_ShiftDataOut(digit, MAX7219_NOOP);
}
else{
_max7219_ShiftDataOut(MAX7219_NOOP, MAX7219_NOOP);
}
}
output_high(loadPin);
output_low(loadPin);
}
return TRUE;
}
void max7219_ScanAllDisplays(unsigned int16 loadPin){
unsigned int8 counter = 0;
output_low(loadPin);
for(counter = 0; counter < MAX7219_DISPLAYS_AMOUNT; counter++){
_max7219_ShiftDataOut(MAX7219_SCANLIMIT_REG, MAX7219_SCAN_ALL);
}
output_high(loadPin);
output_low(loadPin);
}
int1 max7219_ScanDisplayDigits(unsigned int8 displayNumber, unsigned int8 scanConfiguration, unsigned int16 loadPin){
if(displayNumber >= MAX7219_DISPLAYS_AMOUNT) return FALSE;
unsigned int8 counter = 0;
output_low(loadPin);
for(counter = 0; counter < MAX7219_DISPLAYS_AMOUNT; counter++){
if(counter == displayNumber){
_max7219_ShiftDataOut(MAX7219_SCANLIMIT_REG, scanConfiguration);
}
else{
_max7219_ShiftDataOut(MAX7219_NOOP, MAX7219_NOOP);
}
}
output_high(loadPin);
output_low(loadPin);
return TRUE;
}
void max7219_SetAllDisplaysIntensity(unsigned int8 intensity, unsigned int16 loadPin){
unsigned int8 counter = 0;
output_low(loadPin);
for(counter = 0; counter < MAX7219_DISPLAYS_AMOUNT; counter++){
_max7219_ShiftDataOut(MAX7219_INTENSITY_REG, intensity);
}
output_high(loadPin);
output_low(loadPin);
}
int1 max7219_SetDisplayIntensity(unsigned int8 displayNumber, unsigned int8 intensityConfiguration, unsigned int16 loadPin){
if(displayNumber >= MAX7219_DISPLAYS_AMOUNT) return FALSE;
unsigned int8 counter = 0;
output_low(loadPin);
for(counter = 0; counter < MAX7219_DISPLAYS_AMOUNT; counter++){
if(counter == displayNumber){
_max7219_ShiftDataOut(MAX7219_INTENSITY_REG, intensityConfiguration);
}
else{
_max7219_ShiftDataOut(MAX7219_NOOP, MAX7219_NOOP);
}
}
output_high(loadPin);
output_low(loadPin);
return TRUE;
}
void max7219_SendArray(unsigned int8* data, unsigned int8 digitRegister, unsigned int16 loadPin){
unsigned int8 counter = 0;
output_low(loadPin);
for(counter = 0; counter < MAX7219_DISPLAYS_AMOUNT; counter++){
_max7219_ShiftDataOut(digitRegister, data[counter]);
}
output_high(loadPin);
output_low(loadPin);
}
#ifdef MAX7219_NO_SPI
void _max7219_ShiftDataOut(unsigned int8 registerAddress, unsigned int8 data){
unsigned int8 counter = 0;
for(counter = 0; counter < 8; counter++){
output_bit(DOUT_PIN, (registerAddress >> (7 - counter)) & 0x01);
output_high(CLK_PIN);
output_low(CLK_PIN);
}
for(counter = 0; counter < 8; counter++){
output_bit(DOUT_PIN, (data >> (7 - counter)) & 0x01);
output_high(CLK_PIN);
output_low(CLK_PIN);
}
}
#else
void _max7219_ShiftDataOut(unsigned int8 registerAddress, unsigned int8 data){
spi_write(registerAddress);
spi_write(data);
}
#endif