LED dot matrix and STM32

ARM, Embedded, STM32
1 Comment

Description

100%
100%

We all come across some kind of DOT matrix displays in our daily lives. Those sign boards scrolling from one end to another end and some of them even displaying some cool animation. Today in this tutorial I will show you guys How to interface LED dot matrix with STM32.

We are only going to cover the initial part today i.e. display some characters on a single 8×8 dot matrix and scrolling and other features will be covered in upcoming tutorial.

How To

First let’s see how the display even works. I have a 8×8 led display attached to a MAX7219 driver IC. The MAX7219 is compact, serial input/output common-cathode display drivers that interface microprocessors to 7- segment numeric LED displays of up to 8 digits, bar-graph displays, or 64 individual LEDs.
The following are the features of MAX7219

  • Individual LED Segment Control
  • Decode/No-Decode Digit Selection
  • 150µA Low-Power Shutdown (Data Retained)
  • Digital and Analog Brightness Control
  • Display Blanked on Power-Up
  • Drive Common-Cathode LED Display
  • Slew-Rate Limited Segment Drivers for Lower EMI (MAX7221)
  • SPI, QSPI, MICROWIRE Serial Interface (MAX7221)
  • 24-Pin DIP and SO Packages

On going through the datasheet of MAX7219, you will notice the timing diagram and the data format diagram on page-6.

TIMING and DATA FORMAT MAX7219

According to the above diagram, In order to write data to max7219, we need to do the following:-
1.) Pull the CS pin LOW
2.) Pull the clock pin LOW
3.) Write one bit to the data pin
4.) Pull the clock pin HIGH
5.) Repeat steps 2,3 and 4 until both address and data bytes are written
6.) Pull the CS pin HIGH

Also note that the data should be written in MSB first format. Let’s write the code for STM32 now. My CubeMx configuration is shown below..

Pin-cfg STM32F103c8

Some Insight into the CODE

The following is the function for writing a byte to max7219

void write_byte(uint8_t byte)
{
	    for(int i=0;i<8;i++)
          {
	    	 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 0);  // Pull the CLK LOW
	    	 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, byte&0x80);// Write one BIT data MSB first
             byte = byte<<1;  // shift the data to the left
             HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, 1);  // Pull the CLK HIGH
           }
}

As you can see above that for writing each bit of data, we have to Pull the CLK pin low and than pull it high after writing the bit. Also byte&0x80 means that we are writing the MSB bit and than shifting the bit to left by using byte<<1.
Next we need to write another function for writing address and data to the MAX.

void write_max (uint8_t address, uint8_t data)
{
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, 0);   // Pull the CS pin LOW
    write_byte(address);   //  write address
    write_byte(data);  //  write data
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, 1);  // pull the CS HIGH
}

Here we have to pull the CS pin LOW and after writing both address and data, pull it back to HIGH.

void max_init(void)
{
 write_max(0x09, 0x00);       //  no decoding
 write_max(0x0a, 0x03);       //  brightness intensity
 write_max(0x0b, 0x07);       //  scan limit = 8 LEDs
 write_max(0x0c, 0x01);       //  power down =0,normal mode = 1
 write_max(0x0f, 0x00);       //  no test display
}

TO initialize the DOT matrix display we have to do the following:-
–> Set no decode in the Decode mode register (0x09)
–> Set intensity value in the intensity register (0x0A)
–> Scan limit = 8 LEDs in 0x0b
–> value of 1 in the 0x0c denotes the normal power on mode
–> Set 0 in the Display test register (0x0f)

Now in order to display any character (For eg ‘A’), Think of the Registers from 0x01 to 0x08 as the addresses of the 8 ROWs and we need to turn the respective LEDs ON in each ROW. The following are the values for each row to display ‘A’
{0x18,0x24,0x42,0x42,0x7E,0x42,0x42,0x42}
You can check the code for more details.

YOU CAN DOWNLOAD FULL CODE AT THE END OF THIS POST

0x18, 0x24, 0x42, 0x42, 0x7E, 0x42, 0x42, 0x42
100%
100%

Connection

100%
100%
100%
100%

Result

100%
100%
Check out the VIDEO Below

DOWNLOAD

You can buy me a coffee sensor 🙂

download the CODE below

100%
100%
, , , , , , ,

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
S R Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
S R
Guest
S R

Can we develop the same using HAL SPI API’s ?

Menu