lorenzo cervelli / Mbed 2 deprecated max7219ledarray

Dependencies:   mbed

Fork of max7219ledarray by Andrea Corrado

Committer:
andcor02
Date:
Tue Jan 20 11:37:10 2015 +0000
Revision:
7:1f26417a6f51
Parent:
4:81cea7a352b0
Child:
8:880fd42755ca
max7219 8x8 led matrix control;

Who changed what in which revision?

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