-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax7219.h
477 lines (424 loc) · 14.2 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/******************************************************************//**
* @file max7219.h
* Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
**********************************************************************/
#ifndef MAX7219_H
#define MAX7219_H
#include "mbed.h"
/**
* @brief Structure for device configuration
* @code
* #include "max7219.h"
*
* max7219_configuration_t cfg = {
* .device_number = 1,
* .decode_mode = 0,
* .intensity = Max7219::MAX7219_INTENSITY_8,
* .scan_limit = Max7219::MAX7219_SCAN_8
* };
* @endcode
*/
typedef struct
{
uint8_t device_number;
uint8_t decode_mode;
uint8_t intensity;
uint8_t scan_limit;
}max7219_configuration_t;
/**
* @brief Serially Interfaced, 8-Digit, LED Display Driver
*
* @details The MAX7219/MAX7221 are compact, serial input/output common-cathode
* display drivers that interface microprocessors (µPs) to 7-segment numeric
* LED displays of up to 8 digits, bar-graph displays, or 64 individual LEDs.
* Included on-chip are a BCD code-B decoder, multiplex scan circuitry, segment
* and digit drivers, and an 8x8 static RAM that stores each digit. Only one
* external resistor is required to set the segment current for all LEDs.
* The MAX7221 is compatible with SPI™, QSPI™, and MICROWIRE™, and has
* slew-rate-limited segment drivers to reduce EMI.
*
* @code
* #include "mbed.h"
* #include "max7219.h"
*
* Max7219 max7219(SPI0_MOSI, SPI0_MISO, SPI0_SCK, SPI0_SS);
*
* int main()
* {
* max7219_configuration_t cfg = {
* .device_number = 1,
* .decode_mode = 0,
* .intensity = Max7219::MAX7219_INTENSITY_8,
* .scan_limit = Max7219::MAX7219_SCAN_8
* };
*
* max7219.init_device(cfg);
* max7219.enable_device(1);
* max7219.set_display_test();
* wait(1);
* max7219.clear_display_test();
*
* while (1) {
* max7219.write_digit(1, Max7219::MAX7219_DIGIT_0, ...
* }
* }
* @endcode
*/
class Max7219
{
public:
/**
* @brief Digit and Control Registers
* @details The 14 addressable digit and control registers.
*/
typedef enum
{
MAX7219_NO_OP = 0,
MAX7219_DIGIT_0,
MAX7219_DIGIT_1,
MAX7219_DIGIT_2,
MAX7219_DIGIT_3,
MAX7219_DIGIT_4,
MAX7219_DIGIT_5,
MAX7219_DIGIT_6,
MAX7219_DIGIT_7,
MAX7219_DECODE_MODE,
MAX7219_INTENSITY,
MAX7219_SCAN_LIMIT,
MAX7219_SHUTDOWN,
MAX7219_DISPLAY_TEST = 15
}max7219_register_e;
/**
* @brief Intensity values
* @details Digital control of display brightness is provided by an
* internal pulse-width modulator, which is controlled by
* the lower nibble of the intensity register.
*/
typedef enum
{
MAX7219_INTENSITY_0 = 0,
MAX7219_INTENSITY_1,
MAX7219_INTENSITY_2,
MAX7219_INTENSITY_3,
MAX7219_INTENSITY_4,
MAX7219_INTENSITY_5,
MAX7219_INTENSITY_6,
MAX7219_INTENSITY_7,
MAX7219_INTENSITY_8,
MAX7219_INTENSITY_9,
MAX7219_INTENSITY_A,
MAX7219_INTENSITY_B,
MAX7219_INTENSITY_C,
MAX7219_INTENSITY_D,
MAX7219_INTENSITY_E,
MAX7219_INTENSITY_F
}max7219_intensity_e;
/**
* @brief Scan limit for mutiplexing digits
* @details The scan-limit register sets how many digits are
* displayed, from 1 to 8. They are displayed in a multiplexed
* manner with a typical display scan rate of 800Hz with 8
* digits displayed.
*/
typedef enum
{
MAX7219_SCAN_1 = 0,
MAX7219_SCAN_2,
MAX7219_SCAN_3,
MAX7219_SCAN_4,
MAX7219_SCAN_5,
MAX7219_SCAN_6,
MAX7219_SCAN_7,
MAX7219_SCAN_8
}max7219_scan_limit_e;
/**********************************************************//**
* @brief Constructor for Max7219 Class.
*
* @details Allows user to pass pointer to existing SPI bus
*
* On Entry:
* @param[in] spi_bus - pointer to existing SPI object
* @param[in] cs - pin to use for cs
*
* On Exit:
*
* @return None
**************************************************************/
Max7219(SPI *spi_bus, PinName cs);
/**********************************************************//**
* @brief Constructor for Max7219 Class.
*
* @details Allows user to specify SPI peripheral to use
*
* On Entry:
* @param[in] mosi - pin to use for mosi
* @param[in] miso - pin to use for miso
* @param[in] sclk - pin to use for sclk
* @param[in] cs - pin to use for cs
*
* On Exit:
*
* @return None
**************************************************************/
Max7219(PinName mosi, PinName miso, PinName sclk, PinName cs);
/**********************************************************//**
* @brief Default destructor for Max7219 Class.
*
* @details Destroys SPI object if owner
*
* On Entry:
*
* On Exit:
*
* @return None
**************************************************************/
~Max7219();
/**********************************************************//**
* @brief Sets the number of MAX7219 devices being used.
* Defaults to one
*
* @details
*
* On Entry:
* @param[in] num_devices - number of MAX7219 devices being
* used, max of 255
*
* On Exit:
*
* @return Returns number of devices
**************************************************************/
int32_t set_num_devices(uint8_t num_devices);
/**********************************************************//**
* @brief Tests all devices being used
*
* @details Sets bit0 of DISPLAY_TEST regiser in all devices
*
* On Entry:
*
* On Exit:
*
* @return None
**************************************************************/
void set_display_test(void);
/**********************************************************//**
* @brief Stops test
*
* @details Clear bit0 of DISPLAY_TEST regiser in all devices
*
* On Entry:
*
* On Exit:
*
* @return None
**************************************************************/
void clear_display_test(void);
/**********************************************************//**
* @brief Initializes specific device in display with given
* config data
*
* @details
*
* On Entry:
* @param[in] config - Structure containing configuration
* data of device
*
* On Exit:
*
* @return Returns 0 on success\n
* Returns -1 if device number is > _num_devices\n
* Returns -2 if device number is 0\n
**************************************************************/
int32_t init_device(max7219_configuration_t config);
/**********************************************************//**
* @brief Initializes all devices with given config data
*
* @details All devices are configured with given data
*
* On Entry:
* @param[in] config - Structure containing configuration
* data
* On Exit:
*
* @return None
**************************************************************/
void init_display(max7219_configuration_t config);
/**********************************************************//**
* @brief Enables specific device in display
*
* @details
*
* On Entry:
* @param[in] device_number - device to enable
*
* On Exit:
*
* @return Returns 0 on success\n
* Returns -1 if device number is > _num_devices\n
* Returns -2 if device number is 0\n
**************************************************************/
int32_t enable_device(uint8_t device_number);
/**********************************************************//**
* @brief Enables all device in display
*
* @details
*
* On Entry:
*
* On Exit:
*
* @return None
**************************************************************/
void enable_display(void);
/**********************************************************//**
* @brief Disables specific device in display
*
* @details
*
* On Entry:
* @param[in] device_number - device to disable
*
* On Exit:
* @return Returns 0 on success\n
* Returns -1 if device number is > _num_devices\n
* Returns -2 if device number is 0\n
**************************************************************/
int32_t disable_device(uint8_t device_number);
/**********************************************************//**
* @brief Disables all devices in display
*
* @details
*
* On Entry:
*
* On Exit:
*
* @return None
**************************************************************/
void disable_display(void);
/**********************************************************//**
* @brief Writes digit of given device with given data, user
* must enter correct data for decode_mode chosen
*
* @details
*
* On Entry:
* @param[in] device_number - device to write too
* @param[in] digit - digit to write
* @param[in] data - data to write
*
* On Exit:
*
* @return Returns 0 on success\n
* Returns -1 if device number is > _num_devices\n
* Returns -2 if device number is 0\n
* Returns -3 if digit > 8\n
* Returns -4 if digit < 1\n
**************************************************************/
int32_t write_digit(uint8_t device_number, uint8_t digit, uint8_t data);
/**********************************************************//**
* @brief Clears digit of given device
*
* @details
*
* On Entry:
* @param[in] device_number - device to write too
* @param[in] digit - digit to clear
*
* On Exit:
*
* @return Returns 0 on success\n
* Returns -1 if device number is > _num_devices\n
* Returns -2 if device number is 0\n
* Returns -3 if digit > 8\n
* Returns -4 if digit < 1\n
**************************************************************/
int32_t clear_digit(uint8_t device_number, uint8_t digit);
/**********************************************************//**
* @brief Turns on all segments/digits of given device
*
* @details
*
* On Entry:
* @param[in] device_number - device to write too
*
* On Exit:
*
* @return Returns 0 on success\n
* Returns -1 if device number is > _num_devices\n
* Returns -2 if device number is 0\n
**************************************************************/
int32_t device_all_on(uint8_t device_number);
/**********************************************************//**
* @brief Turns off all segments/digits of given device
*
* @details
*
* On Entry:
* @param[in] device_number - device to write too
*
* On Exit:
*
* @return Returns 0 on success\n
* Returns -1 if device number is > _num_devices\n
* Returns -2 if device number is 0\n
**************************************************************/
int32_t device_all_off(uint8_t device_number);
/**********************************************************//**
* @brief Turns on all segments/digits of display
*
* @details
*
* On Entry:
*
* On Exit:
*
* @return None
**************************************************************/
void display_all_on(void);
/**********************************************************//**
* @brief Turns off all segments/digits of display
*
* @details
*
* On Entry:
*
* On Exit:
*
* @return None
**************************************************************/
void display_all_off(void);
private:
SPI *_p_spi;
DigitalOut *_p_cs;
bool _spi_owner;
uint8_t _num_devices;
};
#endif /* MAX7219_H*/