Send data with SPI, CORE-1000

Dependencies:   mbed

Committer:
odb
Date:
Fri Feb 03 07:54:27 2017 +0000
Revision:
1:a4186d2b8561
Parent:
0:21b36798fd00
CORE-1000 First

Who changed what in which revision?

UserRevisionLine numberNew contents of line
odb 1:a4186d2b8561 1 #include "mbed.h"
odb 1:a4186d2b8561 2
odb 1:a4186d2b8561 3 // p5: DIN, p7: CLK, p8: LOAD/CS
odb 1:a4186d2b8561 4 SPI max72_spi(PA_7, NC, PA_5);
odb 1:a4186d2b8561 5 DigitalOut load(PB_6);
odb 1:a4186d2b8561 6 DigitalOut buzzer(PC_4);
odb 1:a4186d2b8561 7
odb 1:a4186d2b8561 8 int maxInUse = 1; //change this variable to set how many MAX7219's you'll use
odb 1:a4186d2b8561 9
odb 1:a4186d2b8561 10 // define max7219 registers
odb 1:a4186d2b8561 11 #define max7219_reg_noop 0x00
odb 1:a4186d2b8561 12 #define max7219_reg_digit0 0x01
odb 1:a4186d2b8561 13 #define max7219_reg_digit1 0x02
odb 1:a4186d2b8561 14 #define max7219_reg_digit2 0x03
odb 1:a4186d2b8561 15 #define max7219_reg_digit3 0x04
odb 1:a4186d2b8561 16 #define max7219_reg_digit4 0x05
odb 1:a4186d2b8561 17 #define max7219_reg_digit5 0x06
odb 1:a4186d2b8561 18 #define max7219_reg_digit6 0x07
odb 1:a4186d2b8561 19 #define max7219_reg_digit7 0x08
odb 1:a4186d2b8561 20 #define max7219_reg_decodeMode 0x09
odb 1:a4186d2b8561 21 #define max7219_reg_intensity 0x0a
odb 1:a4186d2b8561 22 #define max7219_reg_scanLimit 0x0b
odb 1:a4186d2b8561 23 #define max7219_reg_shutdown 0x0c
odb 1:a4186d2b8561 24 #define max7219_reg_displayTest 0x0f
odb 1:a4186d2b8561 25
odb 1:a4186d2b8561 26 #define LOW 0
odb 1:a4186d2b8561 27 #define HIGH 1
odb 1:a4186d2b8561 28 #define MHZ 1000000
odb 1:a4186d2b8561 29
odb 1:a4186d2b8561 30 void maxSingle( int reg, int col) {
odb 1:a4186d2b8561 31 //maxSingle is the "easy" function to use for a
odb 1:a4186d2b8561 32 //single max7219
odb 1:a4186d2b8561 33 load = LOW; // begin
odb 1:a4186d2b8561 34 max72_spi.write(reg); // specify register
odb 1:a4186d2b8561 35 max72_spi.write(col); // put data
odb 1:a4186d2b8561 36 load = HIGH; // make sure data is loaded (on rising edge of LOAD/CS)
odb 1:a4186d2b8561 37 }
odb 1:a4186d2b8561 38
odb 1:a4186d2b8561 39 void maxAll (int reg, int col) { // initialize all MAX7219's in the system
odb 1:a4186d2b8561 40 load = LOW; // begin
odb 1:a4186d2b8561 41 for ( int c=1; c<= maxInUse; c++) {
odb 1:a4186d2b8561 42 max72_spi.write(reg); // specify register
odb 1:a4186d2b8561 43 max72_spi.write(col); // put data
odb 1:a4186d2b8561 44 }
odb 1:a4186d2b8561 45 load = HIGH;
odb 1:a4186d2b8561 46 }
odb 1:a4186d2b8561 47
odb 1:a4186d2b8561 48 void maxOne(int maxNr, int reg, int col) {
odb 1:a4186d2b8561 49 //maxOne is for adressing different MAX7219's,
odb 1:a4186d2b8561 50 //while having a couple of them cascaded
odb 1:a4186d2b8561 51 int c = 0;
odb 1:a4186d2b8561 52 load = LOW;
odb 1:a4186d2b8561 53
odb 1:a4186d2b8561 54 for ( c = maxInUse; c > maxNr; c--) {
odb 1:a4186d2b8561 55 max72_spi.write(0); // no-op
odb 1:a4186d2b8561 56 max72_spi.write(0); // no-op
odb 1:a4186d2b8561 57 }
odb 1:a4186d2b8561 58
odb 1:a4186d2b8561 59 max72_spi.write(reg); // specify register
odb 1:a4186d2b8561 60 max72_spi.write(col); // put data
odb 1:a4186d2b8561 61
odb 1:a4186d2b8561 62 for ( c=maxNr-1; c >= 1; c--) {
odb 1:a4186d2b8561 63 max72_spi.write(0); // no-op
odb 1:a4186d2b8561 64 max72_spi.write(0); // no-op
odb 1:a4186d2b8561 65 }
odb 1:a4186d2b8561 66 load = HIGH;
odb 1:a4186d2b8561 67 }
odb 1:a4186d2b8561 68
odb 1:a4186d2b8561 69
odb 1:a4186d2b8561 70 void setup () {
odb 1:a4186d2b8561 71 // initiation of the max 7219
odb 1:a4186d2b8561 72 // SPI setup: 8 bits, mode 0
odb 1:a4186d2b8561 73 max72_spi.format(8, 0);
odb 1:a4186d2b8561 74
odb 1:a4186d2b8561 75 // going by the datasheet, min clk is 100ns so theoretically 10MHz should work...
odb 1:a4186d2b8561 76 // max72_spi.frequency(10*MHZ);
odb 1:a4186d2b8561 77
odb 1:a4186d2b8561 78 maxAll(max7219_reg_scanLimit, 0x07);
odb 1:a4186d2b8561 79 maxAll(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
odb 1:a4186d2b8561 80 maxAll(max7219_reg_shutdown, 0x01); // not in shutdown mode
odb 1:a4186d2b8561 81 maxAll(max7219_reg_displayTest, 0x00); // no display test
odb 1:a4186d2b8561 82 for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off
odb 1:a4186d2b8561 83 maxAll(e,0);
odb 1:a4186d2b8561 84 }
odb 1:a4186d2b8561 85 maxAll(max7219_reg_intensity, 0x0f & 0x0f); // the first 0x0f is the value you can set
odb 1:a4186d2b8561 86 // range: 0x00 to 0x0f
odb 1:a4186d2b8561 87 }
odb 1:a4186d2b8561 88
odb 1:a4186d2b8561 89 void loop () {
odb 1:a4186d2b8561 90
odb 1:a4186d2b8561 91
odb 1:a4186d2b8561 92 //if you use just one MAX7219 it should look like this
odb 1:a4186d2b8561 93 /* maxSingle(1,1); // + - - - - - - -
odb 1:a4186d2b8561 94 maxSingle(2,2); // - + - - - - - -
odb 1:a4186d2b8561 95 maxSingle(3,4); // - - + - - - - -
odb 1:a4186d2b8561 96 maxSingle(4,8); // - - - + - - - -
odb 1:a4186d2b8561 97 maxSingle(5,16); // - - - - + - - -
odb 1:a4186d2b8561 98 maxSingle(6,32); // - - - - - + - -
odb 1:a4186d2b8561 99 maxSingle(7,64); // - - - - - - + -
odb 1:a4186d2b8561 100 maxSingle(8,128); // - - - - - - - +
odb 1:a4186d2b8561 101 */
odb 1:a4186d2b8561 102 //if you use more than one MAX7219, it should look like this
odb 1:a4186d2b8561 103
odb 1:a4186d2b8561 104
odb 1:a4186d2b8561 105 maxAll(1,1); // + - - - - - - -
odb 1:a4186d2b8561 106 maxAll(2,3); // + + - - - - - -
odb 1:a4186d2b8561 107 maxAll(3,7); // + + + - - - - -
odb 1:a4186d2b8561 108 maxAll(4,15); // + + + + - - - -
odb 1:a4186d2b8561 109 maxAll(5,31); // + + + + + - - -
odb 1:a4186d2b8561 110 maxAll(6,63); // + + + + + + - -
odb 1:a4186d2b8561 111 maxAll(7,127); // + + + + + + + -
odb 1:a4186d2b8561 112 maxAll(8,255); // + + + + + + + +
odb 1:a4186d2b8561 113
odb 1:a4186d2b8561 114
odb 1:a4186d2b8561 115 //
odb 1:a4186d2b8561 116
odb 1:a4186d2b8561 117 //if you use more then one max7219 the second one should look like this
odb 1:a4186d2b8561 118
odb 1:a4186d2b8561 119
odb 1:a4186d2b8561 120 /*
odb 1:a4186d2b8561 121 maxOne(2,1,1); // + - - - - - - -
odb 1:a4186d2b8561 122 maxOne(2,2,2); // - + - - - - - -
odb 1:a4186d2b8561 123 maxOne(2,3,4); // - - + - - - - -
odb 1:a4186d2b8561 124 maxOne(2,4,8); // - - - + - - - -
odb 1:a4186d2b8561 125 maxOne(2,5,16); // - - - - + - - -
odb 1:a4186d2b8561 126 maxOne(2,6,32); // - - - - - + - -
odb 1:a4186d2b8561 127 maxOne(2,7,64); // - - - - - - + -
odb 1:a4186d2b8561 128 maxOne(2,8,128); // - - - - - - - +
odb 1:a4186d2b8561 129
odb 1:a4186d2b8561 130 */
odb 1:a4186d2b8561 131
odb 1:a4186d2b8561 132 //
odb 1:a4186d2b8561 133
odb 1:a4186d2b8561 134 wait_ms(2000);
odb 1:a4186d2b8561 135
odb 1:a4186d2b8561 136 }
odb 1:a4186d2b8561 137
odb 1:a4186d2b8561 138 int main() {
odb 1:a4186d2b8561 139 setup ();
odb 1:a4186d2b8561 140
odb 1:a4186d2b8561 141 while(1)
odb 1:a4186d2b8561 142 {
odb 1:a4186d2b8561 143 loop ();
odb 1:a4186d2b8561 144 wait(1);
odb 1:a4186d2b8561 145 buzzer = 1;
odb 1:a4186d2b8561 146 wait(0.5);
odb 1:a4186d2b8561 147 buzzer = 0;
odb 1:a4186d2b8561 148 wait(0.5);
odb 1:a4186d2b8561 149 }
odb 1:a4186d2b8561 150 }
odb 1:a4186d2b8561 151
odb 1:a4186d2b8561 152
odb 1:a4186d2b8561 153
odb 1:a4186d2b8561 154 /* 06_spi_max7219_led8x8
odb 1:a4186d2b8561 155 *
odb 1:a4186d2b8561 156 * Simple demo to drive a 8x8-as LED matrix by a MAX7219 LED driver IC
odb 1:a4186d2b8561 157 * After initialisation two characters (H and W) are displayed alternatively.
odb 1:a4186d2b8561 158 * The MAX7219 IC is driven by hardware SPI: SPI0 module at PTD1, PTD2, PTD3.
odb 1:a4186d2b8561 159 */
odb 1:a4186d2b8561 160 /*
bcostm 0:21b36798fd00 161 #include "mbed.h"
bcostm 0:21b36798fd00 162
odb 1:a4186d2b8561 163 SPI spi(PA_7, PA_6, PA_5); // Arduino compatible MOSI, MISO, SCLK
odb 1:a4186d2b8561 164 DigitalOut cs(PB_6); // Chip select
odb 1:a4186d2b8561 165 DigitalOut myled(LED1);
odb 1:a4186d2b8561 166
odb 1:a4186d2b8561 167 const unsigned char led1[]= {
odb 1:a4186d2b8561 168 0xFF,0x18,0x18,0x18,0x18,0x18,0x18,0xFF
odb 1:a4186d2b8561 169 }; //H
odb 1:a4186d2b8561 170 const unsigned char led2[]= {
odb 1:a4186d2b8561 171 0x1F,0x60,0x80,0x40,0x40,0x80,0x60,0x1F
odb 1:a4186d2b8561 172 }; //W
odb 1:a4186d2b8561 173
odb 1:a4186d2b8561 174 /// Send two bytes to SPI bus
odb 1:a4186d2b8561 175 void SPI_Write2(unsigned char MSB, unsigned char LSB)
odb 1:a4186d2b8561 176 {
odb 1:a4186d2b8561 177 cs = 0; // Set CS Low
odb 1:a4186d2b8561 178 spi.write(MSB); // Send two bytes
odb 1:a4186d2b8561 179 spi.write(LSB);
odb 1:a4186d2b8561 180 cs = 1; // Set CS High
odb 1:a4186d2b8561 181 }
bcostm 0:21b36798fd00 182
odb 1:a4186d2b8561 183 /// MAX7219 initialisation
odb 1:a4186d2b8561 184 void Init_MAX7219(void)
odb 1:a4186d2b8561 185 {
odb 1:a4186d2b8561 186 SPI_Write2(0x09, 0x00); // Decoding off
odb 1:a4186d2b8561 187 SPI_Write2(0x0A, 0x08); // Brightness to intermediate
odb 1:a4186d2b8561 188 SPI_Write2(0x0B, 0x07); // Scan limit = 7
odb 1:a4186d2b8561 189 SPI_Write2(0x0C, 0x01); // Normal operation mode
odb 1:a4186d2b8561 190 SPI_Write2(0x0F, 0x0F); // Enable display test
odb 1:a4186d2b8561 191 wait_ms(500); // 500 ms delay
odb 1:a4186d2b8561 192 SPI_Write2(0x01, 0x00); // Clear row 0.
odb 1:a4186d2b8561 193 SPI_Write2(0x02, 0x00); // Clear row 1.
odb 1:a4186d2b8561 194 SPI_Write2(0x03, 0x00); // Clear row 2.
odb 1:a4186d2b8561 195 SPI_Write2(0x04, 0x00); // Clear row 3.
odb 1:a4186d2b8561 196 SPI_Write2(0x05, 0x00); // Clear row 4.
odb 1:a4186d2b8561 197 SPI_Write2(0x06, 0x00); // Clear row 5.
odb 1:a4186d2b8561 198 SPI_Write2(0x07, 0x00); // Clear row 6.
odb 1:a4186d2b8561 199 SPI_Write2(0x08, 0x00); // Clear row 7.
odb 1:a4186d2b8561 200 SPI_Write2(0x0F, 0x00); // Disable display test
odb 1:a4186d2b8561 201 wait_ms(500); // 500 ms delay
odb 1:a4186d2b8561 202 }
odb 1:a4186d2b8561 203
odb 1:a4186d2b8561 204 int main()
odb 1:a4186d2b8561 205 {
odb 1:a4186d2b8561 206 cs = 1; // CS initially High
odb 1:a4186d2b8561 207 spi.format(8,0); // 8-bit format, mode 0,0
odb 1:a4186d2b8561 208 spi.frequency(1000000); // SCLK = 1 MHz
odb 1:a4186d2b8561 209 Init_MAX7219(); // Initialize the LED controller
odb 1:a4186d2b8561 210 while (1) {
odb 1:a4186d2b8561 211 for(int i=1; i<9; i++) // Write first character (8 rows)
odb 1:a4186d2b8561 212 SPI_Write2(i,led1[i-1]);
odb 1:a4186d2b8561 213 wait(1); // 1 sec delay
odb 1:a4186d2b8561 214 for(int i=1; i<9; i++) // Write second character
odb 1:a4186d2b8561 215 SPI_Write2(i,led2[i-1]);
odb 1:a4186d2b8561 216 wait(1); // 1 sec delay
odb 1:a4186d2b8561 217
odb 1:a4186d2b8561 218 myled = 1; // LED is ON
odb 1:a4186d2b8561 219 wait(0.5); // 200 ms
odb 1:a4186d2b8561 220 myled = 0; // LED is OFF
odb 1:a4186d2b8561 221 wait(0.5); // 1 sec
bcostm 0:21b36798fd00 222 }
bcostm 0:21b36798fd00 223 }
odb 1:a4186d2b8561 224
odb 1:a4186d2b8561 225 */