lu sheng
/
nucleo_spi_max7219_led8x8
nucleo_spi_max7219_led8x8
Fork of 06_spi_max7219_led8x8 by
main.cpp@1:e065a60fea05, 2016-11-02 (annotated)
- Committer:
- anywill
- Date:
- Wed Nov 02 02:46:17 2016 +0000
- Revision:
- 1:e065a60fea05
- Parent:
- 0:bd34a367f642
nucleo_spi_max7219_led8x8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
anywill | 1:e065a60fea05 | 1 | /* nucleo_spi_max7219_led8x8 |
anywill | 1:e065a60fea05 | 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" |
anywill | 1:e065a60fea05 | 9 | //#include "max7219.h" //此头文件未使用 |
anywill | 1:e065a60fea05 | 10 | SPI spi(D11, D12, D13); // Arduino compatible MOSI, MISO, SCLK |
anywill | 1:e065a60fea05 | 11 | //nucleo MOSI, MISO, SCLK |
anywill | 1:e065a60fea05 | 12 | // PTD2, PTD3, PTD1 |
anywill | 1:e065a60fea05 | 13 | DigitalOut cs(D10); // Chip select |
icserny | 0:bd34a367f642 | 14 | |
icserny | 0:bd34a367f642 | 15 | const unsigned char led1[]= { |
anywill | 1:e065a60fea05 | 16 | 0x0,0x66,0x99,0x81,0x42,0x24,0x18,0x0 |
anywill | 1:e065a60fea05 | 17 | }; //心 |
icserny | 0:bd34a367f642 | 18 | const unsigned char led2[]= { |
anywill | 1:e065a60fea05 | 19 | 0x3C,0x24,0x24,0xFF,0xFF,0x24,0x24,0x3C |
anywill | 1:e065a60fea05 | 20 | }; //中 |
icserny | 0:bd34a367f642 | 21 | |
icserny | 0:bd34a367f642 | 22 | /// Send two bytes to SPI bus |
icserny | 0:bd34a367f642 | 23 | void SPI_Write2(unsigned char MSB, unsigned char LSB) |
icserny | 0:bd34a367f642 | 24 | { |
icserny | 0:bd34a367f642 | 25 | cs = 0; // Set CS Low |
icserny | 0:bd34a367f642 | 26 | spi.write(MSB); // Send two bytes |
icserny | 0:bd34a367f642 | 27 | spi.write(LSB); |
icserny | 0:bd34a367f642 | 28 | cs = 1; // Set CS High |
icserny | 0:bd34a367f642 | 29 | } |
icserny | 0:bd34a367f642 | 30 | |
icserny | 0:bd34a367f642 | 31 | /// MAX7219 initialisation |
icserny | 0:bd34a367f642 | 32 | void Init_MAX7219(void) |
icserny | 0:bd34a367f642 | 33 | { |
icserny | 0:bd34a367f642 | 34 | SPI_Write2(0x09, 0x00); // Decoding off |
icserny | 0:bd34a367f642 | 35 | SPI_Write2(0x0A, 0x08); // Brightness to intermediate |
icserny | 0:bd34a367f642 | 36 | SPI_Write2(0x0B, 0x07); // Scan limit = 7 |
icserny | 0:bd34a367f642 | 37 | SPI_Write2(0x0C, 0x01); // Normal operation mode |
icserny | 0:bd34a367f642 | 38 | SPI_Write2(0x0F, 0x0F); // Enable display test |
icserny | 0:bd34a367f642 | 39 | wait_ms(500); // 500 ms delay |
icserny | 0:bd34a367f642 | 40 | SPI_Write2(0x01, 0x00); // Clear row 0. |
icserny | 0:bd34a367f642 | 41 | SPI_Write2(0x02, 0x00); // Clear row 1. |
icserny | 0:bd34a367f642 | 42 | SPI_Write2(0x03, 0x00); // Clear row 2. |
icserny | 0:bd34a367f642 | 43 | SPI_Write2(0x04, 0x00); // Clear row 3. |
icserny | 0:bd34a367f642 | 44 | SPI_Write2(0x05, 0x00); // Clear row 4. |
icserny | 0:bd34a367f642 | 45 | SPI_Write2(0x06, 0x00); // Clear row 5. |
icserny | 0:bd34a367f642 | 46 | SPI_Write2(0x07, 0x00); // Clear row 6. |
icserny | 0:bd34a367f642 | 47 | SPI_Write2(0x08, 0x00); // Clear row 7. |
icserny | 0:bd34a367f642 | 48 | SPI_Write2(0x0F, 0x00); // Disable display test |
icserny | 0:bd34a367f642 | 49 | wait_ms(500); // 500 ms delay |
icserny | 0:bd34a367f642 | 50 | } |
icserny | 0:bd34a367f642 | 51 | |
icserny | 0:bd34a367f642 | 52 | int main() |
icserny | 0:bd34a367f642 | 53 | { |
icserny | 0:bd34a367f642 | 54 | cs = 1; // CS initially High |
icserny | 0:bd34a367f642 | 55 | spi.format(8,0); // 8-bit format, mode 0,0 |
icserny | 0:bd34a367f642 | 56 | spi.frequency(1000000); // SCLK = 1 MHz |
icserny | 0:bd34a367f642 | 57 | Init_MAX7219(); // Initialize the LED controller |
anywill | 1:e065a60fea05 | 58 | |
icserny | 0:bd34a367f642 | 59 | while (1) { |
icserny | 0:bd34a367f642 | 60 | for(int i=1; i<9; i++) // Write first character (8 rows) |
icserny | 0:bd34a367f642 | 61 | SPI_Write2(i,led1[i-1]); |
icserny | 0:bd34a367f642 | 62 | wait(1); // 1 sec delay |
icserny | 0:bd34a367f642 | 63 | for(int i=1; i<9; i++) // Write second character |
icserny | 0:bd34a367f642 | 64 | SPI_Write2(i,led2[i-1]); |
icserny | 0:bd34a367f642 | 65 | wait(1); // 1 sec delay |
icserny | 0:bd34a367f642 | 66 | } |
icserny | 0:bd34a367f642 | 67 | } |