affichage MAX7219 4 afficheur 8x8 en SPI 4 in 1 Dot Matrix MAX7219

Dependencies:   mbed

Committer:
jlsalvat
Date:
Thu Jun 16 12:09:52 2022 +0000
Revision:
3:97af1281969a
Parent:
2:675b923da5d2
ajout affichage horizontal;

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"
jlsalvat 3:97af1281969a 10 #include "ascii_char_h.h"
icserny 0:bd34a367f642 11
vermaelen 1:61f61158c345 12 SPI spi(D11,D12,D13); // Arduino compatible MOSI, MISO, SCLK
vermaelen 2:675b923da5d2 13 DigitalOut cs(D14);
vermaelen 1:61f61158c345 14
vermaelen 2:675b923da5d2 15 void MAX7219_init(char noChips)
vermaelen 1:61f61158c345 16 {
vermaelen 1:61f61158c345 17 cs = 1; // CS initially High
vermaelen 1:61f61158c345 18 spi.format(8,0); // 8-bit format, mode 0,0
vermaelen 1:61f61158c345 19 spi.frequency(1000000); // SCLK = 1 MHz
vermaelen 2:675b923da5d2 20 while(noChips)
vermaelen 2:675b923da5d2 21 MAX7219_config(--noChips);
vermaelen 2:675b923da5d2 22 }
vermaelen 2:675b923da5d2 23 //------------------------------------------------------------------------------
vermaelen 2:675b923da5d2 24 void MAX7219_config(char chip)
vermaelen 2:675b923da5d2 25 {
vermaelen 2:675b923da5d2 26 MAX7219_write(DECODE_MODE_REG,DISABLE_DECODE,chip);
vermaelen 2:675b923da5d2 27 MAX7219_write(INTESITY_REG,BRIGHTNESS,chip);
vermaelen 2:675b923da5d2 28 MAX7219_write(SCAN_LIMIT_REG,SCAN_ALL_DIGITS,chip);
vermaelen 2:675b923da5d2 29 MAX7219_write(SHUTDOWN_REG,NORMAL_OPERATION,chip);
vermaelen 2:675b923da5d2 30 MAX7219_write(DISPLAY_TEST_REG,DISABLE_TEST,chip);
vermaelen 2:675b923da5d2 31 }
vermaelen 2:675b923da5d2 32 //------------------------------------------------------------------------------
vermaelen 2:675b923da5d2 33 void MAX7219_write(char regName,char data,char chip)
vermaelen 2:675b923da5d2 34 {
vermaelen 2:675b923da5d2 35 cs = 0;
vermaelen 2:675b923da5d2 36
vermaelen 2:675b923da5d2 37 spi.write(regName);
vermaelen 2:675b923da5d2 38 spi.write(data);
vermaelen 2:675b923da5d2 39 while(chip--)
vermaelen 2:675b923da5d2 40 MAX7219_NoOperation(); //Used for daisy chained (Cascaded) arrangements
vermaelen 2:675b923da5d2 41
vermaelen 2:675b923da5d2 42 cs = 1;
vermaelen 2:675b923da5d2 43 }
vermaelen 2:675b923da5d2 44 //------------------------------------------------------------------------------
vermaelen 2:675b923da5d2 45 void MAX7219_displayText(char* text)
vermaelen 2:675b923da5d2 46 {
vermaelen 2:675b923da5d2 47 char chip = 0;
vermaelen 2:675b923da5d2 48
vermaelen 2:675b923da5d2 49 while(*text) {
vermaelen 2:675b923da5d2 50 char row = (*text++) - 32;//(Text-32)...because the first 32 ASCII character codes are none Printable (control chars)
vermaelen 1:61f61158c345 51
vermaelen 2:675b923da5d2 52 for(int col = 0; col < 8; col++) {
jlsalvat 3:97af1281969a 53 MAX7219_write( col+1, symbol_h[row][col], chip );
vermaelen 2:675b923da5d2 54 }
vermaelen 2:675b923da5d2 55
vermaelen 2:675b923da5d2 56 chip++;
vermaelen 1:61f61158c345 57 }
vermaelen 1:61f61158c345 58 }
vermaelen 2:675b923da5d2 59 void MAX7219_displayText(char* text, char indice, char nb_chip)
vermaelen 2:675b923da5d2 60 {
vermaelen 2:675b923da5d2 61 char chip = 0;
vermaelen 2:675b923da5d2 62 char * current_pointer;
vermaelen 2:675b923da5d2 63 current_pointer=text+indice;
vermaelen 2:675b923da5d2 64 while(chip<nb_chip) {
vermaelen 2:675b923da5d2 65 char row = (*current_pointer++) - 32;//(Text-32)...because the first 32 ASCII character codes are none Printable (control chars)
vermaelen 2:675b923da5d2 66 for(int col = 0; col < 8; col++) {
jlsalvat 3:97af1281969a 67 MAX7219_write( col+1, symbol_h[row][col], chip );
vermaelen 2:675b923da5d2 68 }
vermaelen 2:675b923da5d2 69 chip++;
vermaelen 2:675b923da5d2 70 }
vermaelen 2:675b923da5d2 71 }
vermaelen 2:675b923da5d2 72 void MAX7219_display(unsigned const char led[], char no_chip)
vermaelen 2:675b923da5d2 73 {
vermaelen 2:675b923da5d2 74 for(int col = 0; col < 8; col++) {
vermaelen 2:675b923da5d2 75 MAX7219_write( col+1, led[col], no_chip );
vermaelen 2:675b923da5d2 76 }
vermaelen 2:675b923da5d2 77 }
vermaelen 2:675b923da5d2 78 //-----------Passes the data to the adjacent MAX7219 in the Daisy Chain---------
vermaelen 2:675b923da5d2 79 void MAX7219_NoOperation()
vermaelen 2:675b923da5d2 80 {
vermaelen 2:675b923da5d2 81 spi.write(NO_OP_REG);
vermaelen 2:675b923da5d2 82 spi.write(0x00); //Don't care (Can be any arbitrary value)
vermaelen 2:675b923da5d2 83 }
icserny 0:bd34a367f642 84
vermaelen 2:675b923da5d2 85 int main()
vermaelen 1:61f61158c345 86 {
vermaelen 2:675b923da5d2 87 MAX7219_init(4);
vermaelen 2:675b923da5d2 88 while(1) {
vermaelen 2:675b923da5d2 89 MAX7219_displayText("ABCD");
vermaelen 2:675b923da5d2 90 wait(2);
vermaelen 2:675b923da5d2 91 MAX7219_displayText("HELL");
vermaelen 2:675b923da5d2 92 wait(2);
vermaelen 2:675b923da5d2 93 MAX7219_display(led_blank,0);
vermaelen 2:675b923da5d2 94 MAX7219_display(led_heart,1);
vermaelen 2:675b923da5d2 95 MAX7219_display(led_heart,2);
vermaelen 2:675b923da5d2 96 MAX7219_display(led_blank,3);
vermaelen 2:675b923da5d2 97 wait(2);
vermaelen 2:675b923da5d2 98 for(int i=0;i<7;i++){
vermaelen 2:675b923da5d2 99 MAX7219_displayText("HELLO WORLD",i,4);
vermaelen 2:675b923da5d2 100 wait(2);
vermaelen 2:675b923da5d2 101 }
vermaelen 1:61f61158c345 102 }
icserny 0:bd34a367f642 103
vermaelen 2:675b923da5d2 104 }