1

Dependencies:   mbed

Committer:
yu10078999
Date:
Thu Sep 08 08:01:57 2016 +0000
Revision:
1:545a966efb7b
Parent:
0:c831ddf9569f
2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yu10078999 0:c831ddf9569f 1 #include "mbed.h"
yu10078999 0:c831ddf9569f 2
yu10078999 0:c831ddf9569f 3 // PC_3: DIN, PB_10: CLK, D2: LOAD/CS
yu10078999 0:c831ddf9569f 4 SPI max72_spi(PC_3, NC, PB_10);
yu10078999 0:c831ddf9569f 5 DigitalOut load(D2);
yu10078999 0:c831ddf9569f 6
yu10078999 0:c831ddf9569f 7 int maxInUse = 1; //change this variable to set how many MAX7219's you'll use
yu10078999 0:c831ddf9569f 8
yu10078999 0:c831ddf9569f 9 // define max7219 registers
yu10078999 0:c831ddf9569f 10 #define max7219_reg_noop 0x00
yu10078999 0:c831ddf9569f 11 #define max7219_reg_digit0 0x01
yu10078999 0:c831ddf9569f 12 #define max7219_reg_digit1 0x02
yu10078999 0:c831ddf9569f 13 #define max7219_reg_digit2 0x03
yu10078999 0:c831ddf9569f 14 #define max7219_reg_digit3 0x04
yu10078999 0:c831ddf9569f 15 #define max7219_reg_digit4 0x05
yu10078999 0:c831ddf9569f 16 #define max7219_reg_digit5 0x06
yu10078999 0:c831ddf9569f 17 #define max7219_reg_digit6 0x07
yu10078999 0:c831ddf9569f 18 #define max7219_reg_digit7 0x08
yu10078999 0:c831ddf9569f 19 #define max7219_reg_decodeMode 0x09
yu10078999 0:c831ddf9569f 20 #define max7219_reg_intensity 0x0a
yu10078999 0:c831ddf9569f 21 #define max7219_reg_scanLimit 0x0b
yu10078999 0:c831ddf9569f 22 #define max7219_reg_shutdown 0x0c
yu10078999 0:c831ddf9569f 23 #define max7219_reg_displayTest 0x0f
yu10078999 0:c831ddf9569f 24
yu10078999 0:c831ddf9569f 25 #define LOW 0
yu10078999 0:c831ddf9569f 26 #define HIGH 1
yu10078999 0:c831ddf9569f 27 #define MHZ 1000000
yu10078999 0:c831ddf9569f 28
yu10078999 0:c831ddf9569f 29 void maxSingle( int reg, int col) {
yu10078999 0:c831ddf9569f 30 //maxSingle is the "easy" function to use for a
yu10078999 0:c831ddf9569f 31 //single max7219
yu10078999 0:c831ddf9569f 32 load = LOW; // begin
yu10078999 0:c831ddf9569f 33 max72_spi.write(reg); // specify register
yu10078999 0:c831ddf9569f 34 max72_spi.write(col); // put data
yu10078999 0:c831ddf9569f 35 load = HIGH; // make sure data is loaded (on rising edge of LOAD/CS)
yu10078999 0:c831ddf9569f 36 }
yu10078999 0:c831ddf9569f 37
yu10078999 0:c831ddf9569f 38 void maxAll (int reg, int col) { // initialize all MAX7219's in the system
yu10078999 0:c831ddf9569f 39 load = LOW; // begin
yu10078999 0:c831ddf9569f 40 for ( int c=1; c<= maxInUse; c++) {
yu10078999 0:c831ddf9569f 41 max72_spi.write(reg); // specify register
yu10078999 0:c831ddf9569f 42 max72_spi.write(col); // put data
yu10078999 0:c831ddf9569f 43 }
yu10078999 0:c831ddf9569f 44 load = HIGH;
yu10078999 0:c831ddf9569f 45 }
yu10078999 0:c831ddf9569f 46
yu10078999 0:c831ddf9569f 47 void maxOne(int maxNr, int reg, int col) {
yu10078999 0:c831ddf9569f 48 //maxOne is for adressing different MAX7219's,
yu10078999 0:c831ddf9569f 49 //while having a couple of them cascaded
yu10078999 0:c831ddf9569f 50 int c = 0;
yu10078999 0:c831ddf9569f 51 load = LOW;
yu10078999 0:c831ddf9569f 52
yu10078999 0:c831ddf9569f 53 for ( c = maxInUse; c > maxNr; c--) {
yu10078999 0:c831ddf9569f 54 max72_spi.write(0); // no-op
yu10078999 0:c831ddf9569f 55 max72_spi.write(0); // no-op
yu10078999 0:c831ddf9569f 56 }
yu10078999 0:c831ddf9569f 57
yu10078999 0:c831ddf9569f 58 max72_spi.write(reg); // specify register
yu10078999 0:c831ddf9569f 59 max72_spi.write(col); // put data
yu10078999 0:c831ddf9569f 60
yu10078999 0:c831ddf9569f 61 for ( c=maxNr-1; c >= 1; c--) {
yu10078999 0:c831ddf9569f 62 max72_spi.write(0); // no-op
yu10078999 0:c831ddf9569f 63 max72_spi.write(0); // no-op
yu10078999 0:c831ddf9569f 64 }
yu10078999 0:c831ddf9569f 65 load = HIGH;
yu10078999 0:c831ddf9569f 66 }
yu10078999 0:c831ddf9569f 67
yu10078999 0:c831ddf9569f 68
yu10078999 0:c831ddf9569f 69 void setup () {
yu10078999 0:c831ddf9569f 70 // initiation of the max 7219
yu10078999 0:c831ddf9569f 71 // SPI setup: 8 bits, mode 0
yu10078999 0:c831ddf9569f 72 max72_spi.format(8, 0);
yu10078999 0:c831ddf9569f 73
yu10078999 0:c831ddf9569f 74 // going by the datasheet, min clk is 100ns so theoretically 10MHz should work...
yu10078999 0:c831ddf9569f 75 // max72_spi.frequency(10*MHZ);
yu10078999 0:c831ddf9569f 76
yu10078999 0:c831ddf9569f 77 maxAll(max7219_reg_scanLimit, 0x07);
yu10078999 0:c831ddf9569f 78 maxAll(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
yu10078999 0:c831ddf9569f 79 maxAll(max7219_reg_shutdown, 0x01); // not in shutdown mode
yu10078999 0:c831ddf9569f 80 maxAll(max7219_reg_displayTest, 0x00); // no display test
yu10078999 0:c831ddf9569f 81 for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off
yu10078999 0:c831ddf9569f 82 maxAll(e,0);
yu10078999 0:c831ddf9569f 83 }
yu10078999 0:c831ddf9569f 84 maxAll(max7219_reg_intensity, 0x0f & 0x0f); // the first 0x0f is the value you can set
yu10078999 0:c831ddf9569f 85 // range: 0x00 to 0x0f
yu10078999 0:c831ddf9569f 86 }
yu10078999 0:c831ddf9569f 87
yu10078999 0:c831ddf9569f 88
yu10078999 1:545a966efb7b 89 /* maxSingle(1,1); // - - - - - - - +
yu10078999 0:c831ddf9569f 90 maxSingle(2,2); // - - - - - - + -
yu10078999 0:c831ddf9569f 91 maxSingle(3,4); // - - - - - + - -
yu10078999 0:c831ddf9569f 92 maxSingle(4,8); // - - - - + - - -
yu10078999 1:545a966efb7b 93 maxSingle(5,16); // - - - + - - - -
yu10078999 1:545a966efb7b 94 maxSingle(6,32); // - - + - - - - -
yu10078999 1:545a966efb7b 95 maxSingle(7,64); // - + - - - - - -
yu10078999 1:545a966efb7b 96 maxSingle(8,128); // + - - - - - - -
yu10078999 1:545a966efb7b 97 wait_ms(2000);
yu10078999 1:545a966efb7b 98 */
yu10078999 0:c831ddf9569f 99 int main() {
yu10078999 0:c831ddf9569f 100 setup ();
yu10078999 0:c831ddf9569f 101 while(1){
yu10078999 0:c831ddf9569f 102 for(int i=1;i<=8;i++){
yu10078999 0:c831ddf9569f 103 for(int j=1;j<=128;j*=2){
yu10078999 0:c831ddf9569f 104 maxSingle(i,j);
yu10078999 0:c831ddf9569f 105 wait(0.1);
yu10078999 0:c831ddf9569f 106 }
yu10078999 0:c831ddf9569f 107 }
yu10078999 0:c831ddf9569f 108 for(int i=8;i>=1;i--){
yu10078999 0:c831ddf9569f 109 for(int j=128;j>=1;j*=0.5){
yu10078999 0:c831ddf9569f 110 maxSingle(i,j);
yu10078999 0:c831ddf9569f 111 wait(0.1);
yu10078999 0:c831ddf9569f 112 }
yu10078999 0:c831ddf9569f 113 }
yu10078999 0:c831ddf9569f 114 }
yu10078999 0:c831ddf9569f 115 }