Luka Elmir

Dependencies:   mbed

Committer:
tim003
Date:
Mon Jun 02 15:35:11 2014 +0000
Revision:
6:dd675b967b60
Parent:
2:e4d483d82cd8
LIE26

Who changed what in which revision?

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