Components and supplies
1
Arduino Nano R3
1
Maxx7219 Display unit
Apps and platforms
1
Arduino IDE
Project description
Code
Basic demonstration program
c_cpp
1//We always have to include the library 2#include "LedControl.h" 3 4/* 5 Now we need a LedControl to work with. 6 ***** These pin numbers will probably not work with your hardware ***** 7 pin xx is connected to the DataIn 8 pin xx is connected to the CLK 9 pin xx is connected to LOAD 10 We have 2 MAX72XX. 11 */ 12// Matrix 13#define PIN_DATAIN 11 14#define PIN_CLK 13 15#define PIN_LOAD 10 16 17LedControl lc = LedControl(PIN_DATAIN, PIN_CLK, PIN_LOAD, 2); 18 19/* we always wait a bit between updates of the display */ 20unsigned long delaytime=100; 21 22void setup() { 23 /* 24 The MAX72XX is in power-saving mode on startup, 25 we have to do a wakeup call 26 */ 27 lc.shutdown(0,false); 28 /* Set the brightness to a medium values */ 29 lc.setIntensity(0,8); 30 /* and clear the display */ 31 lc.clearDisplay(0); 32} 33 34 35 36void loop() { 37 38 //lc.clearDisplay(0); 39 lc.setLed(0,0,0,true);//setLed(int addr, int row, int column, boolean state) 40 lc.setLed(0,2,1,true);//setLed(int addr, int row, int column, boolean state) 41 //delay(1000); 42 43 //lc.clearDisplay(0); 44 lc.setRow(0,1,B00001111);//setRow(int addr, int row, byte value) 45 //delay(1000); 46 47 //lc.clearDisplay(0); 48 lc.setColumn(0,2,B00000111);//setColumn(int addr, int col, byte value) 49 delay(100000); 50 51} 52
The modified LedControl library
c_cpp
1/* 2 * LedControl.cpp - A library for controling Leds with a MAX7219/MAX7221 3 * Copyright (c) 2007 Eberhard Fahle 4 * 5 * Permission is hereby granted, free of charge, to any person 6 * obtaining a copy of this software and associated documentation 7 * files (the "Software"), to deal in the Software without 8 * restriction, including without limitation the rights to use, 9 * copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following 12 * conditions: 13 * 14 * This permission notice shall be included in all copies or 15 * substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 * OTHER DEALINGS IN THE SOFTWARE. 25 */ 26 27 28#include "LedControl.h" 29 30 31// Modified to allow display rotation 32#define normal 33//#define clockwise_90 34//#define anti_clockwise_90 35//#define one_eighty 36 37 38//the opcodes for the MAX7221 and MAX7219 39#define OP_NOOP 0 40#define OP_DIGIT0 1 41#define OP_DIGIT1 2 42#define OP_DIGIT2 3 43#define OP_DIGIT3 4 44#define OP_DIGIT4 5 45#define OP_DIGIT5 6 46#define OP_DIGIT6 7 47#define OP_DIGIT7 8 48#define OP_DECODEMODE 9 49#define OP_INTENSITY 10 50#define OP_SCANLIMIT 11 51#define OP_SHUTDOWN 12 52#define OP_DISPLAYTEST 15 53 54LedControl::LedControl(int dataPin, int clkPin, int csPin, int numDevices) { 55 SPI_MOSI=dataPin; 56 SPI_CLK=clkPin; 57 SPI_CS=csPin; 58 if(numDevices<=0 || numDevices>8 ) 59 numDevices=8; 60 maxDevices=numDevices; 61 pinMode(SPI_MOSI,OUTPUT); 62 pinMode(SPI_CLK,OUTPUT); 63 pinMode(SPI_CS,OUTPUT); 64 digitalWrite(SPI_CS,HIGH); 65 SPI_MOSI=dataPin; 66 for(int i=0;i<64;i++) 67 status[i]=0x00; 68 for(int i=0;i<maxDevices;i++) { 69 spiTransfer(i,OP_DISPLAYTEST,0); 70 //scanlimit is set to max on startup 71 setScanLimit(i,7); 72 //decode is done in source 73 spiTransfer(i,OP_DECODEMODE,0); 74 clearDisplay(i); 75 //we go into shutdown-mode on startup 76 shutdown(i,true); 77 } 78} 79 80int LedControl::getDeviceCount() { 81 return maxDevices; 82} 83 84void LedControl::shutdown(int addr, bool b) { 85 if(addr<0 || addr>=maxDevices) 86 return; 87 if(b) 88 spiTransfer(addr, OP_SHUTDOWN,0); 89 else 90 spiTransfer(addr, OP_SHUTDOWN,1); 91} 92 93void LedControl::setScanLimit(int addr, int limit) { 94 if(addr<0 || addr>=maxDevices) 95 return; 96 if(limit>=0 && limit<8) 97 spiTransfer(addr, OP_SCANLIMIT,limit); 98} 99 100void LedControl::setIntensity(int addr, int intensity) { 101 if(addr<0 || addr>=maxDevices) 102 return; 103 if(intensity>=0 && intensity<16) 104 spiTransfer(addr, OP_INTENSITY,intensity); 105} 106 107void LedControl::clearDisplay(int addr) { 108 int offset; 109 110 if(addr<0 || addr>=maxDevices) 111 return; 112 offset=addr*8; 113 for(int i=0;i<8;i++) { 114 status[offset+i]=0; 115 spiTransfer(addr, i+1,status[offset+i]); 116 } 117} 118#ifdef normal 119void LedControl::setLed(int addr, int row, int column, boolean state) { 120 int offset; 121 byte val=0x00; 122 123 if(addr<0 || addr>=maxDevices) 124 return; 125 if(row<0 || row>7 || column<0 || column>7) 126 return; 127 offset=addr*8; 128 val=B10000000 >> column; 129 if(state) 130 status[offset+row]=status[offset+row]|val; 131 else { 132 val=~val; 133 status[offset+row]=status[offset+row]&val; 134 } 135 spiTransfer(addr, row+1,status[offset+row]); 136} 137 138void LedControl::setRow(int addr, int row, byte value) { 139 int offset; 140 if(addr<0 || addr>=maxDevices) 141 return; 142 if(row<0 || row>7) 143 return; 144 offset=addr*8; 145 status[offset+row]=value; 146 spiTransfer(addr, row+1,status[offset+row]); 147} 148 149void LedControl::setColumn(int addr, int col, byte value) { 150 byte val; 151 152 if(addr<0 || addr>=maxDevices) 153 return; 154 if(col<0 || col>7) 155 return; 156 for(int row=0;row<8;row++) { 157 val=value >> (7-row); 158 val=val & 0x01; 159 setLed(addr,row,col,val); 160 } 161} 162#endif 163 164#ifdef clockwise_90 165// rotate display 90 degrees clockwise 166void LedControl::setLed(int addr, int row, int column, boolean state) { 167 168 int temp=0; 169 temp=row; 170 row=column; 171 column=temp; 172 173 column=7-column; 174 175 int offset; 176 byte val=0x00; 177 178 if(addr<0 || addr>=maxDevices) 179 return; 180 if(row<0 || row>7 || column<0 || column>7) 181 return; 182 offset=addr*8; 183 val=B10000000 >> column; 184 if(state) 185 status[offset+row]=status[offset+row]|val; 186 else { 187 val=~val; 188 status[offset+row]=status[offset+row]&val; 189 } 190 spiTransfer(addr, row+1,status[offset+row]); 191} 192 193void LedControl::setColumn(int addr, int col, byte value) { 194 195 int row=col; 196 197//byte reversal code 198 byte temp=B00000000; 199 200 if(value&B10000000){temp|=1<<0;} 201 if(value&B01000000){temp|=1<<1;} 202 if(value&B00100000){temp|=1<<2;} 203 if(value&B00010000){temp|=1<<3;} 204 if(value&B00001000){temp|=1<<4;} 205 if(value&B00000100){temp|=1<<5;} 206 if(value&B00000010){temp|=1<<6;} 207 if(value&B00000001){temp|=1<<7;} 208 209 value=temp; 210//end byte reversal code 211 212 int offset; 213 if(addr<0 || addr>=maxDevices) 214 return; 215 if(row<0 || row>7) 216 return; 217 offset=addr*8; 218 status[offset+row]=value; 219 spiTransfer(addr, row+1,status[offset+row]); 220} 221 222void LedControl::setRow(int addr, int row, byte value) { 223 224 int col=row; 225 226 byte val; 227 228 if(addr<0 || addr>=maxDevices) 229 return; 230 if(col<0 || col>7) 231 return; 232 for(int row=0;row<8;row++) { 233 val=value >> (7-row); 234 val=val & 0x01; 235 setLed(addr,col,row,val); 236 } 237} 238#endif 239 240#ifdef anti_clockwise_90 241//rotate display 270 degrees (90 degrees counter clockwise) 242void LedControl::setLed(int addr, int row, int column, boolean state) { 243 244 int temp=row; 245 row=column; 246 column=temp; 247 248 row=7-row; 249 250 int offset; 251 byte val=0x00; 252 253 if(addr<0 || addr>=maxDevices) 254 return; 255 if(row<0 || row>7 || column<0 || column>7) 256 return; 257 offset=addr*8; 258 val=B10000000 >> column; 259 if(state) 260 status[offset+row]=status[offset+row]|val; 261 else { 262 val=~val; 263 status[offset+row]=status[offset+row]&val; 264 } 265 spiTransfer(addr, row+1,status[offset+row]); 266} 267 268void LedControl::setColumn(int addr, int col, byte value) { 269 270 int row=col; 271 272 row=7-row; 273 274 int offset; 275 if(addr<0 || addr>=maxDevices) 276 return; 277 if(row<0 || row>7) 278 return; 279 offset=addr*8; 280 status[offset+row]=value; 281 spiTransfer(addr, row+1,status[offset+row]); 282} 283 284void LedControl::setRow(int addr, int row, byte value) { 285 286 int col=row; 287 288 byte val; 289 290 if(addr<0 || addr>=maxDevices) 291 return; 292 if(col<0 || col>7) 293 return; 294 for(int row=0;row<8;row++) { 295 val=value >> (7-row); 296 val=val & 0x01; 297 setLed(addr,col,row,val); 298 } 299} 300#endif 301 302#ifdef one_eighty 303// rotate display 180 degrees 304void LedControl::setLed(int addr, int row, int column, boolean state) { 305 306 307 row=7-row; 308 column=7-column; 309 310 int offset; 311 byte val=0x00; 312 313 if(addr<0 || addr>=maxDevices) 314 return; 315 if(row<0 || row>7 || column<0 || column>7) 316 return; 317 offset=addr*8; 318 val=B10000000 >> column; 319 if(state) 320 status[offset+row]=status[offset+row]|val; 321 else { 322 val=~val; 323 status[offset+row]=status[offset+row]&val; 324 } 325 spiTransfer(addr, row+1,status[offset+row]); 326} 327 328void LedControl::setRow(int addr, int row, byte value) { 329 330 row=7-row; 331 332//byte reversal code 333 byte temp=B00000000; 334 335 if(value&B10000000){temp|=1<<0;} 336 if(value&B01000000){temp|=1<<1;} 337 if(value&B00100000){temp|=1<<2;} 338 if(value&B00010000){temp|=1<<3;} 339 if(value&B00001000){temp|=1<<4;} 340 if(value&B00000100){temp|=1<<5;} 341 if(value&B00000010){temp|=1<<6;} 342 if(value&B00000001){temp|=1<<7;} 343 344 value=temp; 345//end byte reversal code 346 347 int offset; 348 if(addr<0 || addr>=maxDevices) 349 return; 350 if(row<0 || row>7) 351 return; 352 offset=addr*8; 353 status[offset+row]=value; 354 spiTransfer(addr, row+1,status[offset+row]); 355} 356 357void LedControl::setColumn(int addr, int col, byte value) { 358 byte val; 359 360 if(addr<0 || addr>=maxDevices) 361 return; 362 if(col<0 || col>7) 363 return; 364 for(int row=0;row<8;row++) { 365 val=value >> (7-row); 366 val=val & 0x01; 367 setLed(addr,row,col,val); 368 } 369} 370 371#endif 372 373 374 375 376// The seven segment display functions have not been altered 377void LedControl::setDigit(int addr, int digit, byte value, boolean dp) { 378 int offset; 379 byte v; 380 381 if(addr<0 || addr>=maxDevices) 382 return; 383 if(digit<0 || digit>7 || value>15) 384 return; 385 offset=addr*8; 386 v=pgm_read_byte_near(charTable + value); 387 if(dp) 388 v|=B10000000; 389 status[offset+digit]=v; 390 spiTransfer(addr, digit+1,v); 391} 392 393void LedControl::setChar(int addr, int digit, char value, boolean dp) { 394 int offset; 395 byte index,v; 396 397 if(addr<0 || addr>=maxDevices) 398 return; 399 if(digit<0 || digit>7) 400 return; 401 offset=addr*8; 402 index=(byte)value; 403 if(index >127) { 404 //no defined beyond index 127, so we use the space char 405 index=32; 406 } 407 v=pgm_read_byte_near(charTable + index); 408 if(dp) 409 v|=B10000000; 410 status[offset+digit]=v; 411 spiTransfer(addr, digit+1,v); 412} 413 414void LedControl::spiTransfer(int addr, volatile byte opcode, volatile byte data) { 415 //Create an array with the data to shift out 416 int offset=addr*2; 417 int maxbytes=maxDevices*2; 418 419 for(int i=0;i<maxbytes;i++) 420 spidata[i]=(byte)0; 421 //put our device data into the array 422 spidata[offset+1]=opcode; 423 spidata[offset]=data; 424 //enable the line 425 digitalWrite(SPI_CS,LOW); 426 //Now shift out the data 427 for(int i=maxbytes;i>0;i--) 428 shiftOut(SPI_MOSI,SPI_CLK,MSBFIRST,spidata[i-1]); 429 //latch the data onto the display 430 digitalWrite(SPI_CS,HIGH); 431} 432 433 434
The modified LedControl library
c_cpp
1/* 2 * LedControl.cpp - A library for controling Leds with a MAX7219/MAX7221 3 4 * Copyright (c) 2007 Eberhard Fahle 5 * 6 * Permission is hereby granted, 7 free of charge, to any person 8 * obtaining a copy of this software and associated 9 documentation 10 * files (the "Software"), to deal in the Software without 11 12 * restriction, including without limitation the rights to use, 13 * copy, 14 modify, merge, publish, distribute, sublicense, and/or sell 15 * copies of the 16 Software, and to permit persons to whom the 17 * Software is furnished to do 18 so, subject to the following 19 * conditions: 20 * 21 * This permission 22 notice shall be included in all copies or 23 * substantial portions of the 24 Software. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 27 ANY KIND, 28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 29 30 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 31 * NONINFRINGEMENT. 32 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 33 * HOLDERS BE LIABLE FOR ANY CLAIM, 34 DAMAGES OR OTHER LIABILITY, 35 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 36 ARISING 37 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 38 39 * OTHER DEALINGS IN THE SOFTWARE. 40 */ 41 42 43#include "LedControl.h" 44 45 46// 47 Modified to allow display rotation 48#define normal 49//#define clockwise_90 50//#define 51 anti_clockwise_90 52//#define one_eighty 53 54 55//the opcodes for the MAX7221 56 and MAX7219 57#define OP_NOOP 0 58#define OP_DIGIT0 1 59#define OP_DIGIT1 2 60#define 61 OP_DIGIT2 3 62#define OP_DIGIT3 4 63#define OP_DIGIT4 5 64#define OP_DIGIT5 6 65#define 66 OP_DIGIT6 7 67#define OP_DIGIT7 8 68#define OP_DECODEMODE 9 69#define OP_INTENSITY 70 10 71#define OP_SCANLIMIT 11 72#define OP_SHUTDOWN 12 73#define OP_DISPLAYTEST 74 15 75 76LedControl::LedControl(int dataPin, int clkPin, int csPin, int numDevices) 77 { 78 SPI_MOSI=dataPin; 79 SPI_CLK=clkPin; 80 SPI_CS=csPin; 81 if(numDevices<=0 82 || numDevices>8 ) 83 numDevices=8; 84 maxDevices=numDevices; 85 pinMode(SPI_MOSI,OUTPUT); 86 87 pinMode(SPI_CLK,OUTPUT); 88 pinMode(SPI_CS,OUTPUT); 89 digitalWrite(SPI_CS,HIGH); 90 91 SPI_MOSI=dataPin; 92 for(int i=0;i<64;i++) 93 status[i]=0x00; 94 95 for(int i=0;i<maxDevices;i++) { 96 spiTransfer(i,OP_DISPLAYTEST,0); 97 98 //scanlimit is set to max on startup 99 setScanLimit(i,7); 100 //decode 101 is done in source 102 spiTransfer(i,OP_DECODEMODE,0); 103 clearDisplay(i); 104 105 //we go into shutdown-mode on startup 106 shutdown(i,true); 107 } 108} 109 110int 111 LedControl::getDeviceCount() { 112 return maxDevices; 113} 114 115void LedControl::shutdown(int 116 addr, bool b) { 117 if(addr<0 || addr>=maxDevices) 118 return; 119 if(b) 120 121 spiTransfer(addr, OP_SHUTDOWN,0); 122 else 123 spiTransfer(addr, 124 OP_SHUTDOWN,1); 125} 126 127void LedControl::setScanLimit(int addr, int limit) { 128 129 if(addr<0 || addr>=maxDevices) 130 return; 131 if(limit>=0 && limit<8) 132 133 spiTransfer(addr, OP_SCANLIMIT,limit); 134} 135 136void LedControl::setIntensity(int 137 addr, int intensity) { 138 if(addr<0 || addr>=maxDevices) 139 return; 140 141 if(intensity>=0 && intensity<16) 142 spiTransfer(addr, OP_INTENSITY,intensity); 143} 144 145void 146 LedControl::clearDisplay(int addr) { 147 int offset; 148 149 if(addr<0 || 150 addr>=maxDevices) 151 return; 152 offset=addr*8; 153 for(int i=0;i<8;i++) 154 { 155 status[offset+i]=0; 156 spiTransfer(addr, i+1,status[offset+i]); 157 158 } 159} 160#ifdef normal 161void LedControl::setLed(int addr, int row, int column, 162 boolean state) { 163 int offset; 164 byte val=0x00; 165 166 if(addr<0 || 167 addr>=maxDevices) 168 return; 169 if(row<0 || row>7 || column<0 || column>7) 170 171 return; 172 offset=addr*8; 173 val=B10000000 >> column; 174 if(state) 175 176 status[offset+row]=status[offset+row]|val; 177 else { 178 val=~val; 179 180 status[offset+row]=status[offset+row]&val; 181 } 182 spiTransfer(addr, 183 row+1,status[offset+row]); 184} 185 186void LedControl::setRow(int addr, int row, 187 byte value) { 188 int offset; 189 if(addr<0 || addr>=maxDevices) 190 return; 191 192 if(row<0 || row>7) 193 return; 194 offset=addr*8; 195 status[offset+row]=value; 196 197 spiTransfer(addr, row+1,status[offset+row]); 198} 199 200void LedControl::setColumn(int 201 addr, int col, byte value) { 202 byte val; 203 204 if(addr<0 || addr>=maxDevices) 205 206 return; 207 if(col<0 || col>7) 208 return; 209 for(int row=0;row<8;row++) 210 { 211 val=value >> (7-row); 212 val=val & 0x01; 213 setLed(addr,row,col,val); 214 215 } 216} 217#endif 218 219#ifdef clockwise_90 220// rotate display 90 degrees 221 clockwise 222void LedControl::setLed(int addr, int row, int column, boolean state) 223 { 224 225 int temp=0; 226 temp=row; 227 row=column; 228 column=temp; 229 230 column=7-column; 231 232 233 int offset; 234 byte val=0x00; 235 236 if(addr<0 || addr>=maxDevices) 237 238 return; 239 if(row<0 || row>7 || column<0 || column>7) 240 return; 241 242 offset=addr*8; 243 val=B10000000 >> column; 244 if(state) 245 status[offset+row]=status[offset+row]|val; 246 247 else { 248 val=~val; 249 status[offset+row]=status[offset+row]&val; 250 251 } 252 spiTransfer(addr, row+1,status[offset+row]); 253} 254 255void LedControl::setColumn(int 256 addr, int col, byte value) { 257 258 int row=col; 259 260//byte reversal code 261 262 byte temp=B00000000; 263 264 if(value&B10000000){temp|=1<<0;} 265 if(value&B01000000){temp|=1<<1;} 266 267 if(value&B00100000){temp|=1<<2;} 268 if(value&B00010000){temp|=1<<3;} 269 270 if(value&B00001000){temp|=1<<4;} 271 if(value&B00000100){temp|=1<<5;} 272 273 if(value&B00000010){temp|=1<<6;} 274 if(value&B00000001){temp|=1<<7;} 275 276 277 value=temp; 278//end byte reversal code 279 280 int offset; 281 if(addr<0 282 || addr>=maxDevices) 283 return; 284 if(row<0 || row>7) 285 return; 286 287 offset=addr*8; 288 status[offset+row]=value; 289 spiTransfer(addr, row+1,status[offset+row]); 290} 291 292void 293 LedControl::setRow(int addr, int row, byte value) { 294 295 int col=row; 296 297 298 byte val; 299 300 if(addr<0 || addr>=maxDevices) 301 return; 302 if(col<0 303 || col>7) 304 return; 305 for(int row=0;row<8;row++) { 306 val=value 307 >> (7-row); 308 val=val & 0x01; 309 setLed(addr,col,row,val); 310 311 } 312} 313#endif 314 315#ifdef anti_clockwise_90 316//rotate display 270 degrees 317 (90 degrees counter clockwise) 318void LedControl::setLed(int addr, int row, int 319 column, boolean state) { 320 321 int temp=row; 322 row=column; 323 column=temp; 324 325 row=7-row; 326 327 328 int offset; 329 byte val=0x00; 330 331 if(addr<0 || addr>=maxDevices) 332 333 return; 334 if(row<0 || row>7 || column<0 || column>7) 335 return; 336 337 offset=addr*8; 338 val=B10000000 >> column; 339 if(state) 340 status[offset+row]=status[offset+row]|val; 341 342 else { 343 val=~val; 344 status[offset+row]=status[offset+row]&val; 345 346 } 347 spiTransfer(addr, row+1,status[offset+row]); 348} 349 350void LedControl::setColumn(int 351 addr, int col, byte value) { 352 353 int row=col; 354 355 row=7-row; 356 357 int 358 offset; 359 if(addr<0 || addr>=maxDevices) 360 return; 361 if(row<0 362 || row>7) 363 return; 364 offset=addr*8; 365 status[offset+row]=value; 366 367 spiTransfer(addr, row+1,status[offset+row]); 368} 369 370void LedControl::setRow(int 371 addr, int row, byte value) { 372 373 int col=row; 374 375 byte val; 376 377 378 if(addr<0 || addr>=maxDevices) 379 return; 380 if(col<0 || col>7) 381 382 return; 383 for(int row=0;row<8;row++) { 384 val=value >> 385 (7-row); 386 val=val & 0x01; 387 setLed(addr,col,row,val); 388 } 389} 390#endif 391 392#ifdef 393 one_eighty 394// rotate display 180 degrees 395void LedControl::setLed(int addr, 396 int row, int column, boolean state) { 397 398 399 row=7-row; 400 column=7-column; 401 402 403 int offset; 404 byte val=0x00; 405 406 if(addr<0 || addr>=maxDevices) 407 408 return; 409 if(row<0 || row>7 || column<0 || column>7) 410 return; 411 412 offset=addr*8; 413 val=B10000000 >> column; 414 if(state) 415 status[offset+row]=status[offset+row]|val; 416 417 else { 418 val=~val; 419 status[offset+row]=status[offset+row]&val; 420 421 } 422 spiTransfer(addr, row+1,status[offset+row]); 423} 424 425void LedControl::setRow(int 426 addr, int row, byte value) { 427 428 row=7-row; 429 430//byte reversal code 431 432 byte temp=B00000000; 433 434 if(value&B10000000){temp|=1<<0;} 435 if(value&B01000000){temp|=1<<1;} 436 437 if(value&B00100000){temp|=1<<2;} 438 if(value&B00010000){temp|=1<<3;} 439 440 if(value&B00001000){temp|=1<<4;} 441 if(value&B00000100){temp|=1<<5;} 442 443 if(value&B00000010){temp|=1<<6;} 444 if(value&B00000001){temp|=1<<7;} 445 446 447 value=temp; 448//end byte reversal code 449 450 int offset; 451 if(addr<0 452 || addr>=maxDevices) 453 return; 454 if(row<0 || row>7) 455 return; 456 457 offset=addr*8; 458 status[offset+row]=value; 459 spiTransfer(addr, row+1,status[offset+row]); 460} 461 462void 463 LedControl::setColumn(int addr, int col, byte value) { 464 byte val; 465 466 467 if(addr<0 || addr>=maxDevices) 468 return; 469 if(col<0 || col>7) 470 471 return; 472 for(int row=0;row<8;row++) { 473 val=value >> 474 (7-row); 475 val=val & 0x01; 476 setLed(addr,row,col,val); 477 } 478} 479 480#endif 481 482 483 484 485// 486 The seven segment display functions have not been altered 487void LedControl::setDigit(int 488 addr, int digit, byte value, boolean dp) { 489 int offset; 490 byte v; 491 492 493 if(addr<0 || addr>=maxDevices) 494 return; 495 if(digit<0 || digit>7 496 || value>15) 497 return; 498 offset=addr*8; 499 v=pgm_read_byte_near(charTable 500 + value); 501 if(dp) 502 v|=B10000000; 503 status[offset+digit]=v; 504 505 spiTransfer(addr, digit+1,v); 506} 507 508void LedControl::setChar(int addr, 509 int digit, char value, boolean dp) { 510 int offset; 511 byte index,v; 512 513 514 if(addr<0 || addr>=maxDevices) 515 return; 516 if(digit<0 || digit>7) 517 518 return; 519 offset=addr*8; 520 index=(byte)value; 521 if(index 522 >127) { 523 //no defined beyond index 127, so we use the space char 524 index=32; 525 526 } 527 v=pgm_read_byte_near(charTable + index); 528 if(dp) 529 v|=B10000000; 530 531 status[offset+digit]=v; 532 spiTransfer(addr, digit+1,v); 533} 534 535void 536 LedControl::spiTransfer(int addr, volatile byte opcode, volatile byte data) { 537 538 //Create an array with the data to shift out 539 int offset=addr*2; 540 int 541 maxbytes=maxDevices*2; 542 543 for(int i=0;i<maxbytes;i++) 544 spidata[i]=(byte)0; 545 546 //put our device data into the array 547 spidata[offset+1]=opcode; 548 spidata[offset]=data; 549 550 //enable the line 551 digitalWrite(SPI_CS,LOW); 552 //Now shift out the 553 data 554 for(int i=maxbytes;i>0;i--) 555 shiftOut(SPI_MOSI,SPI_CLK,MSBFIRST,spidata[i-1]); 556 557 //latch the data onto the display 558 digitalWrite(SPI_CS,HIGH); 559} 560 561 562
Downloadable files
Max7219 to Nano circuit
Max7219 to Nano circuit

Max7219 to Nano circuit
Max7219 to Nano circuit

Comments
Only logged in users can leave comments