![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Simple demo to drive a 8x8-as LED matrix by a MAX7219 LED driver IC. After initialisation two characters (H and W) are displayed alternatively. The MAX7219 IC is driven by hardware SPI: SPI0 module at PTD1, PTD2, PTD3.
main.cpp@1:a354187e025d, 2020-05-18 (annotated)
- Committer:
- Diego_Mbed
- Date:
- Mon May 18 16:23:09 2020 +0000
- Revision:
- 1:a354187e025d
- Parent:
- 0:bd34a367f642
Controlador con driver max7219
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icserny | 0:bd34a367f642 | 1 | /* 06_spi_max7219_led8x8 |
icserny | 0:bd34a367f642 | 2 | * |
icserny | 0:bd34a367f642 | 3 | * Simple demo to drive a 8x8-as LED matrix by a MAX7219 LED driver IC |
icserny | 0:bd34a367f642 | 4 | * After initialisation two characters (H and W) are displayed alternatively. |
icserny | 0:bd34a367f642 | 5 | * The MAX7219 IC is driven by hardware SPI: SPI0 module at PTD1, PTD2, PTD3. |
icserny | 0:bd34a367f642 | 6 | */ |
icserny | 0:bd34a367f642 | 7 | |
icserny | 0:bd34a367f642 | 8 | #include "mbed.h" |
icserny | 0:bd34a367f642 | 9 | |
Diego_Mbed | 1:a354187e025d | 10 | SPI spi(D11, NC, D13); // Arduino compatible MOSI, MISO, SCLK |
Diego_Mbed | 1:a354187e025d | 11 | DigitalOut cs(D10); // Chip select |
icserny | 0:bd34a367f642 | 12 | |
Diego_Mbed | 1:a354187e025d | 13 | const unsigned char num1[]= { |
Diego_Mbed | 1:a354187e025d | 14 | 0x00,0x00,0x00,0x42,0x7F,0x40,0x00,0x00 |
Diego_Mbed | 1:a354187e025d | 15 | }; //1 |
Diego_Mbed | 1:a354187e025d | 16 | const unsigned char num2[]= { |
Diego_Mbed | 1:a354187e025d | 17 | 0x00,0x46,0x61,0x51,0x49,0x46,0x00,0x00 |
Diego_Mbed | 1:a354187e025d | 18 | }; //2 |
Diego_Mbed | 1:a354187e025d | 19 | const unsigned char num3[]= { |
Diego_Mbed | 1:a354187e025d | 20 | 0x00,0x22,0x41,0x49,0x49,0x36,0x00,0x00 |
Diego_Mbed | 1:a354187e025d | 21 | }; //3 |
Diego_Mbed | 1:a354187e025d | 22 | const unsigned char num4[]= { |
Diego_Mbed | 1:a354187e025d | 23 | 0x00,0x0C,0x0A,0x09,0x7F,0x08,0x00,0x00 |
Diego_Mbed | 1:a354187e025d | 24 | }; //4 |
Diego_Mbed | 1:a354187e025d | 25 | const unsigned char num5[]= { |
Diego_Mbed | 1:a354187e025d | 26 | 0x00,0x00,0x4F,0x49,0x49,0x31,0x00,0x00 |
Diego_Mbed | 1:a354187e025d | 27 | }; //5 |
Diego_Mbed | 1:a354187e025d | 28 | const unsigned char num6[]= { |
Diego_Mbed | 1:a354187e025d | 29 | 0x00,0x00,0x3C,0x4A,0x49,0x30,0x00,0x00 |
Diego_Mbed | 1:a354187e025d | 30 | }; //6 |
Diego_Mbed | 1:a354187e025d | 31 | const unsigned char num7[]= { |
Diego_Mbed | 1:a354187e025d | 32 | 0x00,0x00,0x61,0x11,0x09,0x07,0x00,0x00 |
Diego_Mbed | 1:a354187e025d | 33 | }; //7 |
Diego_Mbed | 1:a354187e025d | 34 | const unsigned char num8[]= { |
Diego_Mbed | 1:a354187e025d | 35 | 0x00,0x00,0x36,0x49,0x49,0x36,0x00,0x00 |
Diego_Mbed | 1:a354187e025d | 36 | }; //8 |
Diego_Mbed | 1:a354187e025d | 37 | const unsigned char num9[]= { |
Diego_Mbed | 1:a354187e025d | 38 | 0x00,0x00,0x06,0x49,0x49,0x3E,0x00,0x00 |
Diego_Mbed | 1:a354187e025d | 39 | }; //9 |
Diego_Mbed | 1:a354187e025d | 40 | const unsigned char num0[]= { |
Diego_Mbed | 1:a354187e025d | 41 | 0x00,0x00,0x3E,0x41,0x41,0x3E,0x00,0x00 |
Diego_Mbed | 1:a354187e025d | 42 | }; //0 |
icserny | 0:bd34a367f642 | 43 | |
icserny | 0:bd34a367f642 | 44 | /// Send two bytes to SPI bus |
icserny | 0:bd34a367f642 | 45 | void SPI_Write2(unsigned char MSB, unsigned char LSB) |
icserny | 0:bd34a367f642 | 46 | { |
icserny | 0:bd34a367f642 | 47 | cs = 0; // Set CS Low |
icserny | 0:bd34a367f642 | 48 | spi.write(MSB); // Send two bytes |
icserny | 0:bd34a367f642 | 49 | spi.write(LSB); |
icserny | 0:bd34a367f642 | 50 | cs = 1; // Set CS High |
icserny | 0:bd34a367f642 | 51 | } |
icserny | 0:bd34a367f642 | 52 | |
icserny | 0:bd34a367f642 | 53 | /// MAX7219 initialisation |
icserny | 0:bd34a367f642 | 54 | void Init_MAX7219(void) |
icserny | 0:bd34a367f642 | 55 | { |
icserny | 0:bd34a367f642 | 56 | SPI_Write2(0x09, 0x00); // Decoding off |
icserny | 0:bd34a367f642 | 57 | SPI_Write2(0x0A, 0x08); // Brightness to intermediate |
icserny | 0:bd34a367f642 | 58 | SPI_Write2(0x0B, 0x07); // Scan limit = 7 |
icserny | 0:bd34a367f642 | 59 | SPI_Write2(0x0C, 0x01); // Normal operation mode |
icserny | 0:bd34a367f642 | 60 | SPI_Write2(0x0F, 0x0F); // Enable display test |
icserny | 0:bd34a367f642 | 61 | wait_ms(500); // 500 ms delay |
icserny | 0:bd34a367f642 | 62 | SPI_Write2(0x01, 0x00); // Clear row 0. |
icserny | 0:bd34a367f642 | 63 | SPI_Write2(0x02, 0x00); // Clear row 1. |
icserny | 0:bd34a367f642 | 64 | SPI_Write2(0x03, 0x00); // Clear row 2. |
icserny | 0:bd34a367f642 | 65 | SPI_Write2(0x04, 0x00); // Clear row 3. |
icserny | 0:bd34a367f642 | 66 | SPI_Write2(0x05, 0x00); // Clear row 4. |
icserny | 0:bd34a367f642 | 67 | SPI_Write2(0x06, 0x00); // Clear row 5. |
icserny | 0:bd34a367f642 | 68 | SPI_Write2(0x07, 0x00); // Clear row 6. |
icserny | 0:bd34a367f642 | 69 | SPI_Write2(0x08, 0x00); // Clear row 7. |
icserny | 0:bd34a367f642 | 70 | SPI_Write2(0x0F, 0x00); // Disable display test |
icserny | 0:bd34a367f642 | 71 | wait_ms(500); // 500 ms delay |
icserny | 0:bd34a367f642 | 72 | } |
icserny | 0:bd34a367f642 | 73 | |
icserny | 0:bd34a367f642 | 74 | int main() |
icserny | 0:bd34a367f642 | 75 | { |
icserny | 0:bd34a367f642 | 76 | cs = 1; // CS initially High |
icserny | 0:bd34a367f642 | 77 | spi.format(8,0); // 8-bit format, mode 0,0 |
icserny | 0:bd34a367f642 | 78 | spi.frequency(1000000); // SCLK = 1 MHz |
icserny | 0:bd34a367f642 | 79 | Init_MAX7219(); // Initialize the LED controller |
icserny | 0:bd34a367f642 | 80 | while (1) { |
icserny | 0:bd34a367f642 | 81 | for(int i=1; i<9; i++) // Write first character (8 rows) |
Diego_Mbed | 1:a354187e025d | 82 | SPI_Write2(i,num1[i-1]); |
icserny | 0:bd34a367f642 | 83 | wait(1); // 1 sec delay |
icserny | 0:bd34a367f642 | 84 | for(int i=1; i<9; i++) // Write second character |
Diego_Mbed | 1:a354187e025d | 85 | SPI_Write2(i,num2[i-1]); |
Diego_Mbed | 1:a354187e025d | 86 | wait(1); |
Diego_Mbed | 1:a354187e025d | 87 | for(int i=1; i<9; i++) // Write second character |
Diego_Mbed | 1:a354187e025d | 88 | SPI_Write2(i,num3[i-1]); |
Diego_Mbed | 1:a354187e025d | 89 | wait(1); |
Diego_Mbed | 1:a354187e025d | 90 | for(int i=1; i<9; i++) // Write second character |
Diego_Mbed | 1:a354187e025d | 91 | SPI_Write2(i,num4[i-1]); |
Diego_Mbed | 1:a354187e025d | 92 | wait(1); |
Diego_Mbed | 1:a354187e025d | 93 | for(int i=1; i<9; i++) // Write second character |
Diego_Mbed | 1:a354187e025d | 94 | SPI_Write2(i,num5[i-1]); |
Diego_Mbed | 1:a354187e025d | 95 | wait(1); |
Diego_Mbed | 1:a354187e025d | 96 | for(int i=1; i<9; i++) // Write second character |
Diego_Mbed | 1:a354187e025d | 97 | SPI_Write2(i,num6[i-1]); |
Diego_Mbed | 1:a354187e025d | 98 | wait(1); |
Diego_Mbed | 1:a354187e025d | 99 | for(int i=1; i<9; i++) // Write second character |
Diego_Mbed | 1:a354187e025d | 100 | SPI_Write2(i,num7[i-1]); |
Diego_Mbed | 1:a354187e025d | 101 | wait(1); |
Diego_Mbed | 1:a354187e025d | 102 | for(int i=1; i<9; i++) // Write second character |
Diego_Mbed | 1:a354187e025d | 103 | SPI_Write2(i,num8[i-1]); |
Diego_Mbed | 1:a354187e025d | 104 | wait(1); |
Diego_Mbed | 1:a354187e025d | 105 | for(int i=1; i<9; i++) // Write second character |
Diego_Mbed | 1:a354187e025d | 106 | SPI_Write2(i,num9[i-1]); |
Diego_Mbed | 1:a354187e025d | 107 | wait(1); |
Diego_Mbed | 1:a354187e025d | 108 | for(int i=1; i<9; i++) // Write second character |
Diego_Mbed | 1:a354187e025d | 109 | SPI_Write2(i,num0[i-1]); |
icserny | 0:bd34a367f642 | 110 | wait(1); // 1 sec delay |
icserny | 0:bd34a367f642 | 111 | } |
icserny | 0:bd34a367f642 | 112 | } |
icserny | 0:bd34a367f642 | 113 | |
icserny | 0:bd34a367f642 | 114 | |
icserny | 0:bd34a367f642 | 115 | |
icserny | 0:bd34a367f642 | 116 | |
icserny | 0:bd34a367f642 | 117 | |
icserny | 0:bd34a367f642 | 118 |