max7219 is a driver chip for driving 8-bit 7-segment digital LED or 8x8 dot matrix LED. In the way of column scanning, 64 light-emitting points are managed with 16 pin s. When displaying 8 numbers, the refresh rate is 500-1300Hz, and the typical value is 800Hz
pin function
- DIG0 - DIG7: the 8 pin s represent a 7-segment number + point number respectively
- SEGA - SEGG, DP: A-G stands for each paragraph in the 7-segment number, DP stands for Decimal Point, Decimal Point
- The driving current of each section is 40mA
- V+, GND: voltage and ground
- DIN: serial data input
- CS: chip selection. When the level is pulled down, it is moved from the serial port to the shift register in time pulse. When the level is pulled up, it is latched
- DOUT: serial data output. This port is used for cascading
- The cascaded N MAX7219 can be regarded as N 16 bit shift registers in series
- If you want to operate the nth bit, the data can arrive only after 16*N clocks. At this time, CS pulls up and locks the command
communication mode
- The communication protocol is SPI. It accepts the input of SPI Master and does not return data
- MAX7219, no matter what level CS is, DIN data will be written into the shift register with the clock
- MAX7221, data can be written from DIN or to DOUT only when CS is pulled down
- CS must be raised before the rising edge of the next clock after the 16th number is written, otherwise the data will be lost
- The data is MSB, and the large value comes first
- For 16 bit s, the first 8 (D15-D08) are addresses. In fact, only the four D11-D8 are used, and the last 8 D7-D0 are data
Address before communication
There are 14 in total
- 0x? 0: no OP, used to output data to DOUT
- 0x?1: First number
- 0x?2: Second
- 0x?3: Third
- 0x?4: Fourth
- 0x?5: Fifth
- 0x?6: Sixth
- 0x?7: Seventh
- 0x?8: Eighth
- 0x?9: Digital decoding mode
- 0x?A: Brightness, 0x00 to 0xFF
- 0x?B: Scan limit (number of digits), which affects the brightness. If the parameters of two cascades are different, the brightness will be different
- 0x?C: Status (off, normal)
- 0x?F: Test status (test, normal)
Cascade transmission
- Cascade transmission using no OP address operation
- For example, write to the fourth MAX7219
- First, write the address according to the preset address and write the value
- Write three NoOp operations (0x?0?)? The number represents an arbitrary value
- When the CS is pulled high, the four MAX7219 will receive the operation address and operation value, but the first three blocks see NoOp, so the first three blocks have no action
Driven by STC12 hardware SPI
STC12C5A60S2 series is supported by built-in SPI and is based on HML_FwLib_STC12 The package library can easily realize the dot matrix drive of MAX7219. The code is:
/*****************************************************************************/ /** * \file spi_max7219.c * \author IOsetting | iosetting@outlook.com * \date * \brief Example code of SPI driving dot matrix module * \note The module chip is MAX7219, pin connection: * P1_3 => CS, * P1_5(MOSI) => DIN, * P1_7(SPCLK) => CLK * * \version v0.1 * \ingroup example * \remarks test-board: Minimum System; test-MCU: STC12C5AF56S2 ******************************************************************************/ /***************************************************************************** * header file * *****************************************************************************/ #include "hml/hml.h" #define CS P1_3 #define DECODE_MODE 0x09 #define INTENSITY 0x0A #define SCAN_LIMIT 0x0B #define SHUT_DOWN 0x0C #define DISPLAY_TEST 0x0F const byte numbers[]={ 0x00,0x00,0x7C,0xC6,0xC6,0xCE,0xD6,0xD6, // -0-. 0xE6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00, 0x00,0x00,0x18,0x38,0x78,0x18,0x18,0x18, // -1- 0x18,0x18,0x18,0x7E,0x00,0x00,0x00,0x00, 0x00,0x00,0x7C,0xC6,0x06,0x0C,0x18,0x30, // -2- 0x60,0xC0,0xC6,0xFE,0x00,0x00,0x00,0x00, 0x00,0x00,0x7C,0xC6,0x06,0x06,0x3C,0x06, // -3- 0x06,0x06,0xC6,0x7C,0x00,0x00,0x00,0x00, 0x00,0x00,0x0C,0x1C,0x3C,0x6C,0xCC,0xFE, // -4- 0x0C,0x0C,0x0C,0x1E,0x00,0x00,0x00,0x00, 0x00,0x00,0xFE,0xC0,0xC0,0xC0,0xFC,0x0E, // -5- 0x06,0x06,0xC6,0x7C,0x00,0x00,0x00,0x00, 0x00,0x00,0x38,0x60,0xC0,0xC0,0xFC,0xC6, // -6- 0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00, 0x00,0x00,0xFE,0xC6,0x06,0x06,0x0C,0x18, // -7- 0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x00, 0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7C,0xC6, // -8- 0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00, 0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7E,0x06, // -9- 0x06,0x06,0x0C,0x78,0x00,0x00,0x00,0x00}; void Write7219(byte addr, byte dat) { CS = 0; SPI_RW(addr); SPI_RW(dat); CS = 1; } /*****************************************************************************/ /** * \author IOsetting * \date * \brief initialize MAX7219 * \param[in] * \return none * \ingroup example * \remarks ******************************************************************************/ void Init7219(void) { Write7219(SHUT_DOWN,0x01); // 0x00:shutdown, 0x01:normal Write7219(DECODE_MODE,0x00); // No decode Write7219(SCAN_LIMIT,0x07); // Display 8 digits Write7219(INTENSITY,0x00); // 0x00:min, 0xFF:max Write7219(DISPLAY_TEST,0x00); // 0x00:normal, 0x01:test mode } /*****************************************************************************/ /** * \author IOsetting * \date * \brief initialize SPI * \param[in] * \return none * \ingroup example * \remarks ******************************************************************************/ void initSys(void) { SPI_configTypeDef sc; sc.baudRatePrescaler = SPI_BaudRatePrescaler_64; sc.cpha = SPI_CPHA_1Edge; sc.cpol = SPI_CPOL_low; sc.firstBit = SPI_FirstBit_MSB; sc.pinmap = SPI_pinmap_P1; sc.nss = SPI_NSS_Soft; sc.mode = SPI_Mode_Master; SPI_config(&sc); SPI_cmd(ENABLE); } void main() { initSys(); Init7219(); P1_3 = 1; byte pos = 0, size = sizeof(numbers), i, j; while(1) { for (i = 0; i < 8; i++) { j = (pos + i) % size; Write7219(i + 1, numbers[j]); } pos = (pos + 1) % size; sleep(100); } }