Grzegorz Kaczmarek / Mbed 2 deprecated Max7221

Dependencies:   mbed

Fork of Max7221 by Dwayne Dilbeck

Committer:
jakowisp
Date:
Mon Aug 05 18:08:30 2013 +0000
Revision:
0:cb8e1d05a4a7
Child:
1:d8589d1f368c
Fully functional target board.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jakowisp 0:cb8e1d05a4a7 1 #include "mbed.h"
jakowisp 0:cb8e1d05a4a7 2
jakowisp 0:cb8e1d05a4a7 3 // p5: DIN, p7: CLK, p8: LOAD/CS
jakowisp 0:cb8e1d05a4a7 4 SPI max72_spi(p5, NC, p7);
jakowisp 0:cb8e1d05a4a7 5 DigitalOut load(p8);
jakowisp 0:cb8e1d05a4a7 6
jakowisp 0:cb8e1d05a4a7 7 int maxInUse = 1; //change this variable to set how many MAX7219's you'll use
jakowisp 0:cb8e1d05a4a7 8
jakowisp 0:cb8e1d05a4a7 9 // define max7219/max7221 registers
jakowisp 0:cb8e1d05a4a7 10 #define max7219_reg_noop 0x00
jakowisp 0:cb8e1d05a4a7 11 #define max7219_reg_digit0 0x01
jakowisp 0:cb8e1d05a4a7 12 #define max7219_reg_digit1 0x02
jakowisp 0:cb8e1d05a4a7 13 #define max7219_reg_digit2 0x03
jakowisp 0:cb8e1d05a4a7 14 #define max7219_reg_digit3 0x04
jakowisp 0:cb8e1d05a4a7 15 #define max7219_reg_digit4 0x05
jakowisp 0:cb8e1d05a4a7 16 #define max7219_reg_digit5 0x06
jakowisp 0:cb8e1d05a4a7 17 #define max7219_reg_digit6 0x07
jakowisp 0:cb8e1d05a4a7 18 #define max7219_reg_digit7 0x08
jakowisp 0:cb8e1d05a4a7 19 #define max7219_reg_decodeMode 0x09
jakowisp 0:cb8e1d05a4a7 20 #define max7219_reg_intensity 0x0a
jakowisp 0:cb8e1d05a4a7 21 #define max7219_reg_scanLimit 0x0b
jakowisp 0:cb8e1d05a4a7 22 #define max7219_reg_shutdown 0x0c
jakowisp 0:cb8e1d05a4a7 23 #define max7219_reg_displayTest 0x0f
jakowisp 0:cb8e1d05a4a7 24
jakowisp 0:cb8e1d05a4a7 25 #define LOW 0
jakowisp 0:cb8e1d05a4a7 26 #define HIGH 1
jakowisp 0:cb8e1d05a4a7 27 #define MHZ 1000000
jakowisp 0:cb8e1d05a4a7 28 unsigned int count;
jakowisp 0:cb8e1d05a4a7 29
jakowisp 0:cb8e1d05a4a7 30 void maxSingle( unsigned int reg, unsigned int col) {
jakowisp 0:cb8e1d05a4a7 31 //maxSingle is the "easy" function to use for a
jakowisp 0:cb8e1d05a4a7 32 //single max7219
jakowisp 0:cb8e1d05a4a7 33
jakowisp 0:cb8e1d05a4a7 34 load = LOW; // begin
jakowisp 0:cb8e1d05a4a7 35
jakowisp 0:cb8e1d05a4a7 36 #ifdef SPI16
jakowisp 0:cb8e1d05a4a7 37 unsigned int temp;
jakowisp 0:cb8e1d05a4a7 38 temp=(reg<<8)||col;
jakowisp 0:cb8e1d05a4a7 39 max72_spi.format(16, 0);
jakowisp 0:cb8e1d05a4a7 40 max72_spi.write(temp);
jakowisp 0:cb8e1d05a4a7 41 max72_spi.format(8, 0);
jakowisp 0:cb8e1d05a4a7 42 #else
jakowisp 0:cb8e1d05a4a7 43 max72_spi.write(reg);
jakowisp 0:cb8e1d05a4a7 44 max72_spi.write(col); // put data
jakowisp 0:cb8e1d05a4a7 45 #endif
jakowisp 0:cb8e1d05a4a7 46 load = HIGH; // make sure data is loaded (on rising edge of LOAD/CS)
jakowisp 0:cb8e1d05a4a7 47 }
jakowisp 0:cb8e1d05a4a7 48
jakowisp 0:cb8e1d05a4a7 49 void maxAll (unsigned int reg, unsigned int col) { // initialize all MAX7219's in the system
jakowisp 0:cb8e1d05a4a7 50 load = LOW; // begin
jakowisp 0:cb8e1d05a4a7 51 for ( int c=1; c<= maxInUse; c++) {
jakowisp 0:cb8e1d05a4a7 52 max72_spi.write(reg); // specify register
jakowisp 0:cb8e1d05a4a7 53 max72_spi.write(col); // put data
jakowisp 0:cb8e1d05a4a7 54 }
jakowisp 0:cb8e1d05a4a7 55 load = HIGH;
jakowisp 0:cb8e1d05a4a7 56 }
jakowisp 0:cb8e1d05a4a7 57
jakowisp 0:cb8e1d05a4a7 58 void maxOne(unsigned int maxNr, unsigned int reg, unsigned int col) {
jakowisp 0:cb8e1d05a4a7 59 //maxOne is for adressing different MAX7219's,
jakowisp 0:cb8e1d05a4a7 60 //while having a couple of them cascaded
jakowisp 0:cb8e1d05a4a7 61 int c = 0;
jakowisp 0:cb8e1d05a4a7 62 load = LOW;
jakowisp 0:cb8e1d05a4a7 63
jakowisp 0:cb8e1d05a4a7 64 for ( c = maxInUse; c > maxNr; c--) {
jakowisp 0:cb8e1d05a4a7 65 max72_spi.write(0); // no-op
jakowisp 0:cb8e1d05a4a7 66 max72_spi.write(0); // no-op
jakowisp 0:cb8e1d05a4a7 67 }
jakowisp 0:cb8e1d05a4a7 68
jakowisp 0:cb8e1d05a4a7 69 max72_spi.write(reg); // specify register
jakowisp 0:cb8e1d05a4a7 70 max72_spi.write(col); // put data
jakowisp 0:cb8e1d05a4a7 71
jakowisp 0:cb8e1d05a4a7 72 for ( c=maxNr-1; c >= 1; c--) {
jakowisp 0:cb8e1d05a4a7 73 max72_spi.write(0); // no-op
jakowisp 0:cb8e1d05a4a7 74 max72_spi.write(0); // no-op
jakowisp 0:cb8e1d05a4a7 75 }
jakowisp 0:cb8e1d05a4a7 76 load = HIGH;
jakowisp 0:cb8e1d05a4a7 77 }
jakowisp 0:cb8e1d05a4a7 78
jakowisp 0:cb8e1d05a4a7 79
jakowisp 0:cb8e1d05a4a7 80 void setup () {
jakowisp 0:cb8e1d05a4a7 81 // initiation of the max 7219
jakowisp 0:cb8e1d05a4a7 82 // SPI setup: 8 bits, mode 0
jakowisp 0:cb8e1d05a4a7 83 max72_spi.format(8, 0);
jakowisp 0:cb8e1d05a4a7 84
jakowisp 0:cb8e1d05a4a7 85 // going by the datasheet, min clk is 100ns so theoretically 10MHz should work...
jakowisp 0:cb8e1d05a4a7 86 max72_spi.frequency(10*MHZ);
jakowisp 0:cb8e1d05a4a7 87
jakowisp 0:cb8e1d05a4a7 88 maxAll(max7219_reg_scanLimit, 0x07);
jakowisp 0:cb8e1d05a4a7 89 maxAll(max7219_reg_decodeMode, 0xff); // using an led matrix (not digits)
jakowisp 0:cb8e1d05a4a7 90 maxAll(max7219_reg_shutdown, 0x01); // not in shutdown mode
jakowisp 0:cb8e1d05a4a7 91 maxAll(max7219_reg_displayTest, 0x00); // no display test
jakowisp 0:cb8e1d05a4a7 92 for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off
jakowisp 0:cb8e1d05a4a7 93 maxAll(e,0xf);
jakowisp 0:cb8e1d05a4a7 94 }
jakowisp 0:cb8e1d05a4a7 95 maxAll(max7219_reg_intensity, 0x01 & 0x0f); // the first 0x0f is the value you can set
jakowisp 0:cb8e1d05a4a7 96 // range: 0x00 to 0x0f
jakowisp 0:cb8e1d05a4a7 97 }
jakowisp 0:cb8e1d05a4a7 98
jakowisp 0:cb8e1d05a4a7 99 void loop () {
jakowisp 0:cb8e1d05a4a7 100
jakowisp 0:cb8e1d05a4a7 101
jakowisp 0:cb8e1d05a4a7 102 //if you use just one MAX7219 it should look like this
jakowisp 0:cb8e1d05a4a7 103
jakowisp 0:cb8e1d05a4a7 104 for (int i=1;i<=8;i++) {
jakowisp 0:cb8e1d05a4a7 105 if (i<=count && i !=0)
jakowisp 0:cb8e1d05a4a7 106 maxSingle(i,i);
jakowisp 0:cb8e1d05a4a7 107 else
jakowisp 0:cb8e1d05a4a7 108 maxSingle(i,0xf);
jakowisp 0:cb8e1d05a4a7 109 }
jakowisp 0:cb8e1d05a4a7 110
jakowisp 0:cb8e1d05a4a7 111 if (count <8)
jakowisp 0:cb8e1d05a4a7 112 count=count++;
jakowisp 0:cb8e1d05a4a7 113 else
jakowisp 0:cb8e1d05a4a7 114 count=0;
jakowisp 0:cb8e1d05a4a7 115
jakowisp 0:cb8e1d05a4a7 116 /*
jakowisp 0:cb8e1d05a4a7 117 maxSingle(1,255); // + - - - - - - -
jakowisp 0:cb8e1d05a4a7 118 maxSingle(2,255); // - + - - - - - -
jakowisp 0:cb8e1d05a4a7 119 maxSingle(3,255); // - - + - - - - -
jakowisp 0:cb8e1d05a4a7 120 maxSingle(4,255); // - - - + - - - -
jakowisp 0:cb8e1d05a4a7 121 maxSingle(5,255); // - - - - + - - -
jakowisp 0:cb8e1d05a4a7 122 maxSingle(6,255); // - - - - - + - -
jakowisp 0:cb8e1d05a4a7 123 maxSingle(7,0); // - - - - - - + -
jakowisp 0:cb8e1d05a4a7 124 maxSingle(8,0); // - - - - - - - +
jakowisp 0:cb8e1d05a4a7 125 */
jakowisp 0:cb8e1d05a4a7 126 //if you use more than one MAX7219, it should look like this
jakowisp 0:cb8e1d05a4a7 127
jakowisp 0:cb8e1d05a4a7 128 /*
jakowisp 0:cb8e1d05a4a7 129 maxAll(1,1); // + - - - - - - -
jakowisp 0:cb8e1d05a4a7 130 maxAll(2,3); // + + - - - - - -
jakowisp 0:cb8e1d05a4a7 131 maxAll(3,7); // + + + - - - - -
jakowisp 0:cb8e1d05a4a7 132 maxAll(4,15); // + + + + - - - -
jakowisp 0:cb8e1d05a4a7 133 maxAll(5,31); // + + + + + - - -
jakowisp 0:cb8e1d05a4a7 134 maxAll(6,63); // + + + + + + - -
jakowisp 0:cb8e1d05a4a7 135 maxAll(7,127); // + + + + + + + -
jakowisp 0:cb8e1d05a4a7 136 maxAll(8,255); // + + + + + + + +
jakowisp 0:cb8e1d05a4a7 137 */
jakowisp 0:cb8e1d05a4a7 138
jakowisp 0:cb8e1d05a4a7 139 //
jakowisp 0:cb8e1d05a4a7 140
jakowisp 0:cb8e1d05a4a7 141 //if you use more then one max7219 the second one should look like this
jakowisp 0:cb8e1d05a4a7 142
jakowisp 0:cb8e1d05a4a7 143
jakowisp 0:cb8e1d05a4a7 144 /*
jakowisp 0:cb8e1d05a4a7 145 maxOne(2,1,1); // + - - - - - - -
jakowisp 0:cb8e1d05a4a7 146 maxOne(2,2,2); // - + - - - - - -
jakowisp 0:cb8e1d05a4a7 147 maxOne(2,3,4); // - - + - - - - -
jakowisp 0:cb8e1d05a4a7 148 maxOne(2,4,8); // - - - + - - - -
jakowisp 0:cb8e1d05a4a7 149 maxOne(2,5,16); // - - - - + - - -
jakowisp 0:cb8e1d05a4a7 150 maxOne(2,6,32); // - - - - - + - -
jakowisp 0:cb8e1d05a4a7 151 maxOne(2,7,64); // - - - - - - + -
jakowisp 0:cb8e1d05a4a7 152 maxOne(2,8,128); // - - - - - - - +
jakowisp 0:cb8e1d05a4a7 153
jakowisp 0:cb8e1d05a4a7 154 */
jakowisp 0:cb8e1d05a4a7 155
jakowisp 0:cb8e1d05a4a7 156 //
jakowisp 0:cb8e1d05a4a7 157
jakowisp 0:cb8e1d05a4a7 158
jakowisp 0:cb8e1d05a4a7 159 }
jakowisp 0:cb8e1d05a4a7 160
jakowisp 0:cb8e1d05a4a7 161 int main() {
jakowisp 0:cb8e1d05a4a7 162 count=1;
jakowisp 0:cb8e1d05a4a7 163 setup ();
jakowisp 0:cb8e1d05a4a7 164 while (1) {
jakowisp 0:cb8e1d05a4a7 165 loop ();
jakowisp 0:cb8e1d05a4a7 166 wait_ms(1000);
jakowisp 0:cb8e1d05a4a7 167 }
jakowisp 0:cb8e1d05a4a7 168
jakowisp 0:cb8e1d05a4a7 169 return 0;
jakowisp 0:cb8e1d05a4a7 170 }