Grzegorz Kaczmarek
/
Max7221
Fix to have load pin working with SPI1.
Fork of Max7221 by
Max7221.cpp@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.cpp |
ky3orr | 7:a160cb7cdd86 | 3 | * @brief 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 | #include "mbed.h" |
jakowisp | 1:d8589d1f368c | 11 | #include "Max7221.h" |
jakowisp | 1:d8589d1f368c | 12 | |
ky3orr | 9:75bddbfbb692 | 13 | // Static members initialization |
ky3orr | 9:75bddbfbb692 | 14 | SPI *Max7221::mp_spi = NULL; |
ky3orr | 9:75bddbfbb692 | 15 | DigitalOut *Max7221::mp_cs = NULL; |
ky3orr | 9:75bddbfbb692 | 16 | unsigned int Max7221::m_counter = 0; |
ky3orr | 9:75bddbfbb692 | 17 | |
ky3orr | 9:75bddbfbb692 | 18 | Max7221::Max7221() { |
ky3orr | 9:75bddbfbb692 | 19 | m_position = m_counter; |
ky3orr | 9:75bddbfbb692 | 20 | m_counter++; |
jakowisp | 1:d8589d1f368c | 21 | } |
jakowisp | 1:d8589d1f368c | 22 | |
ky3orr | 9:75bddbfbb692 | 23 | Max7221::Max7221(SPI *spi, DigitalOut *cs) { |
ky3orr | 9:75bddbfbb692 | 24 | mp_spi = spi; |
ky3orr | 9:75bddbfbb692 | 25 | mp_cs = cs; |
ky3orr | 9:75bddbfbb692 | 26 | m_position = m_counter; |
ky3orr | 9:75bddbfbb692 | 27 | m_counter++; |
ky3orr | 9:75bddbfbb692 | 28 | } |
ky3orr | 9:75bddbfbb692 | 29 | |
ky3orr | 9:75bddbfbb692 | 30 | void Max7221::SetSpi(SPI *spi) { |
ky3orr | 9:75bddbfbb692 | 31 | mp_spi = spi; |
ky3orr | 9:75bddbfbb692 | 32 | } |
ky3orr | 9:75bddbfbb692 | 33 | |
ky3orr | 9:75bddbfbb692 | 34 | void Max7221::SetCs(DigitalOut *cs) { |
ky3orr | 9:75bddbfbb692 | 35 | mp_cs = cs; |
ky3orr | 9:75bddbfbb692 | 36 | } |
ky3orr | 9:75bddbfbb692 | 37 | |
ky3orr | 9:75bddbfbb692 | 38 | |
ky3orr | 7:a160cb7cdd86 | 39 | void Max7221::CsLow() |
ky3orr | 7:a160cb7cdd86 | 40 | { |
ky3orr | 7:a160cb7cdd86 | 41 | if(mp_cs != NULL) { |
ky3orr | 7:a160cb7cdd86 | 42 | *mp_cs = LOG_0; |
jakowisp | 1:d8589d1f368c | 43 | } |
jakowisp | 1:d8589d1f368c | 44 | } |
jakowisp | 1:d8589d1f368c | 45 | |
ky3orr | 7:a160cb7cdd86 | 46 | void Max7221::CsHigh() |
ky3orr | 7:a160cb7cdd86 | 47 | { |
ky3orr | 7:a160cb7cdd86 | 48 | if(mp_cs != NULL) { |
ky3orr | 7:a160cb7cdd86 | 49 | *mp_cs = LOG_1; |
jakowisp | 1:d8589d1f368c | 50 | } |
jakowisp | 1:d8589d1f368c | 51 | } |
jakowisp | 1:d8589d1f368c | 52 | |
ky3orr | 7:a160cb7cdd86 | 53 | void Max7221::WriteRaw(unsigned int reg, unsigned int data) { |
ky3orr | 7:a160cb7cdd86 | 54 | if(mp_spi != NULL) { |
ky3orr | 7:a160cb7cdd86 | 55 | mp_spi->write(reg); |
ky3orr | 7:a160cb7cdd86 | 56 | mp_spi->write(data); |
jakowisp | 1:d8589d1f368c | 57 | } |
jakowisp | 1:d8589d1f368c | 58 | } |
ky3orr | 7:a160cb7cdd86 | 59 | |
ky3orr | 7:a160cb7cdd86 | 60 | void Max7221::Write(unsigned int reg, unsigned int data) { |
ky3orr | 7:a160cb7cdd86 | 61 | unsigned int i; |
ky3orr | 7:a160cb7cdd86 | 62 | |
ky3orr | 7:a160cb7cdd86 | 63 | CsLow(); |
ky3orr | 9:75bddbfbb692 | 64 | if((m_counter > 1) && ((m_counter - 1) > m_position)) { |
ky3orr | 9:75bddbfbb692 | 65 | i = m_counter - 1 - m_position; |
ky3orr | 9:75bddbfbb692 | 66 | while(i--) { |
ky3orr | 9:75bddbfbb692 | 67 | WriteRaw(max7221_reg_noop, 0x00); |
ky3orr | 9:75bddbfbb692 | 68 | } |
ky3orr | 9:75bddbfbb692 | 69 | } |
ky3orr | 7:a160cb7cdd86 | 70 | WriteRaw(reg, data); |
ky3orr | 7:a160cb7cdd86 | 71 | if(m_position > 0) { |
ky3orr | 7:a160cb7cdd86 | 72 | i = m_position; |
ky3orr | 7:a160cb7cdd86 | 73 | while(i--) { |
ky3orr | 9:75bddbfbb692 | 74 | WriteRaw(max7221_reg_noop, 0x00); |
ky3orr | 7:a160cb7cdd86 | 75 | } |
ky3orr | 7:a160cb7cdd86 | 76 | } |
ky3orr | 7:a160cb7cdd86 | 77 | CsHigh(); |
ky3orr | 7:a160cb7cdd86 | 78 | } |
ky3orr | 7:a160cb7cdd86 | 79 | |
ky3orr | 7:a160cb7cdd86 | 80 | void Max7221::TestMode(bool mode) { |
ky3orr | 7:a160cb7cdd86 | 81 | if(mode) { |
ky3orr | 9:75bddbfbb692 | 82 | Write(max7221_reg_displayTest, 0x01); |
ky3orr | 7:a160cb7cdd86 | 83 | } else { |
ky3orr | 9:75bddbfbb692 | 84 | Write(max7221_reg_displayTest, 0x00); |
ky3orr | 7:a160cb7cdd86 | 85 | } |
ky3orr | 7:a160cb7cdd86 | 86 | } |
ky3orr | 7:a160cb7cdd86 | 87 | |
ky3orr | 7:a160cb7cdd86 | 88 | void Max7221::UseDigitsNo(unsigned int digits_no) { |
ky3orr | 7:a160cb7cdd86 | 89 | if((digits_no > 0) && (digits_no < 9)) { |
ky3orr | 9:75bddbfbb692 | 90 | Write(max7221_reg_scanLimit, (digits_no - 1)); |
ky3orr | 7:a160cb7cdd86 | 91 | } |
ky3orr | 7:a160cb7cdd86 | 92 | } |
ky3orr | 7:a160cb7cdd86 | 93 | |
ky3orr | 7:a160cb7cdd86 | 94 | void Max7221::DecodeMode(unsigned int mode) { |
ky3orr | 9:75bddbfbb692 | 95 | Write(max7221_reg_decodeMode, mode); |
jakowisp | 1:d8589d1f368c | 96 | } |
jakowisp | 1:d8589d1f368c | 97 | |
ky3orr | 7:a160cb7cdd86 | 98 | void Max7221::WriteDigit(unsigned int digit_no, unsigned int value) { |
ky3orr | 9:75bddbfbb692 | 99 | unsigned int digit_reg; |
ky3orr | 9:75bddbfbb692 | 100 | |
ky3orr | 7:a160cb7cdd86 | 101 | if(digit_no < 8) { |
ky3orr | 9:75bddbfbb692 | 102 | switch(digit_no) { |
ky3orr | 9:75bddbfbb692 | 103 | case 0: |
ky3orr | 9:75bddbfbb692 | 104 | digit_reg = max7221_reg_digit0; |
ky3orr | 9:75bddbfbb692 | 105 | break; |
ky3orr | 9:75bddbfbb692 | 106 | case 1: |
ky3orr | 9:75bddbfbb692 | 107 | digit_reg = max7221_reg_digit1; |
ky3orr | 9:75bddbfbb692 | 108 | break; |
ky3orr | 9:75bddbfbb692 | 109 | case 2: |
ky3orr | 9:75bddbfbb692 | 110 | digit_reg = max7221_reg_digit2; |
ky3orr | 9:75bddbfbb692 | 111 | break; |
ky3orr | 9:75bddbfbb692 | 112 | case 3: |
ky3orr | 9:75bddbfbb692 | 113 | digit_reg = max7221_reg_digit3; |
ky3orr | 9:75bddbfbb692 | 114 | break; |
ky3orr | 9:75bddbfbb692 | 115 | case 4: |
ky3orr | 9:75bddbfbb692 | 116 | digit_reg = max7221_reg_digit4; |
ky3orr | 9:75bddbfbb692 | 117 | break; |
ky3orr | 9:75bddbfbb692 | 118 | case 5: |
ky3orr | 9:75bddbfbb692 | 119 | digit_reg = max7221_reg_digit5; |
ky3orr | 9:75bddbfbb692 | 120 | break; |
ky3orr | 9:75bddbfbb692 | 121 | case 6: |
ky3orr | 9:75bddbfbb692 | 122 | digit_reg = max7221_reg_digit6; |
ky3orr | 9:75bddbfbb692 | 123 | break; |
ky3orr | 9:75bddbfbb692 | 124 | case 7: |
ky3orr | 9:75bddbfbb692 | 125 | digit_reg = max7221_reg_digit7; |
ky3orr | 9:75bddbfbb692 | 126 | break; |
ky3orr | 9:75bddbfbb692 | 127 | default: |
ky3orr | 9:75bddbfbb692 | 128 | digit_reg = max7221_reg_noop; |
ky3orr | 9:75bddbfbb692 | 129 | break; |
ky3orr | 9:75bddbfbb692 | 130 | } |
ky3orr | 9:75bddbfbb692 | 131 | Write(digit_reg, value); |
ky3orr | 9:75bddbfbb692 | 132 | } |
ky3orr | 7:a160cb7cdd86 | 133 | } |
jakowisp | 1:d8589d1f368c | 134 | |
ky3orr | 7:a160cb7cdd86 | 135 | void Max7221::Intensity(unsigned int intensity) { |
ky3orr | 7:a160cb7cdd86 | 136 | if(intensity > 0x0F) { |
ky3orr | 7:a160cb7cdd86 | 137 | intensity = 0x0F; |
ky3orr | 7:a160cb7cdd86 | 138 | } |
ky3orr | 9:75bddbfbb692 | 139 | Write(max7221_reg_intensity, intensity); |
jakowisp | 1:d8589d1f368c | 140 | } |
jakowisp | 1:d8589d1f368c | 141 | |
ky3orr | 7:a160cb7cdd86 | 142 | void Max7221::OperationMode(bool mode) { |
ky3orr | 7:a160cb7cdd86 | 143 | if(mode) { |
ky3orr | 9:75bddbfbb692 | 144 | Write(max7221_reg_shutdown, 0x01); |
ky3orr | 7:a160cb7cdd86 | 145 | } else { |
ky3orr | 9:75bddbfbb692 | 146 | Write(max7221_reg_shutdown, 0x00); |
jakowisp | 1:d8589d1f368c | 147 | } |
jakowisp | 1:d8589d1f368c | 148 | } |
jakowisp | 2:828c62cc1861 | 149 | |
ky3orr | 7:a160cb7cdd86 | 150 | void Max7221::Setup () { |
ky3orr | 7:a160cb7cdd86 | 151 | unsigned int i; |
ky3orr | 7:a160cb7cdd86 | 152 | |
ky3orr | 7:a160cb7cdd86 | 153 | TestMode(false); // Disable display test |
ky3orr | 7:a160cb7cdd86 | 154 | UseDigitsNo(8); // Use all 8 digits |
ky3orr | 7:a160cb7cdd86 | 155 | DecodeMode(0xFF); // Turn on Code B font decode for all digits |
ky3orr | 9:75bddbfbb692 | 156 | for(i=0;i<8;i++) { // Clean all digits |
ky3orr | 7:a160cb7cdd86 | 157 | WriteDigit(i, 0x0F); |
ky3orr | 7:a160cb7cdd86 | 158 | } |
ky3orr | 7:a160cb7cdd86 | 159 | Intensity(0x01); // Set lowest display intensity(0x00-0xFF) |
ky3orr | 7:a160cb7cdd86 | 160 | OperationMode(true); // Enable operation mode |
ky3orr | 7:a160cb7cdd86 | 161 | } |