lu sheng
/
nucleo_spi_max7219_led8x8
nucleo_spi_max7219_led8x8
Fork of 06_spi_max7219_led8x8 by
main.cpp@0:bd34a367f642, 2015-11-24 (annotated)
- Committer:
- icserny
- Date:
- Tue Nov 24 06:41:43 2015 +0000
- Revision:
- 0:bd34a367f642
- Child:
- 1:e065a60fea05
First version
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 | |
icserny | 0:bd34a367f642 | 10 | SPI spi(PTD2, PTD3, PTD1); // Arduino compatible MOSI, MISO, SCLK |
icserny | 0:bd34a367f642 | 11 | DigitalOut cs(PTD0); // Chip select |
icserny | 0:bd34a367f642 | 12 | |
icserny | 0:bd34a367f642 | 13 | const unsigned char led1[]= { |
icserny | 0:bd34a367f642 | 14 | 0xFF,0x18,0x18,0x18,0x18,0x18,0x18,0xFF |
icserny | 0:bd34a367f642 | 15 | }; //H |
icserny | 0:bd34a367f642 | 16 | const unsigned char led2[]= { |
icserny | 0:bd34a367f642 | 17 | 0x1F,0x60,0x80,0x40,0x40,0x80,0x60,0x1F |
icserny | 0:bd34a367f642 | 18 | }; //W |
icserny | 0:bd34a367f642 | 19 | |
icserny | 0:bd34a367f642 | 20 | /// Send two bytes to SPI bus |
icserny | 0:bd34a367f642 | 21 | void SPI_Write2(unsigned char MSB, unsigned char LSB) |
icserny | 0:bd34a367f642 | 22 | { |
icserny | 0:bd34a367f642 | 23 | cs = 0; // Set CS Low |
icserny | 0:bd34a367f642 | 24 | spi.write(MSB); // Send two bytes |
icserny | 0:bd34a367f642 | 25 | spi.write(LSB); |
icserny | 0:bd34a367f642 | 26 | cs = 1; // Set CS High |
icserny | 0:bd34a367f642 | 27 | } |
icserny | 0:bd34a367f642 | 28 | |
icserny | 0:bd34a367f642 | 29 | /// MAX7219 initialisation |
icserny | 0:bd34a367f642 | 30 | void Init_MAX7219(void) |
icserny | 0:bd34a367f642 | 31 | { |
icserny | 0:bd34a367f642 | 32 | SPI_Write2(0x09, 0x00); // Decoding off |
icserny | 0:bd34a367f642 | 33 | SPI_Write2(0x0A, 0x08); // Brightness to intermediate |
icserny | 0:bd34a367f642 | 34 | SPI_Write2(0x0B, 0x07); // Scan limit = 7 |
icserny | 0:bd34a367f642 | 35 | SPI_Write2(0x0C, 0x01); // Normal operation mode |
icserny | 0:bd34a367f642 | 36 | SPI_Write2(0x0F, 0x0F); // Enable display test |
icserny | 0:bd34a367f642 | 37 | wait_ms(500); // 500 ms delay |
icserny | 0:bd34a367f642 | 38 | SPI_Write2(0x01, 0x00); // Clear row 0. |
icserny | 0:bd34a367f642 | 39 | SPI_Write2(0x02, 0x00); // Clear row 1. |
icserny | 0:bd34a367f642 | 40 | SPI_Write2(0x03, 0x00); // Clear row 2. |
icserny | 0:bd34a367f642 | 41 | SPI_Write2(0x04, 0x00); // Clear row 3. |
icserny | 0:bd34a367f642 | 42 | SPI_Write2(0x05, 0x00); // Clear row 4. |
icserny | 0:bd34a367f642 | 43 | SPI_Write2(0x06, 0x00); // Clear row 5. |
icserny | 0:bd34a367f642 | 44 | SPI_Write2(0x07, 0x00); // Clear row 6. |
icserny | 0:bd34a367f642 | 45 | SPI_Write2(0x08, 0x00); // Clear row 7. |
icserny | 0:bd34a367f642 | 46 | SPI_Write2(0x0F, 0x00); // Disable display test |
icserny | 0:bd34a367f642 | 47 | wait_ms(500); // 500 ms delay |
icserny | 0:bd34a367f642 | 48 | } |
icserny | 0:bd34a367f642 | 49 | |
icserny | 0:bd34a367f642 | 50 | int main() |
icserny | 0:bd34a367f642 | 51 | { |
icserny | 0:bd34a367f642 | 52 | cs = 1; // CS initially High |
icserny | 0:bd34a367f642 | 53 | spi.format(8,0); // 8-bit format, mode 0,0 |
icserny | 0:bd34a367f642 | 54 | spi.frequency(1000000); // SCLK = 1 MHz |
icserny | 0:bd34a367f642 | 55 | Init_MAX7219(); // Initialize the LED controller |
icserny | 0:bd34a367f642 | 56 | while (1) { |
icserny | 0:bd34a367f642 | 57 | for(int i=1; i<9; i++) // Write first character (8 rows) |
icserny | 0:bd34a367f642 | 58 | SPI_Write2(i,led1[i-1]); |
icserny | 0:bd34a367f642 | 59 | wait(1); // 1 sec delay |
icserny | 0:bd34a367f642 | 60 | for(int i=1; i<9; i++) // Write second character |
icserny | 0:bd34a367f642 | 61 | SPI_Write2(i,led2[i-1]); |
icserny | 0:bd34a367f642 | 62 | wait(1); // 1 sec delay |
icserny | 0:bd34a367f642 | 63 | } |
icserny | 0:bd34a367f642 | 64 | } |
icserny | 0:bd34a367f642 | 65 | |
icserny | 0:bd34a367f642 | 66 | |
icserny | 0:bd34a367f642 | 67 | |
icserny | 0:bd34a367f642 | 68 | |
icserny | 0:bd34a367f642 | 69 | |
icserny | 0:bd34a367f642 | 70 |