Afficher Hello

Dependencies:   mbed

Committer:
vermaelen
Date:
Mon Mar 28 10:11:31 2022 +0000
Revision:
1:61f61158c345
Parent:
0:bd34a367f642
v1;

Who changed what in which revision?

UserRevisionLine numberNew 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"
vermaelen 1:61f61158c345 9 #include "lib_matrix.h"
icserny 0:bd34a367f642 10
vermaelen 1:61f61158c345 11 SPI spi(D11,D12,D13); // Arduino compatible MOSI, MISO, SCLK
vermaelen 1:61f61158c345 12 DigitalOut cs(D14); // Chip select
vermaelen 1:61f61158c345 13
vermaelen 1:61f61158c345 14 int main()
vermaelen 1:61f61158c345 15 {
vermaelen 1:61f61158c345 16 cs = 1; // CS initially High
vermaelen 1:61f61158c345 17 spi.format(8,0); // 8-bit format, mode 0,0
vermaelen 1:61f61158c345 18 spi.frequency(1000000); // SCLK = 1 MHz
vermaelen 1:61f61158c345 19 Init_MAX7219(); // Initialize the LED controller
vermaelen 1:61f61158c345 20 char tab[20]="HELLOWO&&&";
vermaelen 1:61f61158c345 21 int i=0;
vermaelen 1:61f61158c345 22 while (1) {
vermaelen 1:61f61158c345 23 disp(tab[i]);
vermaelen 1:61f61158c345 24 i=(i+1)%10;
vermaelen 1:61f61158c345 25 wait(0.5);
vermaelen 1:61f61158c345 26 disp(' ');
vermaelen 1:61f61158c345 27 wait(0.1); // 1 sec delay
vermaelen 1:61f61158c345 28
vermaelen 1:61f61158c345 29 }
vermaelen 1:61f61158c345 30 }
icserny 0:bd34a367f642 31
vermaelen 1:61f61158c345 32 void disp(char c)
vermaelen 1:61f61158c345 33 {
vermaelen 1:61f61158c345 34 switch(c) {
vermaelen 1:61f61158c345 35 case 'E' :
vermaelen 1:61f61158c345 36 for(int i=1; i<9; i++) // Write first character (8 rows)
vermaelen 1:61f61158c345 37 SPI_Write2(i,led_E[i-1]);
vermaelen 1:61f61158c345 38 break;
vermaelen 1:61f61158c345 39 case 'H' :
vermaelen 1:61f61158c345 40 for(int i=1; i<9; i++) // Write first character (8 rows)
vermaelen 1:61f61158c345 41 SPI_Write2(i,led_H[i-1]);
vermaelen 1:61f61158c345 42 break;
vermaelen 1:61f61158c345 43 case 'L' :
vermaelen 1:61f61158c345 44 for(int i=1; i<9; i++) // Write first character (8 rows)
vermaelen 1:61f61158c345 45 SPI_Write2(i,led_L[i-1]);
vermaelen 1:61f61158c345 46 break;
vermaelen 1:61f61158c345 47 case 'O' :
vermaelen 1:61f61158c345 48 for(int i=1; i<9; i++) // Write first character (8 rows)
vermaelen 1:61f61158c345 49 SPI_Write2(i,led_O[i-1]);
vermaelen 1:61f61158c345 50 break;
vermaelen 1:61f61158c345 51 case 'W' : // 1 sec delay
vermaelen 1:61f61158c345 52 for(int i=1; i<9; i++) // Write second character
vermaelen 1:61f61158c345 53 SPI_Write2(i,led_W[i-1]);
vermaelen 1:61f61158c345 54 break;
vermaelen 1:61f61158c345 55 case ' ' : // 1 sec delay
vermaelen 1:61f61158c345 56 for(int i=1; i<9; i++) // Write second character
vermaelen 1:61f61158c345 57 SPI_Write2(i,led_blank[i-1]);
vermaelen 1:61f61158c345 58 break;
vermaelen 1:61f61158c345 59 case '&' : // 1 sec delay
vermaelen 1:61f61158c345 60 for(int i=1; i<9; i++) // Write second character
vermaelen 1:61f61158c345 61 SPI_Write2(i,led_heart[i-1]);
vermaelen 1:61f61158c345 62 break;
vermaelen 1:61f61158c345 63 }
vermaelen 1:61f61158c345 64 }
icserny 0:bd34a367f642 65 /// Send two bytes to SPI bus
icserny 0:bd34a367f642 66 void SPI_Write2(unsigned char MSB, unsigned char LSB)
icserny 0:bd34a367f642 67 {
icserny 0:bd34a367f642 68 cs = 0; // Set CS Low
icserny 0:bd34a367f642 69 spi.write(MSB); // Send two bytes
icserny 0:bd34a367f642 70 spi.write(LSB);
icserny 0:bd34a367f642 71 cs = 1; // Set CS High
icserny 0:bd34a367f642 72 }
icserny 0:bd34a367f642 73
icserny 0:bd34a367f642 74 /// MAX7219 initialisation
icserny 0:bd34a367f642 75 void Init_MAX7219(void)
icserny 0:bd34a367f642 76 {
icserny 0:bd34a367f642 77 SPI_Write2(0x09, 0x00); // Decoding off
icserny 0:bd34a367f642 78 SPI_Write2(0x0A, 0x08); // Brightness to intermediate
icserny 0:bd34a367f642 79 SPI_Write2(0x0B, 0x07); // Scan limit = 7
icserny 0:bd34a367f642 80 SPI_Write2(0x0C, 0x01); // Normal operation mode
icserny 0:bd34a367f642 81 SPI_Write2(0x0F, 0x0F); // Enable display test
icserny 0:bd34a367f642 82 wait_ms(500); // 500 ms delay
icserny 0:bd34a367f642 83 SPI_Write2(0x01, 0x00); // Clear row 0.
icserny 0:bd34a367f642 84 SPI_Write2(0x02, 0x00); // Clear row 1.
icserny 0:bd34a367f642 85 SPI_Write2(0x03, 0x00); // Clear row 2.
icserny 0:bd34a367f642 86 SPI_Write2(0x04, 0x00); // Clear row 3.
icserny 0:bd34a367f642 87 SPI_Write2(0x05, 0x00); // Clear row 4.
icserny 0:bd34a367f642 88 SPI_Write2(0x06, 0x00); // Clear row 5.
icserny 0:bd34a367f642 89 SPI_Write2(0x07, 0x00); // Clear row 6.
icserny 0:bd34a367f642 90 SPI_Write2(0x08, 0x00); // Clear row 7.
icserny 0:bd34a367f642 91 SPI_Write2(0x0F, 0x00); // Disable display test
icserny 0:bd34a367f642 92 wait_ms(500); // 500 ms delay
icserny 0:bd34a367f642 93 }