A generic library to support Max7221 chip for 8 digit 7 segment displays

Dependencies:   mbed

Dependents:   Example_Max7221

MAXIM SPI interfaced, 8 digit LED display Driver

MAX221

  • The image below was my initial development setup. The top of the image shows a wire wrap connection to a Max7221 to an 8 digit 7 segment display.
  • The small board below it is the new max7221 board that was created with surface mount chips and using a toaster oven to do reflow soldering. /media/uploads/jakowisp/img_0012.jpg

Example of using a single Max7221 on SPI p5,p6,p7

Import program

00001  #include "mbed.h"
00002  #include "Max7221.h"
00003  
00004 
00005 // p5: DIN, p7: CLK, p8: LOAD/CS
00006 Max7221 max7221disp1(p5, p7, p8);
00007 //Max7221 max7221disp2(p5, p7, p8);
00008 //Max7221 max7221disp3(p11, p13, p14);
00009 //Max7221 max7221disp4(p11, p13, p14);
00010 
00011 int count=-99;
00012 
00013 void loop(void) {
00014    max7221disp1=count;
00015    if (count < 100)
00016       count=count+1;
00017    else 
00018       count=-99; 
00019 }
00020 
00021 int main() {  
00022     max7221disp1.Setup();
00023     //Max7221::SetupALl();
00024     max7221disp1.WriteFloat(123.125);
00025     wait(1.0);  
00026 
00027     while (1) {
00028         loop();
00029         wait(1.0);
00030     }
00031 }
00032 
00033 
Committer:
jakowisp
Date:
Wed Aug 07 03:07:01 2013 +0000
Revision:
4:e2b160410338
Parent:
3:5abc4047af8d
Correct the Doxygen generation for the class.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jakowisp 2:828c62cc1861 1 /**
jakowisp 2:828c62cc1861 2 * @file Max7221.h
jakowisp 2:828c62cc1861 3 * @brief This file contains the class defnition of Max7221 and define statements related to the class, and max7221 device
jakowisp 2:828c62cc1861 4 * The methods in this class are derived from posts on the mbed forum written by Igor Skochinsky on October 2009
jakowisp 2:828c62cc1861 5 *
jakowisp 2:828c62cc1861 6 * @author Dwayne S. Dilbeck
jakowisp 2:828c62cc1861 7 *
jakowisp 2:828c62cc1861 8 * @date 8/6/2013
jakowisp 2:828c62cc1861 9 */
jakowisp 1:d8589d1f368c 10 #ifndef Max7221_H
jakowisp 1:d8589d1f368c 11 #define Max7221_H
jakowisp 1:d8589d1f368c 12
jakowisp 4:e2b160410338 13
jakowisp 4:e2b160410338 14 // define max7219/max7221 registers
jakowisp 4:e2b160410338 15 #define max7219_reg_noop 0x00
jakowisp 4:e2b160410338 16 #define max7219_reg_digit0 0x01
jakowisp 4:e2b160410338 17 #define max7219_reg_digit1 0x02
jakowisp 4:e2b160410338 18 #define max7219_reg_digit2 0x03
jakowisp 4:e2b160410338 19 #define max7219_reg_digit3 0x04
jakowisp 4:e2b160410338 20 #define max7219_reg_digit4 0x05
jakowisp 4:e2b160410338 21 #define max7219_reg_digit5 0x06
jakowisp 4:e2b160410338 22 #define max7219_reg_digit6 0x07
jakowisp 4:e2b160410338 23 #define max7219_reg_digit7 0x08
jakowisp 4:e2b160410338 24 #define max7219_reg_decodeMode 0x09
jakowisp 4:e2b160410338 25 #define max7219_reg_intensity 0x0a
jakowisp 4:e2b160410338 26 #define max7219_reg_scanLimit 0x0b
jakowisp 4:e2b160410338 27 #define max7219_reg_shutdown 0x0c
jakowisp 4:e2b160410338 28 #define max7219_reg_displayTest 0x0f
jakowisp 4:e2b160410338 29
jakowisp 4:e2b160410338 30 #define LOW 0
jakowisp 4:e2b160410338 31 #define HIGH 1
jakowisp 4:e2b160410338 32 #define MHZ 1000000
jakowisp 4:e2b160410338 33 #define NUMDIGITS 8
jakowisp 4:e2b160410338 34
jakowisp 4:e2b160410338 35 #ifdef NUMDIGITS
jakowisp 4:e2b160410338 36 #define UPPERBOUND 99999999
jakowisp 4:e2b160410338 37 #define LOWERBOUND -9999999
jakowisp 4:e2b160410338 38 #endif
jakowisp 4:e2b160410338 39
jakowisp 4:e2b160410338 40 /**
jakowisp 4:e2b160410338 41 * Max7221 Example
jakowisp 2:828c62cc1861 42 * @code
jakowisp 2:828c62cc1861 43 * #include "mbed.h"
jakowisp 2:828c62cc1861 44 * #include "Max7221.h"
jakowisp 2:828c62cc1861 45 *
jakowisp 2:828c62cc1861 46 *
jakowisp 2:828c62cc1861 47 * // p5: DIN, p7: CLK, p8: LOAD/CS
jakowisp 2:828c62cc1861 48 * Max7221 max7221disp1(p5, p7, p8);
jakowisp 2:828c62cc1861 49 * //Max7221 max7221disp2(p5, p7, p8);
jakowisp 2:828c62cc1861 50 * //Max7221 max7221disp3(p11, p13, p14);
jakowisp 2:828c62cc1861 51 * //Max7221 max7221disp4(p11, p13, p14);
jakowisp 2:828c62cc1861 52 *
jakowisp 2:828c62cc1861 53 * int count=-99;
jakowisp 2:828c62cc1861 54 *
jakowisp 2:828c62cc1861 55 * void loop(void) {
jakowisp 2:828c62cc1861 56 * max7221disp1.WriteInt(count);
jakowisp 2:828c62cc1861 57 * if (count < 100)
jakowisp 2:828c62cc1861 58 * count=count+1;
jakowisp 2:828c62cc1861 59 * else
jakowisp 2:828c62cc1861 60 * count=-99;
jakowisp 2:828c62cc1861 61 * }
jakowisp 2:828c62cc1861 62 *
jakowisp 2:828c62cc1861 63 * int main() {
jakowisp 2:828c62cc1861 64 * max7221disp1.Setup();
jakowisp 2:828c62cc1861 65 * //Max7221::SetupALl();
jakowisp 2:828c62cc1861 66 * max7221disp1.WriteFloat(123.125);
jakowisp 2:828c62cc1861 67 * wait(1.0);
jakowisp 2:828c62cc1861 68 *
jakowisp 2:828c62cc1861 69 * while (1) {
jakowisp 2:828c62cc1861 70 * loop();
jakowisp 2:828c62cc1861 71 * wait(1.0);
jakowisp 2:828c62cc1861 72 * }
jakowisp 2:828c62cc1861 73 * }
jakowisp 2:828c62cc1861 74 * @endcode
jakowisp 2:828c62cc1861 75 */
jakowisp 1:d8589d1f368c 76 class Max7221 {
jakowisp 1:d8589d1f368c 77 public:
jakowisp 4:e2b160410338 78 /**
jakowisp 4:e2b160410338 79 * Constructor. This is the default constructor
jakowisp 4:e2b160410338 80 * @author Dwayne S. Dilbeck
jakowisp 4:e2b160410338 81 * @param msoi The SPI pin used as input to the device. Valid values for LPC1768 are p5 or p11
jakowisp 4:e2b160410338 82 * @param mclk The SPI pin used as clock for the device. Valid values for LPC1768 are p7 or p13
jakowisp 4:e2b160410338 83 * @param load The pin used to control load for the device. Any pin capable for DigitalOut can be used, but the same load pin must be used for
jakowisp 4:e2b160410338 84 * device that share the same msoi and mclk pins
jakowisp 4:e2b160410338 85 * @date 8/6/2013
jakowisp 4:e2b160410338 86 */
jakowisp 4:e2b160410338 87 Max7221(PinName msoi=p5, PinName mclk=p7, PinName load=p8);
jakowisp 1:d8589d1f368c 88
jakowisp 4:e2b160410338 89 /**
jakowisp 4:e2b160410338 90 * This method is used to write a byte of data to a specified register for only the device defined in this class instance
jakowisp 4:e2b160410338 91 * @author Dwayne S. Dilbeck
jakowisp 4:e2b160410338 92 * @param reg The register to write to.
jakowisp 4:e2b160410338 93 * @param data The value to be written.
jakowisp 4:e2b160410338 94 * @date 8/6/2013
jakowisp 4:e2b160410338 95 */
jakowisp 4:e2b160410338 96 void Write( unsigned int reg, unsigned int data);
jakowisp 2:828c62cc1861 97
jakowisp 4:e2b160410338 98 /**
jakowisp 4:e2b160410338 99 * This method is used to display an integer to the specified device instance. Underflow and overflow result in '-' written to all digits
jakowisp 4:e2b160410338 100 * @author Dwayne S. Dilbeck
jakowisp 4:e2b160410338 101 * @param value An integer value to display
jakowisp 4:e2b160410338 102 * @date 8/6/2013
jakowisp 4:e2b160410338 103 */
jakowisp 4:e2b160410338 104 void WriteInt( int value );
jakowisp 2:828c62cc1861 105
jakowisp 4:e2b160410338 106 /**
jakowisp 4:e2b160410338 107 * This method is used to display a floating point number to the specified device instance.
jakowisp 4:e2b160410338 108 * Underflow and overflow result in '-' written to all digits. The digits after the decimal
jakowisp 4:e2b160410338 109 * point are truncated to fit the display.
jakowisp 4:e2b160410338 110 * @author Dwayne S. Dilbeck
jakowisp 4:e2b160410338 111 * @param value A float value to display
jakowisp 4:e2b160410338 112 * @date 8/6/2013
jakowisp 4:e2b160410338 113 */
jakowisp 4:e2b160410338 114 void WriteFloat( float value);
jakowisp 1:d8589d1f368c 115
jakowisp 4:e2b160410338 116 /**
jakowisp 4:e2b160410338 117 * Overload of the EQUALS operator to provide easy use of the class.
jakowisp 4:e2b160410338 118 * @author Dwayne S. Dilbeck
jakowisp 4:e2b160410338 119 * @param value An integer value to display
jakowisp 4:e2b160410338 120 * @date 8/6/2013
jakowisp 4:e2b160410338 121 */
jakowisp 4:e2b160410338 122 Max7221& operator= (int value){
jakowisp 1:d8589d1f368c 123 WriteInt(value);
jakowisp 1:d8589d1f368c 124 return *this;
jakowisp 1:d8589d1f368c 125 };
jakowisp 4:e2b160410338 126 /**
jakowisp 4:e2b160410338 127 * Overload of the EQUALS operator to provide easy use of the class.
jakowisp 4:e2b160410338 128 * @author Dwayne S. Dilbeck
jakowisp 4:e2b160410338 129 * @param value A float value to display
jakowisp 4:e2b160410338 130 * @date 8/6/2013
jakowisp 4:e2b160410338 131 */
jakowisp 4:e2b160410338 132 Max7221& operator= (float value){
jakowisp 1:d8589d1f368c 133 WriteFloat(value);
jakowisp 1:d8589d1f368c 134 return *this;
jakowisp 1:d8589d1f368c 135 };
jakowisp 4:e2b160410338 136
jakowisp 4:e2b160410338 137 /**
jakowisp 4:e2b160410338 138 * This method is used to write an intial set off values to the device to prepare it for use.
jakowisp 4:e2b160410338 139 * @author Dwayne S. Dilbeck
jakowisp 4:e2b160410338 140 * @date 8/6/2013
jakowisp 4:e2b160410338 141 */
jakowisp 4:e2b160410338 142 void Setup (void);
jakowisp 2:828c62cc1861 143
jakowisp 4:e2b160410338 144 /**
jakowisp 4:e2b160410338 145 * This method is used to write a byte of data to a specified register for all the devices instantiated.
jakowisp 4:e2b160410338 146 * @author Dwayne S. Dilbeck
jakowisp 4:e2b160410338 147 * @param reg The register to write to.
jakowisp 4:e2b160410338 148 * @param data The value to be written.
jakowisp 4:e2b160410338 149 * @date 8/6/2013
jakowisp 4:e2b160410338 150 */
jakowisp 4:e2b160410338 151 static void WriteAll (unsigned int reg, unsigned int data);
jakowisp 2:828c62cc1861 152
jakowisp 4:e2b160410338 153 /**
jakowisp 4:e2b160410338 154 * This method is used to write an intial set off values to ALL device to prepare them for use.
jakowisp 4:e2b160410338 155 * @author Dwayne S. Dilbeck
jakowisp 4:e2b160410338 156 * @date 8/6/2013
jakowisp 4:e2b160410338 157 */
jakowisp 4:e2b160410338 158 static void SetupAll (void);
jakowisp 1:d8589d1f368c 159
jakowisp 1:d8589d1f368c 160 private:
jakowisp 2:828c62cc1861 161 /// Pointer for the class instance to a particular SPI bus
jakowisp 1:d8589d1f368c 162 SPI *max72_spi;
jakowisp 2:828c62cc1861 163 /// pointer for the class instance of a particular load signal
jakowisp 1:d8589d1f368c 164 DigitalOut *load;
jakowisp 2:828c62cc1861 165 /// id of the class instance
jakowisp 1:d8589d1f368c 166 int id;
jakowisp 2:828c62cc1861 167 /// pointer to the number of devices connected to the SPI bus instance this device instance is connected to.
jakowisp 1:d8589d1f368c 168 int *maxInUse;
jakowisp 1:d8589d1f368c 169
jakowisp 2:828c62cc1861 170 ///For the class we have a static set of values. There are 2 SPI buses, 2 load signals, and 2 counters of the number of devices connected to a SPI bus.
jakowisp 1:d8589d1f368c 171 static SPI *spi1;
jakowisp 1:d8589d1f368c 172 static SPI *spi2;
jakowisp 1:d8589d1f368c 173 static DigitalOut *load1;
jakowisp 1:d8589d1f368c 174 static DigitalOut *load2;
jakowisp 1:d8589d1f368c 175 static int maxInUseSPI1;
jakowisp 1:d8589d1f368c 176 static int maxInUseSPI2;
jakowisp 1:d8589d1f368c 177 };
jakowisp 1:d8589d1f368c 178
jakowisp 1:d8589d1f368c 179 #endif