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