Grzegorz Kaczmarek
/
Max7221
Fix to have load pin working with SPI1.
Fork of Max7221 by
MAX7221/Max7221.cpp@1:d8589d1f368c, 2013-08-06 (annotated)
- Committer:
- jakowisp
- Date:
- Tue Aug 06 08:18:53 2013 +0000
- Revision:
- 1:d8589d1f368c
- Child:
- 2:828c62cc1861
Library mostly complete. Need to clean up code and document.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jakowisp | 1:d8589d1f368c | 1 | #include "mbed.h" |
jakowisp | 1:d8589d1f368c | 2 | #include "Max7221.h" |
jakowisp | 1:d8589d1f368c | 3 | |
jakowisp | 1:d8589d1f368c | 4 | int Max7221::maxInUseSPI1 = 0; |
jakowisp | 1:d8589d1f368c | 5 | int Max7221::maxInUseSPI2 = 0; |
jakowisp | 1:d8589d1f368c | 6 | SPI *Max7221::spi1=NULL; |
jakowisp | 1:d8589d1f368c | 7 | SPI *Max7221::spi2=NULL; |
jakowisp | 1:d8589d1f368c | 8 | DigitalOut *Max7221::load1=NULL; |
jakowisp | 1:d8589d1f368c | 9 | DigitalOut *Max7221::load2=NULL; |
jakowisp | 1:d8589d1f368c | 10 | |
jakowisp | 1:d8589d1f368c | 11 | Max7221::Max7221(PinName msoi, PinName mclk, PinName load){ |
jakowisp | 1:d8589d1f368c | 12 | switch (msoi) { |
jakowisp | 1:d8589d1f368c | 13 | case p5: maxInUseSPI1++; |
jakowisp | 1:d8589d1f368c | 14 | this->id=maxInUseSPI1; |
jakowisp | 1:d8589d1f368c | 15 | this->maxInUse=&maxInUseSPI1; |
jakowisp | 1:d8589d1f368c | 16 | if (spi1 ==NULL) { |
jakowisp | 1:d8589d1f368c | 17 | spi1 = new SPI(msoi, NC, mclk); |
jakowisp | 1:d8589d1f368c | 18 | load1 = new DigitalOut(load); |
jakowisp | 1:d8589d1f368c | 19 | } else { |
jakowisp | 1:d8589d1f368c | 20 | //TODO: Check that load pin is the same for all SP2 |
jakowisp | 1:d8589d1f368c | 21 | } |
jakowisp | 1:d8589d1f368c | 22 | this->max72_spi=spi1; |
jakowisp | 1:d8589d1f368c | 23 | this->load = load2; |
jakowisp | 1:d8589d1f368c | 24 | break; |
jakowisp | 1:d8589d1f368c | 25 | case p11: maxInUseSPI2++; |
jakowisp | 1:d8589d1f368c | 26 | this->id=maxInUseSPI2; |
jakowisp | 1:d8589d1f368c | 27 | this->maxInUse=&maxInUseSPI2; |
jakowisp | 1:d8589d1f368c | 28 | if (spi2 ==NULL) { |
jakowisp | 1:d8589d1f368c | 29 | spi2 = new SPI(msoi, NC, mclk); |
jakowisp | 1:d8589d1f368c | 30 | load2 = new DigitalOut(load); |
jakowisp | 1:d8589d1f368c | 31 | } else { |
jakowisp | 1:d8589d1f368c | 32 | //TODO: Check that load pin is the same for all SP2 |
jakowisp | 1:d8589d1f368c | 33 | } |
jakowisp | 1:d8589d1f368c | 34 | this->max72_spi=spi2; |
jakowisp | 1:d8589d1f368c | 35 | this->load = load2; |
jakowisp | 1:d8589d1f368c | 36 | break; |
jakowisp | 1:d8589d1f368c | 37 | default: error("Not a SPI port"); |
jakowisp | 1:d8589d1f368c | 38 | } |
jakowisp | 1:d8589d1f368c | 39 | |
jakowisp | 1:d8589d1f368c | 40 | } |
jakowisp | 1:d8589d1f368c | 41 | |
jakowisp | 1:d8589d1f368c | 42 | void Max7221::Write( unsigned int reg, unsigned int col) { |
jakowisp | 1:d8589d1f368c | 43 | //maxOne is for adressing different MAX7219's, |
jakowisp | 1:d8589d1f368c | 44 | //while having a couple of them cascaded |
jakowisp | 1:d8589d1f368c | 45 | int c = 0; |
jakowisp | 1:d8589d1f368c | 46 | *load = LOW; |
jakowisp | 1:d8589d1f368c | 47 | |
jakowisp | 1:d8589d1f368c | 48 | for ( c = *maxInUse; c > this->id; c--) { |
jakowisp | 1:d8589d1f368c | 49 | max72_spi->write(0); // no-op |
jakowisp | 1:d8589d1f368c | 50 | max72_spi->write(0); // no-op |
jakowisp | 1:d8589d1f368c | 51 | } |
jakowisp | 1:d8589d1f368c | 52 | |
jakowisp | 1:d8589d1f368c | 53 | max72_spi->write(reg); // specify register |
jakowisp | 1:d8589d1f368c | 54 | max72_spi->write(col); // put data |
jakowisp | 1:d8589d1f368c | 55 | |
jakowisp | 1:d8589d1f368c | 56 | for ( c=this->id-1; c >= 1; c--) { |
jakowisp | 1:d8589d1f368c | 57 | max72_spi->write(0); // no-op |
jakowisp | 1:d8589d1f368c | 58 | max72_spi->write(0); // no-op |
jakowisp | 1:d8589d1f368c | 59 | } |
jakowisp | 1:d8589d1f368c | 60 | *load = HIGH; |
jakowisp | 1:d8589d1f368c | 61 | } |
jakowisp | 1:d8589d1f368c | 62 | |
jakowisp | 1:d8589d1f368c | 63 | void Max7221::Setup () { |
jakowisp | 1:d8589d1f368c | 64 | // initiation of the max 7219 |
jakowisp | 1:d8589d1f368c | 65 | // SPI setup: 8 bits, mode 0 |
jakowisp | 1:d8589d1f368c | 66 | max72_spi->format(8, 0); |
jakowisp | 1:d8589d1f368c | 67 | |
jakowisp | 1:d8589d1f368c | 68 | // going by the datasheet, min clk is 100ns so theoretically 10MHz should work... |
jakowisp | 1:d8589d1f368c | 69 | max72_spi->frequency(10*MHZ); |
jakowisp | 1:d8589d1f368c | 70 | |
jakowisp | 1:d8589d1f368c | 71 | Write(max7219_reg_scanLimit, 0x07); |
jakowisp | 1:d8589d1f368c | 72 | Write(max7219_reg_decodeMode, 0xff); // using an led matrix (not digits) |
jakowisp | 1:d8589d1f368c | 73 | Write(max7219_reg_shutdown, 0x01); // not in shutdown mode |
jakowisp | 1:d8589d1f368c | 74 | Write(max7219_reg_displayTest, 0x00); // no display test |
jakowisp | 1:d8589d1f368c | 75 | for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off |
jakowisp | 1:d8589d1f368c | 76 | Write(e,0xf); |
jakowisp | 1:d8589d1f368c | 77 | } |
jakowisp | 1:d8589d1f368c | 78 | Write(max7219_reg_intensity, 0x01 & 0x0f); // the first 0x0f is the value you can set |
jakowisp | 1:d8589d1f368c | 79 | // range: 0x00 to 0x0f |
jakowisp | 1:d8589d1f368c | 80 | } |
jakowisp | 1:d8589d1f368c | 81 | |
jakowisp | 1:d8589d1f368c | 82 | |
jakowisp | 1:d8589d1f368c | 83 | void Max7221::WriteAll (unsigned int reg, unsigned int col) { // initialize all MAX7219's in the system |
jakowisp | 1:d8589d1f368c | 84 | if(load1 !=NULL) { |
jakowisp | 1:d8589d1f368c | 85 | *load1 = LOW; // begin |
jakowisp | 1:d8589d1f368c | 86 | for ( int c=1; c<= maxInUseSPI1; c++) { |
jakowisp | 1:d8589d1f368c | 87 | spi1->write(reg); // specify register |
jakowisp | 1:d8589d1f368c | 88 | spi1->write(col); // put data |
jakowisp | 1:d8589d1f368c | 89 | } |
jakowisp | 1:d8589d1f368c | 90 | *load1 = HIGH; |
jakowisp | 1:d8589d1f368c | 91 | } |
jakowisp | 1:d8589d1f368c | 92 | if(load2 !=NULL) { |
jakowisp | 1:d8589d1f368c | 93 | *load2 = LOW; |
jakowisp | 1:d8589d1f368c | 94 | for ( int c=1; c<= maxInUseSPI2; c++) { |
jakowisp | 1:d8589d1f368c | 95 | spi2->write(reg); // specify register |
jakowisp | 1:d8589d1f368c | 96 | spi2->write(col); // put data |
jakowisp | 1:d8589d1f368c | 97 | } |
jakowisp | 1:d8589d1f368c | 98 | *load2 = HIGH; |
jakowisp | 1:d8589d1f368c | 99 | } |
jakowisp | 1:d8589d1f368c | 100 | } |
jakowisp | 1:d8589d1f368c | 101 | |
jakowisp | 1:d8589d1f368c | 102 | void Max7221::WriteInt( int value ){ |
jakowisp | 1:d8589d1f368c | 103 | char buffer[16]; |
jakowisp | 1:d8589d1f368c | 104 | |
jakowisp | 1:d8589d1f368c | 105 | //TODO:SET UPPERBOUND AND LOWERBOUND based on NUMDIGITS |
jakowisp | 1:d8589d1f368c | 106 | if (value <= UPPERBOUND && value >= LOWERBOUND) { |
jakowisp | 1:d8589d1f368c | 107 | sprintf(buffer,"%8d",value); |
jakowisp | 1:d8589d1f368c | 108 | } else { |
jakowisp | 1:d8589d1f368c | 109 | sprintf(buffer,"--------"); |
jakowisp | 1:d8589d1f368c | 110 | } |
jakowisp | 1:d8589d1f368c | 111 | |
jakowisp | 1:d8589d1f368c | 112 | Write(max7219_reg_decodeMode, 0xff); |
jakowisp | 1:d8589d1f368c | 113 | for (int i=0;i<NUMDIGITS;i++) { |
jakowisp | 1:d8589d1f368c | 114 | switch(buffer[i]){ |
jakowisp | 1:d8589d1f368c | 115 | case 0x2d: buffer[i]=0xa; break; |
jakowisp | 1:d8589d1f368c | 116 | case 0x20: buffer[i]=0xf; break; |
jakowisp | 1:d8589d1f368c | 117 | default: buffer[i]= buffer[i] & 0x0f; |
jakowisp | 1:d8589d1f368c | 118 | } |
jakowisp | 1:d8589d1f368c | 119 | Write(NUMDIGITS-i,buffer[i]); |
jakowisp | 1:d8589d1f368c | 120 | } |
jakowisp | 1:d8589d1f368c | 121 | } |
jakowisp | 1:d8589d1f368c | 122 | |
jakowisp | 1:d8589d1f368c | 123 | void Max7221::WriteFloat( float value ){ |
jakowisp | 1:d8589d1f368c | 124 | char buffer[32]; |
jakowisp | 1:d8589d1f368c | 125 | int ptr=-1,len; |
jakowisp | 1:d8589d1f368c | 126 | int i; |
jakowisp | 1:d8589d1f368c | 127 | |
jakowisp | 1:d8589d1f368c | 128 | sprintf(buffer,"%f",value); |
jakowisp | 1:d8589d1f368c | 129 | len=strlen(buffer); |
jakowisp | 1:d8589d1f368c | 130 | i=len-1; |
jakowisp | 1:d8589d1f368c | 131 | while(buffer[i]==0x30) { |
jakowisp | 1:d8589d1f368c | 132 | buffer[i]='\0'; |
jakowisp | 1:d8589d1f368c | 133 | i--; |
jakowisp | 1:d8589d1f368c | 134 | len--; |
jakowisp | 1:d8589d1f368c | 135 | } |
jakowisp | 1:d8589d1f368c | 136 | for( i =0; i<=len; i++) { |
jakowisp | 1:d8589d1f368c | 137 | switch(buffer[i]){ |
jakowisp | 1:d8589d1f368c | 138 | case 0x2d: buffer[i]=0xa; break; |
jakowisp | 1:d8589d1f368c | 139 | case 0x20: buffer[i]=0xf; break; |
jakowisp | 1:d8589d1f368c | 140 | case 0x2e: buffer[i]=buffer[i-1] | 0x80; |
jakowisp | 1:d8589d1f368c | 141 | ptr = i-1; |
jakowisp | 1:d8589d1f368c | 142 | break; |
jakowisp | 1:d8589d1f368c | 143 | default: buffer[i]= buffer[i]; |
jakowisp | 1:d8589d1f368c | 144 | } |
jakowisp | 1:d8589d1f368c | 145 | if (ptr != -1) { |
jakowisp | 1:d8589d1f368c | 146 | buffer[i-1]=buffer[i]; |
jakowisp | 1:d8589d1f368c | 147 | } |
jakowisp | 1:d8589d1f368c | 148 | } |
jakowisp | 1:d8589d1f368c | 149 | |
jakowisp | 1:d8589d1f368c | 150 | len=strlen(buffer); |
jakowisp | 1:d8589d1f368c | 151 | Write(max7219_reg_decodeMode, 0xff); |
jakowisp | 1:d8589d1f368c | 152 | |
jakowisp | 1:d8589d1f368c | 153 | // If too large for display set to '-' |
jakowisp | 1:d8589d1f368c | 154 | if(len > NUMDIGITS && (ptr==-1 || ptr>NUMDIGITS)) |
jakowisp | 1:d8589d1f368c | 155 | for (int i=0;i<NUMDIGITS;i++) { |
jakowisp | 1:d8589d1f368c | 156 | buffer[i]=0x0a; |
jakowisp | 1:d8589d1f368c | 157 | } |
jakowisp | 1:d8589d1f368c | 158 | //if number is smaller than display, fill with ' ' |
jakowisp | 1:d8589d1f368c | 159 | if (len<=NUMDIGITS) { |
jakowisp | 1:d8589d1f368c | 160 | for (int i=1;i<=NUMDIGITS;i++) { |
jakowisp | 1:d8589d1f368c | 161 | if(len-i>=0) { |
jakowisp | 1:d8589d1f368c | 162 | Write(i,buffer[len-i]); |
jakowisp | 1:d8589d1f368c | 163 | } else { |
jakowisp | 1:d8589d1f368c | 164 | Write(i,0xf); |
jakowisp | 1:d8589d1f368c | 165 | } |
jakowisp | 1:d8589d1f368c | 166 | } |
jakowisp | 1:d8589d1f368c | 167 | } else { |
jakowisp | 1:d8589d1f368c | 168 | //Write out the buffer, truncating the decimal digits if larger than display |
jakowisp | 1:d8589d1f368c | 169 | for (int i=0;i<NUMDIGITS;i++) { |
jakowisp | 1:d8589d1f368c | 170 | Write(NUMDIGITS-i,buffer[i]); |
jakowisp | 1:d8589d1f368c | 171 | } |
jakowisp | 1:d8589d1f368c | 172 | } |
jakowisp | 1:d8589d1f368c | 173 | } |
jakowisp | 1:d8589d1f368c | 174 | |
jakowisp | 1:d8589d1f368c | 175 | void Max7221::SetupAll () { |
jakowisp | 1:d8589d1f368c | 176 | // initiation of the max 7219 |
jakowisp | 1:d8589d1f368c | 177 | // SPI setup: 8 bits, mode 0 |
jakowisp | 1:d8589d1f368c | 178 | if(spi1!=NULL) { |
jakowisp | 1:d8589d1f368c | 179 | spi1->format(8, 0); |
jakowisp | 1:d8589d1f368c | 180 | spi1->frequency(10*MHZ); |
jakowisp | 1:d8589d1f368c | 181 | } |
jakowisp | 1:d8589d1f368c | 182 | if(spi2!=NULL) { |
jakowisp | 1:d8589d1f368c | 183 | spi2->format(8, 0); |
jakowisp | 1:d8589d1f368c | 184 | spi2->frequency(10*MHZ); |
jakowisp | 1:d8589d1f368c | 185 | } |
jakowisp | 1:d8589d1f368c | 186 | WriteAll(max7219_reg_scanLimit, 0x07); |
jakowisp | 1:d8589d1f368c | 187 | WriteAll(max7219_reg_decodeMode, 0xff); // using an led matrix (not digits) |
jakowisp | 1:d8589d1f368c | 188 | WriteAll(max7219_reg_shutdown, 0x01); // not in shutdown mode |
jakowisp | 1:d8589d1f368c | 189 | WriteAll(max7219_reg_displayTest, 0x00); // no display test |
jakowisp | 1:d8589d1f368c | 190 | for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off |
jakowisp | 1:d8589d1f368c | 191 | WriteAll(e,0xf); |
jakowisp | 1:d8589d1f368c | 192 | } |
jakowisp | 1:d8589d1f368c | 193 | WriteAll(max7219_reg_intensity, 0x01 & 0x0f); // the first 0x0f is the value you can set |
jakowisp | 1:d8589d1f368c | 194 | // range: 0x00 to 0x0f |
jakowisp | 1:d8589d1f368c | 195 | } |