Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
max7219.cpp
00001 /******************************************************************//** 00002 * @file max7219.cpp 00003 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. 00004 * 00005 * Permission is hereby granted, free of charge, to any person obtaining a 00006 * copy of this software and associated documentation files (the "Software"), 00007 * to deal in the Software without restriction, including without limitation 00008 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00009 * and/or sell copies of the Software, and to permit persons to whom the 00010 * Software is furnished to do so, subject to the following conditions: 00011 * 00012 * The above copyright notice and this permission notice shall be included 00013 * in all copies or substantial portions of the Software. 00014 * 00015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00016 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00017 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00018 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES 00019 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00020 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00021 * OTHER DEALINGS IN THE SOFTWARE. 00022 * 00023 * Except as contained in this notice, the name of Maxim Integrated 00024 * Products, Inc. shall not be used except as stated in the Maxim Integrated 00025 * Products, Inc. Branding Policy. 00026 * 00027 * The mere transfer of this software does not imply any licenses 00028 * of trade secrets, proprietary technology, copyrights, patents, 00029 * trademarks, maskwork rights, or any other form of intellectual 00030 * property whatsoever. Maxim Integrated Products, Inc. retains all 00031 * ownership rights. 00032 **********************************************************************/ 00033 00034 00035 #include "max7219.h" 00036 00037 00038 //********************************************************************* 00039 Max7219::Max7219(SPI *spi_bus, PinName cs): _p_spi(spi_bus) 00040 { 00041 _num_devices = 1; 00042 00043 _p_cs = new DigitalOut(cs, 0); //change for salvat board 00044 _spi_owner = false; 00045 } 00046 00047 00048 //********************************************************************* 00049 Max7219::Max7219(PinName mosi, PinName miso, PinName sclk, PinName cs) 00050 { 00051 _num_devices = 1; 00052 00053 _p_spi = new SPI(mosi, miso, sclk); 00054 _p_cs = new DigitalOut(cs, 0);//change for salvat board 00055 00056 _spi_owner = true; 00057 } 00058 00059 00060 //********************************************************************* 00061 Max7219::~Max7219() 00062 { 00063 delete _p_cs; 00064 00065 if(_spi_owner) 00066 { 00067 delete _p_spi; 00068 } 00069 } 00070 00071 00072 //********************************************************************* 00073 int32_t Max7219::set_num_devices(uint8_t num_devices) 00074 { 00075 int32_t rtn_val = -1; 00076 00077 if(num_devices > 0) 00078 { 00079 _num_devices = num_devices; 00080 rtn_val = _num_devices; 00081 } 00082 00083 return(rtn_val); 00084 } 00085 00086 00087 //********************************************************************* 00088 void Max7219::set_display_test(void) 00089 { 00090 uint8_t idx = 0; 00091 00092 _p_cs->write(1); //change for salvat board 00093 for(idx = 0; idx < _num_devices; idx++) 00094 { 00095 _p_spi->write(MAX7219_DISPLAY_TEST); 00096 _p_spi->write(1); 00097 } 00098 _p_cs->write(0); //change for salvat board 00099 } 00100 00101 00102 //********************************************************************* 00103 void Max7219::clear_display_test(void) 00104 { 00105 uint8_t idx = 0; 00106 00107 _p_cs->write(1); //change for salvat board 00108 for(idx = 0; idx < _num_devices; idx++) 00109 { 00110 _p_spi->write(MAX7219_DISPLAY_TEST); 00111 _p_spi->write(0); 00112 } 00113 _p_cs->write(0); //change for salvat board 00114 } 00115 00116 00117 //********************************************************************* 00118 int32_t Max7219::init_device(max7219_configuration_t config) 00119 { 00120 int32_t rtn_val = -1; 00121 uint8_t idx = 0; 00122 00123 if(config.device_number > _num_devices) 00124 { 00125 rtn_val = -1; 00126 } 00127 else if(config.device_number == 0) 00128 { 00129 //device numbering starts with index 1 00130 rtn_val = -2; 00131 } 00132 else 00133 { 00134 //write DECODE_MODE register of device 00135 _p_cs->write(1); //change for salvat board 00136 for(idx = _num_devices; idx > 0; idx--) 00137 { 00138 if(config.device_number == idx) 00139 { 00140 _p_spi->write(MAX7219_DECODE_MODE); 00141 _p_spi->write(config.decode_mode); 00142 } 00143 else 00144 { 00145 _p_spi->write(MAX7219_NO_OP); 00146 _p_spi->write(0); 00147 } 00148 } 00149 _p_cs->write(0); //change for salvat board 00150 00151 wait_us(1); 00152 00153 //write INTENSITY register of device 00154 _p_cs->write(1); //change for salvat board 00155 for(idx = _num_devices; idx > 0; idx--) 00156 { 00157 if(config.device_number == idx) 00158 { 00159 _p_spi->write(MAX7219_INTENSITY); 00160 _p_spi->write(config.intensity); 00161 } 00162 else 00163 { 00164 _p_spi->write(MAX7219_NO_OP); 00165 _p_spi->write(0); 00166 } 00167 } 00168 _p_cs->write(0); //change for salvat board 00169 00170 wait_us(1); 00171 00172 //write SCAN_LIMT register of device 00173 _p_cs->write(1); //change for salvat board 00174 for(idx = _num_devices; idx > 0; idx--) 00175 { 00176 if(config.device_number == idx) 00177 { 00178 _p_spi->write(MAX7219_SCAN_LIMIT); 00179 _p_spi->write(config.scan_limit); 00180 } 00181 else 00182 { 00183 _p_spi->write(MAX7219_NO_OP); 00184 _p_spi->write(0); 00185 } 00186 } 00187 _p_cs->write(0); //change for salvat board 00188 00189 wait_us(1); 00190 00191 rtn_val = 0; 00192 } 00193 00194 return(rtn_val); 00195 } 00196 00197 00198 //********************************************************************* 00199 void Max7219::init_display(max7219_configuration_t config) 00200 { 00201 uint8_t idx = 0; 00202 00203 //write DECODE_MODE register of all devices 00204 _p_cs->write(1); //change for salvat board 00205 for(idx = 0; idx < _num_devices; idx++) 00206 { 00207 _p_spi->write(MAX7219_DECODE_MODE); 00208 _p_spi->write(config.decode_mode); 00209 } 00210 _p_cs->write(0); //change for salvat board 00211 00212 wait_us(1); 00213 00214 //write INTENSITY register of all devices 00215 _p_cs->write(1); //change for salvat board 00216 for(idx = 0; idx < _num_devices; idx++) 00217 { 00218 _p_spi->write(MAX7219_INTENSITY); 00219 _p_spi->write(config.intensity); 00220 } 00221 _p_cs->write(0); //change for salvat board 00222 00223 wait_us(1); 00224 00225 //write SCAN_LIMT register of all devices 00226 _p_cs->write(1); //change for salvat board 00227 for(idx = 0; idx < _num_devices; idx++) 00228 { 00229 _p_spi->write(MAX7219_SCAN_LIMIT); 00230 _p_spi->write(config.scan_limit); 00231 } 00232 _p_cs->write(0); //change for salvat board 00233 00234 wait_us(1); 00235 } 00236 00237 00238 //********************************************************************* 00239 int32_t Max7219::enable_device(uint8_t device_number) 00240 { 00241 int32_t rtn_val = -1; 00242 uint8_t idx = 0; 00243 00244 if(device_number > _num_devices) 00245 { 00246 rtn_val = -1; 00247 } 00248 else if(device_number == 0) 00249 { 00250 //device numbering starts with index 1 00251 rtn_val = -2; 00252 } 00253 else 00254 { 00255 _p_cs->write(1); //change for salvat board 00256 for(idx = _num_devices; idx > 0; idx--) 00257 { 00258 if(device_number == idx) 00259 { 00260 _p_spi->write(MAX7219_SHUTDOWN); 00261 _p_spi->write(1); 00262 } 00263 else 00264 { 00265 _p_spi->write(MAX7219_NO_OP); 00266 _p_spi->write(0); 00267 } 00268 } 00269 _p_cs->write(0); //change for salvat board 00270 00271 rtn_val = 0; 00272 } 00273 00274 return(rtn_val); 00275 } 00276 00277 00278 //********************************************************************* 00279 void Max7219::enable_display(void) 00280 { 00281 uint8_t idx = 0; 00282 00283 _p_cs->write(1); //change for salvat board 00284 for(idx = 0; idx < _num_devices; idx++) 00285 { 00286 _p_spi->write(MAX7219_SHUTDOWN); 00287 _p_spi->write(1); 00288 } 00289 _p_cs->write(0); //change for salvat board 00290 } 00291 00292 00293 //********************************************************************* 00294 int32_t Max7219::disable_device(uint8_t device_number) 00295 { 00296 int32_t rtn_val = -1; 00297 uint8_t idx = 0; 00298 00299 if(device_number > _num_devices) 00300 { 00301 rtn_val = -1; 00302 } 00303 else if(device_number == 0) 00304 { 00305 //device numbering starts with index 1 00306 rtn_val = -2; 00307 } 00308 else 00309 { 00310 _p_cs->write(1); //change for salvat board 00311 for(idx = _num_devices; idx > 0; idx--) 00312 { 00313 if(device_number == idx) 00314 { 00315 _p_spi->write(MAX7219_SHUTDOWN); 00316 _p_spi->write(0); 00317 } 00318 else 00319 { 00320 _p_spi->write(MAX7219_NO_OP); 00321 _p_spi->write(0); 00322 } 00323 } 00324 _p_cs->write(0); //change for salvat board 00325 00326 rtn_val = 0; 00327 } 00328 00329 return(rtn_val); 00330 } 00331 00332 00333 //********************************************************************* 00334 void Max7219::disable_display(void) 00335 { 00336 uint8_t idx = 0; 00337 00338 _p_cs->write(1); //change for salvat board 00339 for(idx = 0; idx < _num_devices; idx++) 00340 { 00341 _p_spi->write(MAX7219_SHUTDOWN); 00342 _p_spi->write(0); 00343 } 00344 _p_cs->write(0); //change for salvat board 00345 } 00346 00347 00348 00349 //********************************************************************* 00350 int32_t Max7219::write_digit(uint8_t device_number, uint8_t digit, uint8_t data) 00351 { 00352 int32_t rtn_val = -1; 00353 uint8_t idx = 0; 00354 00355 if(digit > MAX7219_DIGIT_7) 00356 { 00357 rtn_val = -3; 00358 } 00359 else if(digit < MAX7219_DIGIT_0) 00360 { 00361 rtn_val = -4; 00362 } 00363 else 00364 { 00365 if(device_number > _num_devices) 00366 { 00367 rtn_val = -1; 00368 } 00369 else if(device_number == 0) 00370 { 00371 rtn_val = -2; 00372 } 00373 else 00374 { 00375 _p_cs->write(1); //change for salvat board 00376 for(idx = _num_devices; idx > 0; idx--) 00377 { 00378 if(idx == device_number) 00379 { 00380 _p_spi->write(digit); 00381 _p_spi->write(data); 00382 } 00383 else 00384 { 00385 _p_spi->write(MAX7219_NO_OP); 00386 _p_spi->write(0); 00387 } 00388 } 00389 _p_cs->write(0); //change for salvat board 00390 00391 rtn_val = 0; 00392 } 00393 } 00394 00395 return(rtn_val); 00396 } 00397 00398 00399 //********************************************************************* 00400 int32_t Max7219::clear_digit(uint8_t device_number, uint8_t digit) 00401 { 00402 int32_t rtn_val = -1; 00403 uint8_t idx = 0; 00404 00405 if(digit > MAX7219_DIGIT_7) 00406 { 00407 rtn_val = -3; 00408 } 00409 else if(digit < MAX7219_DIGIT_0) 00410 { 00411 rtn_val = -4; 00412 } 00413 else 00414 { 00415 if(device_number > _num_devices) 00416 { 00417 rtn_val = -1; 00418 } 00419 else if(device_number == 0) 00420 { 00421 rtn_val = -2; 00422 } 00423 else 00424 { 00425 _p_cs->write(1); //change for salvat board 00426 for(idx = _num_devices; idx > 0; idx--) 00427 { 00428 if(idx == device_number) 00429 { 00430 _p_spi->write(digit); 00431 _p_spi->write(0); 00432 } 00433 else 00434 { 00435 _p_spi->write(MAX7219_NO_OP); 00436 _p_spi->write(0); 00437 } 00438 } 00439 _p_cs->write(0); //change for salvat board 00440 00441 rtn_val = 0; 00442 } 00443 } 00444 00445 return(rtn_val); 00446 } 00447 00448 00449 //********************************************************************* 00450 int32_t Max7219::device_all_on(uint8_t device_number) 00451 { 00452 int32_t rtn_val = -1; 00453 uint8_t idx = 0; 00454 00455 if(device_number > _num_devices) 00456 { 00457 rtn_val = -1; 00458 } 00459 else if(device_number == 0) 00460 { 00461 rtn_val = -2; 00462 } 00463 else 00464 { 00465 rtn_val = 0; 00466 00467 //writes every digit of given device to 0xFF 00468 for(idx = 0; idx < 8; idx++) 00469 { 00470 if(rtn_val == 0) 00471 { 00472 rtn_val = write_digit(device_number, (idx + 1), 0xFF); 00473 } 00474 } 00475 } 00476 00477 return(rtn_val); 00478 } 00479 00480 00481 //********************************************************************* 00482 int32_t Max7219::device_all_off(uint8_t device_number) 00483 { 00484 int32_t rtn_val = -1; 00485 uint8_t idx = 0; 00486 00487 if(device_number > _num_devices) 00488 { 00489 rtn_val = -1; 00490 } 00491 else if(device_number == 0) 00492 { 00493 rtn_val = -2; 00494 } 00495 else 00496 { 00497 rtn_val = 0; 00498 00499 //writes every digit of given device to 0 00500 for(idx = 0; idx < 8; idx++) 00501 { 00502 if(rtn_val == 0) 00503 { 00504 rtn_val = write_digit(device_number, (idx + 1), 0); 00505 } 00506 } 00507 } 00508 00509 return(rtn_val); 00510 } 00511 00512 00513 void Max7219::display_all_on(void) 00514 { 00515 uint8_t idx, idy; 00516 00517 //writes every digit of every device to 0xFF 00518 for(idx = 0; idx < _num_devices; idx++) 00519 { 00520 for(idy = 0; idy < MAX7219_DIGIT_7; idy++) 00521 { 00522 write_digit((idx + 1), (idy + 1), 0xFF); 00523 } 00524 } 00525 } 00526 00527 00528 void Max7219::display_all_off(void) 00529 { 00530 uint8_t idx, idy; 00531 00532 //writes every digit of every device to 0 00533 for(idx = 0; idx < _num_devices; idx++) 00534 { 00535 for(idy = 0; idy < MAX7219_DIGIT_7; idy++) 00536 { 00537 write_digit((idx + 1), (idy + 1), 0); 00538 } 00539 } 00540 } 00541
Generated on Fri Jul 15 2022 09:24:46 by
1.7.2