STC12 hardware SPI drives MAX7219 dot matrix LED

Publisher:RadiantBreezeLatest update time:2022-08-05 Source: csdnKeywords:STC12 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

max7219 is a driver chip for driving 8-bit 7-segment digital LED or 8x8 dot matrix LED. It uses 16 pins to manage 64 light points in a column scan mode. The refresh rate is 500-1300Hz when displaying 8 digits, and the typical value is 800Hz.


Pin Function

DIG0 - DIG7: These 8 pins represent a 7-segment number + a dot.

SEGA - SEGG, DP: AG represents each segment of the 7-segment number, DP represents the decimal point between the numbers

The driving current of each section is 40mA. If the load requires a higher current, an external hardware driver is required.

V+, GND: Voltage and Ground

DIN: Serial data input

CS: Chip select, when the level is pulled down, the data is shifted into the shift register according to the clock from the serial port, and when the level is pulled up, it is latched

DOUT: serial data output, this port is used for cascading

N MAX7219s in cascade can be viewed as N 16-bit shift registers in series.

If you want to operate on the Nth bit, the data will arrive after 16*N clocks. At this time, CS is pulled up to latch the command.


way of communication

The communication protocol is SPI, accepts input from the SPI Master, and does not return data

MAX7219, no matter what the CS level is, the data on DIN will be written into the shift register along with the clock.

MAX7221, data is written from DIN or written to DOUT only when CS is pulled down

CS must be pulled high after the 16th number is written and before the next rising edge of the clock, otherwise the data will be lost.

Data is MSB, large value first

For 16 bits, the first 8 bits (D15-D08) are addresses, and only 4 bits D11-D8 are actually used. The last 8 bits D7-D0 are data.


Previous address of correspondence

There are 14 in total

0x?0: No-Op, used to output data to DOUT

0x?1: the first number

0x?2: The second one

0x?3: The third one

0x?4: The fourth

0x?5: The fifth

0x?6: The sixth

0x?7: The seventh

0x?8: The eighth

0x?9: Digital decoding mode

0x?A: Brightness, 0x00 to 0xFF

0x?B: Scan limit (limit the number of columns displayed), the value is 0-7, the smaller the number, the brighter the single column, generally should not be less than 3.

0x?C: Status (off, normal)

0x?F: test status (test, normal)


Cascade transmission

Using No-Op address operation to achieve cascade transmission

For example, write to the fourth MAX7219

First, write the address according to the preset address, write the value

Write three NoOp operations (0x?0??), where ? represents a random value

When CS is pulled high, the four MAX7219s will receive the operation address and operation value, but the first three blocks see NoOp, so the first three blocks have no action

Use STC12 hardware SPI driver

The STC12C5A60S2 series has built-in SPI support. Based on the HML_FwLib_STC12 package library, the dot matrix driver of MAX7219 can be easily implemented. Code:


/********************************************************************************/

/** 

 * 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        

 * briefly 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        

 * briefly 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);

    }

}


refer to

Very useful instructions for MAX7219 https://wayoda.github.io/LedControl/pages/software


Keywords:STC12 Reference address:STC12 hardware SPI drives MAX7219 dot matrix LED

Previous article:STC8 MCU OLED is driven by SPI hardware interrupt (Part 1)
Next article:51 MCU package library HML_FwLib_STC89/STC11

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号