-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmax6954.cpp
348 lines (310 loc) · 8.63 KB
/
max6954.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "Arduino.h"
#include "max6954.h"
#include <SPI.h>
uint8_t lowercase_character_map[][2] = {
{ B00000000, B00000000 }, // space
{ B00000000, B00000000 }, // ! 32
{ B00000000, B00000000 }, // "
{ B00000000, B00000000 }, // #
{ B00000000, B00000000 }, // $
{ B00000000, B00000000 }, // %
{ B00000000, B00000000 }, // &
{ B00000000, B00000000 }, // '
{ B00000000, B00000000 }, // (
{ B00000000, B00000000 }, // )
{ B00000000, B00000000 }, // *
{ B00000000, B00000000 }, // +
{ B00000000, B00000000 }, // ,
{ B00000000, B00000000 }, // -
{ B00000000, B00000000 }, // .
{ B00000000, B00000000 }, // /
{ B00000000, B00000000 }, // 0
{ B00000000, B00000000 }, // 1
{ B00000000, B00000000 }, // 2
{ B00000000, B00000000 }, // 3
{ B00000000, B00000000 }, // 4
{ B00000000, B00000000 }, // 5
{ B00000000, B00000000 }, // 6
{ B00000000, B00000000 }, // 7
{ B00000000, B00000000 }, // 8
{ B00000000, B00000000 }, // 9
{ B00000000, B00000000 }, // :
{ B00000000, B00000000 }, // ;
{ B00000000, B00000000 }, // <
{ B00000000, B00000000 }, // =
{ B00000000, B00000000 }, // >
{ B00000000, B00000000 }, // ?
{ B00000000, B00000000 }, // @
{ B10001101, B00000010 }, // A
{ B10000111, B00000010 }, // B
{ B10000101, B00000000 }, // C
{ B10000101, B00010010 }, // D
{ B10000101, B00000001 }, // E
{ B01000111, B00000000 }, // F
{ B11000011, B00010010 }, // G
{ B00000111, B00000010 }, // H
{ B00000100, B00000000 }, // I
{ B10000000, B00000010 }, // J
{ B00000000, B01010110 }, // K
{ B00001000, B00010010 }, // L
{ B00010101, B01000010 }, // M
{ B00000101, B00000010 }, // N
{ B10000101, B00000010 }, // O
{ B01000111, B00010000 }, // P
{ B01001011, B00010010 }, // Q
{ B00000101, B00000000 }, // R
{ B00001000, B01000100 }, // S
{ B00001001, B01010010 }, // T
{ B10000100, B00000010 }, // U
{ B00000100, B00000001 }, // V
{ B10011100, B00000010 }, // W
{ B00000000, B00101101 }, // X
{ B10000011, B00010010 }, // Y
{ B10000001, B00000001 }, // Z
{ B00000000, B00000000 }, // [
{ B00000000, B00000000 }, // backslash
{ B00000000, B00000000 }, // ]
{ B00000000, B00000000 }, // ^
{ B00000000, B00000000 }, // _
{ B00000000, B00000000 } // ` 96
// { 123
// |
// }
// ~
};
MAX6954::MAX6954(uint8_t out, uint8_t in, uint8_t clk, uint8_t c1, uint8_t c2) {
data_out = out;
data_in = in;
clock = clk;
chip1 = c1;
chip2 = c2;
pinMode(data_out, OUTPUT);
pinMode(data_in, INPUT);
pinMode(clock, OUTPUT);
pinMode(chip1, OUTPUT);
pinMode(chip2, OUTPUT);
digitalWrite(chip1, HIGH); // disable device
digitalWrite(chip2, HIGH); // disable device
}
void MAX6954::begin() {
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
// SPI Speed = 1MHz
// SPI.setClockDivider(SPI_CLOCK_DIV8); // 8MHz Arduino
SPI.setClockDivider(SPI_CLOCK_DIV16); // 16MHz Arduino
// SPI_CLOCK_DIV possibilities 2,4,8,16,32,64,128
// Decode mode enabled
write(0x01, B11111111);
// Scan Limit - all digits
write(0x03, B00000111);
// Configuration - fast blinking and normal operation, invidual digit brightness
//write(0x04, B01000101);
// Configuration - fast blinking and normal operation, global brightness
write(0x04, B00000101);
// Digit Type - all 16 segments
write(0x0C, B00000000);
}
void MAX6954::set_digit_type_all_16segment() {
// Digit Type - all 16 segments
write(0x0C, B00000000);
}
void MAX6954::set_digit_type_all_14segment() {
// Digit Type - all 14 segments
write(0x0C, B11111111);
}
void MAX6954::read(byte address, uint8_t cs){
//Serial.print(address, HEX);
//Serial.print(" = ");
digitalWrite(cs, LOW);
SPI.transfer(address | B10000000);
SPI.transfer(0x00);
digitalWrite(cs, HIGH);
digitalWrite(cs, LOW);
SPI.transfer(address);
//Serial.println(SPI.transfer(0x00), BIN);
digitalWrite(cs, HIGH);
}
void MAX6954::write(byte address, byte value) {
digitalWrite(chip1, LOW);
digitalWrite(chip2, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(chip1, HIGH);
digitalWrite(chip2, HIGH);
}
void MAX6954::write_chip1(byte address, byte value) {
digitalWrite(chip1, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(chip1, HIGH);
}
void MAX6954::write_chip2(byte address, byte value) {
digitalWrite(chip2, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(chip2, HIGH);
}
void MAX6954::control_intensity(int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7) {
write(0x10, d1 << 4 | d0);
write(0x11, d3 << 4 | d2);
write(0x12, d5 << 4 | d4);
write(0x13, d7 << 4 | d6);
}
void MAX6954::write_string(char str[], int row, int col){
int i=0;
for (int digit = 0x20+col; digit <= 0x27; digit++) {
if (str[i] == '\0')
return;
// If the next character is a period
if (str[i+1] == '.') {
if (row==0)
write_chip1(digit, str[i] | B10000000);
else if (row==1)
write_chip2(digit, str[i] | B10000000);
else
write(digit, str[i] | B10000000);
i+=2; // skip the decimal
}
else {
if (row==0)
write_chip1(digit, str[i]);
else if (row==1)
write_chip2(digit, str[i]);
else
write(digit, str[i]);
i++;
}
}
}
void MAX6954::write_string1(char str[]){
write_chip1(0x20, str[0]);
write_chip1(0x21, str[1]);
write_chip1(0x22, str[2]);
write_chip1(0x23, str[3]);
write_chip1(0x24, str[4]);
write_chip1(0x25, str[5]);
write_chip1(0x26, str[6]);
write_chip1(0x27, str[7]);
}
void MAX6954::write_string2(char str[]){
write_chip2(0x20, str[0]);
write_chip2(0x21, str[1]);
write_chip2(0x22, str[2]);
write_chip2(0x23, str[3]);
write_chip2(0x24, str[4]);
write_chip2(0x25, str[5]);
write_chip2(0x26, str[6]);
write_chip2(0x27, str[7]);
}
void MAX6954::write_int1(int value){
char buffer[16];
int column = 0x20;
ultoa(value, buffer, 10);
int len = strlen(buffer);
for (int i=0; i<len; i++) {
write_chip1(column++, buffer[i]);
}
}
void MAX6954::write_lowercase(char c, int row=0, int col=0) {
int offset = 32;
if (c >= 'a')
offset = 64;
if (row==0) {
write_chip1(0x20+col, lowercase_character_map[c-offset][0]);
write_chip1(0x28+col, lowercase_character_map[c-offset][1]);
}
else {
write_chip2(0x20+col, lowercase_character_map[c-offset][0]);
write_chip2(0x28+col, lowercase_character_map[c-offset][1]);
}
}
void MAX6954::write_lowercase_string(char string[], int row=0, int col=0) {
uint16_t i;
for (i=0; i<strlen(string); i++) {
write_lowercase(string[i], row+i/8, col+i%8);
}
}
void MAX6954::enable_individual_segment_brightness() {
// Global Intensity to zero - individual segment control
write(0x02, B00000000);
// Set Configuration - fast blinking and normal operation, individual brightness
write(0x04, B01000101);
}
void MAX6954::enable_global_segment_brightness() {
// Global Intensity to one
write(0x02, B00000001);
// Set Configuration - fast blinking and normal operation, global brightness
write(0x04, B00000101);
}
void MAX6954::disable_decode_mode() {
write(0x01, B00000000); // Disable Decode Mode
}
void MAX6954::turn_off_individual_segments() {
// Turn off all segments manually
for (int i=0x20; i<=0x2F; i++) {
write(i, B00000000);
}
}
void MAX6954::enable_decode_mode() {
write(0x01, B11111111); // Decode mode enabled
}
void MAX6954::set_global_brightness(int i) {
if (i>15) i=15;
if (i<0) i=0;
// Global Intensity to one
write(0x02, i);
}
void MAX6954::individual_segment_test() {
// Decode mode disabled
write(0x01, B00000000);
// Turn off all segments manually
for (int i=0x20; i<=0x2F; i++) {
write(i, B00000000);
}
// Turn on all segments manually (decode mode off)
// write(0x20, B11111111);
// write(0x28, B11111111);
// delay(5000);
int dl = 40;
for (int i=0; i<4; i++) {
write(0x20,B00000001);
delay(dl);
write(0x20,B00000000);
write(0x28,B00100000);
delay(dl);
write(0x28,B00010000);
delay(dl);
write(0x28,B00001000);
delay(dl);
write(0x28,B01000000);
delay(dl);
write(0x28,B00000100);
delay(dl);
write(0x28,B00000010);
delay(dl);
write(0x28,B00000001);
delay(dl);
}
for (int i=0x20; i<=0x2F; i++) {
write(i, B00000000);
}
// Test each segment bit slowly
// dl = 3000;
// int segment = B10000000;
// for (int i=0; i<8; i++) {
// write(0x20,segment);
// segment = segment >> 1;
// delay(dl);
// }
// write(0x20,B00000000);
// segment = B10000000;
// for (int i=0; i<8; i++) {
// write(0x28,segment);
// segment = segment >> 1;
// delay(dl);
// }
// write(0x28,B00000000);
// Decode mode enabled
write(0x01, B11111111);
}