Cascading Dot Matrix and STM32
Some time ago, we did interface a dot matrix display with STM32. Today in this tutorial I am going to show you guys How to program Dot matrix displays in cascade using STM32. I will also show you guys how to scroll a single character or the entire string in different direction. I am using STM32f103c8 microcontroller and FC16 MAX7219 Dot matrix Displays, which I found using MD_MAX72XX library for arduino.
In order to connect the displays in cascading mode, the data output pin (Dout) of one is connected to data input pin (Din) of the other display. All the displays are connected to the same CS pin. Now to write data to the display I have written a library and in this tutorial, I am going to go through some functions and their usage.
How to
Ok so after generating the code through CubeMx, next step is to include the library and to do that, put the max_matrix_stm32.h in the inc folder and put max_matrix_stm32.c in the src folder.
First of all we need to change few details in the max_matrix_stm32.h file and they are as follows
// change the max7219 PORT and Pins below
#define maxport GPIOA
#define data_Pin GPIO_PIN_2
#define cs_Pin GPIO_PIN_3
#define clock_Pin GPIO_PIN_4
// Set the number of dot-matrix-displays being used
#define num 4
maxport : is the common port for all the pins that are being used in the display
data_pin : the pin, Data pin is connected to
and same for the cs_pin and clock_pin
num : the number of displays connected in cascade
Now let’s initiate the displays. The following function initialises the connected displays with the brightness varying from 1-15.
void max_init (uint8_t brightness);
Next we are going to write a character on the display and to do that we have the following function
void write_char (char c, uint8_t max)
It takes 2 parameters:
‘c’ : is the character to write
‘max’ : is the number of the dot matrix display, where you want to write the character. This should start with 1.
write_char ('A',4); // write 'A' on 4th display
write_char ('2',3); // write '2' on 3rd display
write_char (3,2); // print heart on 2nd display
write_char ('m',1); // write 'm'on st display
In order to write a character with left scroll or right scroll effects, the following function is used.
void scroll_char (char c, uint32_t speed, char direction);
It takes three parameters:
‘c’ : is the character which you want to shift
‘speed’ : is the time delay in ms between each shift
‘direction’ : is the direction of the shift. This value can only be either left or right
Scrolling the entire string is also same as shifting a character.
void shift_string (uint8_t *string, uint32_t speed, char direction);
The above function takes 3 parameters:
‘*string’ : Pointer to the string that you want to shift on the display. If you want to input the string directly, you have to type convert it eg- (uint8_t *) “string here”.
speed and direction are mentioned above.
You can check the rest of the functions in the **.h file and the **.c file itself. The code is commented properly so that you guys can understand it.