![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Fix to have load pin working with SPI1.
Fork of Max7221 by
Max7221.h@9:75bddbfbb692, 2015-11-24 (annotated)
- Committer:
- ky3orr
- Date:
- Tue Nov 24 00:38:36 2015 +0000
- Revision:
- 9:75bddbfbb692
- Parent:
- 8:3f5901f2f813
All instances now share one SPI and /CS signal and operate in display chain.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jakowisp | 2:828c62cc1861 | 1 | /** |
jakowisp | 2:828c62cc1861 | 2 | * @file Max7221.h |
ky3orr | 7:a160cb7cdd86 | 3 | * @brief Header file for the MAX7219/7221 driver class. |
jakowisp | 2:828c62cc1861 | 4 | * |
ky3orr | 7:a160cb7cdd86 | 5 | * @author Grzegorz Kaczmarek |
ky3orr | 8:3f5901f2f813 | 6 | * @comment Code inspired on earlier Dwayne S. Dilbeck's work |
ky3orr | 7:a160cb7cdd86 | 7 | * @date 20/11/2015 |
jakowisp | 2:828c62cc1861 | 8 | */ |
ky3orr | 7:a160cb7cdd86 | 9 | |
jakowisp | 1:d8589d1f368c | 10 | #ifndef Max7221_H |
jakowisp | 1:d8589d1f368c | 11 | #define Max7221_H |
jakowisp | 1:d8589d1f368c | 12 | |
ky3orr | 7:a160cb7cdd86 | 13 | // MAX7219/7221 register map |
ky3orr | 9:75bddbfbb692 | 14 | #define max7221_reg_noop 0x00 |
ky3orr | 9:75bddbfbb692 | 15 | #define max7221_reg_digit0 0x01 |
ky3orr | 9:75bddbfbb692 | 16 | #define max7221_reg_digit1 0x02 |
ky3orr | 9:75bddbfbb692 | 17 | #define max7221_reg_digit2 0x03 |
ky3orr | 9:75bddbfbb692 | 18 | #define max7221_reg_digit3 0x04 |
ky3orr | 9:75bddbfbb692 | 19 | #define max7221_reg_digit4 0x05 |
ky3orr | 9:75bddbfbb692 | 20 | #define max7221_reg_digit5 0x06 |
ky3orr | 9:75bddbfbb692 | 21 | #define max7221_reg_digit6 0x07 |
ky3orr | 9:75bddbfbb692 | 22 | #define max7221_reg_digit7 0x08 |
ky3orr | 9:75bddbfbb692 | 23 | #define max7221_reg_decodeMode 0x09 |
ky3orr | 9:75bddbfbb692 | 24 | #define max7221_reg_intensity 0x0A |
ky3orr | 9:75bddbfbb692 | 25 | #define max7221_reg_scanLimit 0x0B |
ky3orr | 9:75bddbfbb692 | 26 | #define max7221_reg_shutdown 0x0C |
ky3orr | 9:75bddbfbb692 | 27 | #define max7221_reg_displayTest 0x0F |
jakowisp | 4:e2b160410338 | 28 | |
ky3orr | 7:a160cb7cdd86 | 29 | // Logical values |
ky3orr | 7:a160cb7cdd86 | 30 | #define LOG_0 0 |
ky3orr | 7:a160cb7cdd86 | 31 | #define LOG_1 1 |
jakowisp | 4:e2b160410338 | 32 | |
jakowisp | 1:d8589d1f368c | 33 | class Max7221 { |
ky3orr | 7:a160cb7cdd86 | 34 | private: |
ky3orr | 9:75bddbfbb692 | 35 | static SPI *mp_spi; // SPI used for this display (no data transmission when NULL) |
ky3orr | 9:75bddbfbb692 | 36 | static DigitalOut *mp_cs; // /CS signal (it should be handled manually when NULL) |
ky3orr | 9:75bddbfbb692 | 37 | static unsigned int m_counter; // Total number of displays on SPI chain |
ky3orr | 9:75bddbfbb692 | 38 | unsigned int m_position; // Display position on SPI chain (0 = 1'st) |
jakowisp | 1:d8589d1f368c | 39 | public: |
jakowisp | 4:e2b160410338 | 40 | /** |
ky3orr | 7:a160cb7cdd86 | 41 | * Default constructor. |
ky3orr | 9:75bddbfbb692 | 42 | * At least one instance should set SPI and /CS pointers for display chain to work. |
ky3orr | 9:75bddbfbb692 | 43 | * Use SetSpi() and SetCs() or parametrized constructor. |
ky3orr | 9:75bddbfbb692 | 44 | */ |
ky3orr | 9:75bddbfbb692 | 45 | Max7221(); |
ky3orr | 9:75bddbfbb692 | 46 | |
ky3orr | 9:75bddbfbb692 | 47 | /** |
ky3orr | 9:75bddbfbb692 | 48 | * Constructor. |
ky3orr | 9:75bddbfbb692 | 49 | * Sets SPI and /CS pointers. Display chain is operational. |
ky3orr | 7:a160cb7cdd86 | 50 | */ |
ky3orr | 9:75bddbfbb692 | 51 | Max7221(SPI *spi, DigitalOut *cs); |
ky3orr | 9:75bddbfbb692 | 52 | |
ky3orr | 9:75bddbfbb692 | 53 | /** |
ky3orr | 9:75bddbfbb692 | 54 | * Sets pointer to configured SPI. |
ky3orr | 9:75bddbfbb692 | 55 | * SPI is used by all display instances. |
ky3orr | 9:75bddbfbb692 | 56 | */ |
ky3orr | 9:75bddbfbb692 | 57 | void SetSpi(SPI *spi); |
ky3orr | 9:75bddbfbb692 | 58 | |
ky3orr | 9:75bddbfbb692 | 59 | /** |
ky3orr | 9:75bddbfbb692 | 60 | * Sets pointer to configured /CS signal. |
ky3orr | 9:75bddbfbb692 | 61 | * /CS signal is used by all display instances. |
ky3orr | 9:75bddbfbb692 | 62 | */ |
ky3orr | 9:75bddbfbb692 | 63 | void SetCs(DigitalOut *cs); |
ky3orr | 7:a160cb7cdd86 | 64 | |
ky3orr | 7:a160cb7cdd86 | 65 | /** |
ky3orr | 7:a160cb7cdd86 | 66 | * Method for setting /CS signal LOW (enable data reception). |
jakowisp | 4:e2b160410338 | 67 | */ |
ky3orr | 7:a160cb7cdd86 | 68 | void CsLow(void); |
ky3orr | 7:a160cb7cdd86 | 69 | |
ky3orr | 7:a160cb7cdd86 | 70 | /** |
ky3orr | 7:a160cb7cdd86 | 71 | * Method for setting /CS signal HIGH (received data is latched and processed). |
ky3orr | 7:a160cb7cdd86 | 72 | */ |
ky3orr | 7:a160cb7cdd86 | 73 | void CsHigh(void); |
ky3orr | 7:a160cb7cdd86 | 74 | |
ky3orr | 7:a160cb7cdd86 | 75 | /** |
ky3orr | 7:a160cb7cdd86 | 76 | * Method used to send data over SPI (no use of /CS signal). |
ky3orr | 7:a160cb7cdd86 | 77 | * To be used with display chains and when building display libraries |
ky3orr | 7:a160cb7cdd86 | 78 | * where amount of data needs to be pushed via SPI in one transaction. |
ky3orr | 7:a160cb7cdd86 | 79 | */ |
ky3orr | 7:a160cb7cdd86 | 80 | void WriteRaw(unsigned int reg, unsigned int data); |
jakowisp | 1:d8589d1f368c | 81 | |
jakowisp | 4:e2b160410338 | 82 | /** |
ky3orr | 7:a160cb7cdd86 | 83 | * Method for sending register-value pair for this diasplay. |
ky3orr | 7:a160cb7cdd86 | 84 | * Supports display chain on single SPI (m_position > 0). |
jakowisp | 4:e2b160410338 | 85 | */ |
ky3orr | 7:a160cb7cdd86 | 86 | void Write(unsigned int reg, unsigned int data); |
jakowisp | 2:828c62cc1861 | 87 | |
jakowisp | 4:e2b160410338 | 88 | /** |
ky3orr | 7:a160cb7cdd86 | 89 | * Sets test mode which causes all display LEDs to emit light with full brightess. |
ky3orr | 7:a160cb7cdd86 | 90 | * @param mode true - test mode, false - normal operation. |
ky3orr | 7:a160cb7cdd86 | 91 | */ |
ky3orr | 7:a160cb7cdd86 | 92 | void TestMode(bool mode); |
ky3orr | 7:a160cb7cdd86 | 93 | |
ky3orr | 7:a160cb7cdd86 | 94 | /** |
ky3orr | 7:a160cb7cdd86 | 95 | * Sets amount of digits to be used by this display. |
ky3orr | 7:a160cb7cdd86 | 96 | * @param digits_no 1 - one digit, (2-7), 8 - all 8 digits. |
ky3orr | 7:a160cb7cdd86 | 97 | */ |
ky3orr | 7:a160cb7cdd86 | 98 | void UseDigitsNo(unsigned int digits_no); |
jakowisp | 2:828c62cc1861 | 99 | |
jakowisp | 4:e2b160410338 | 100 | /** |
ky3orr | 7:a160cb7cdd86 | 101 | * Selects for which digits Code-B data decoding is used. |
ky3orr | 7:a160cb7cdd86 | 102 | * @param mode 0x00 - no decoding, 0x0F - digits 0-3, 0xFF - all digits. |
ky3orr | 7:a160cb7cdd86 | 103 | */ |
ky3orr | 7:a160cb7cdd86 | 104 | void DecodeMode(unsigned int mode); |
jakowisp | 4:e2b160410338 | 105 | |
jakowisp | 4:e2b160410338 | 106 | /** |
ky3orr | 7:a160cb7cdd86 | 107 | * Sends data to selected digit. |
ky3orr | 7:a160cb7cdd86 | 108 | * @param digit_no Selected digit (0-7). |
ky3orr | 7:a160cb7cdd86 | 109 | * @param digit_no Data to display. |
ky3orr | 7:a160cb7cdd86 | 110 | */ |
ky3orr | 7:a160cb7cdd86 | 111 | void WriteDigit(unsigned int digit_no, unsigned int value); |
jakowisp | 2:828c62cc1861 | 112 | |
jakowisp | 4:e2b160410338 | 113 | /** |
ky3orr | 7:a160cb7cdd86 | 114 | * Sets display brightness. |
ky3orr | 7:a160cb7cdd86 | 115 | * @param intensity 0x00 - minimum brightness, 0x0F - maximum brightness. |
ky3orr | 7:a160cb7cdd86 | 116 | */ |
ky3orr | 7:a160cb7cdd86 | 117 | void Intensity(unsigned int intensity); |
ky3orr | 7:a160cb7cdd86 | 118 | |
ky3orr | 7:a160cb7cdd86 | 119 | /** |
ky3orr | 7:a160cb7cdd86 | 120 | * Sets display operation mode. |
ky3orr | 7:a160cb7cdd86 | 121 | * @param mode true - normal operation, false - shutdown mode. |
ky3orr | 7:a160cb7cdd86 | 122 | */ |
ky3orr | 7:a160cb7cdd86 | 123 | void OperationMode(bool mode); |
ky3orr | 7:a160cb7cdd86 | 124 | |
ky3orr | 7:a160cb7cdd86 | 125 | /** |
ky3orr | 7:a160cb7cdd86 | 126 | * Initial setup of this display. |
ky3orr | 7:a160cb7cdd86 | 127 | * 8-digit blank decimal display set to nimimal brightness. |
ky3orr | 7:a160cb7cdd86 | 128 | * Supports display chain on single SPI (m_position > 0). |
jakowisp | 4:e2b160410338 | 129 | */ |
ky3orr | 7:a160cb7cdd86 | 130 | void Setup(void); |
jakowisp | 1:d8589d1f368c | 131 | }; |
jakowisp | 1:d8589d1f368c | 132 | |
jakowisp | 1:d8589d1f368c | 133 | #endif |