
Running the same STM32 at the end of a long chain of 8×8 LED matrices sounds simple, but a single misplaced chip-select edge can leave all four MAX7219 modules dark. In this guide, we’ll drive a 4-in-1 MAX7219 module from an STM32 over SPI with the HAL, build a small daisy-chain driver that latches every row correctly, and use it to run smooth scrolling text and frame-based animations.
In this guide, we shall cover the following:
- Introduction.
- STM32CubeMX setup.
- Hardware connection
- Importing the project to STM32CubeIDE.
- Firmware development.
- Results.
1. Introduction:
8×8 LED matrices are one of the most satisfying displays to bring up on a microcontroller. They’re bright, readable from across a room, cheap, and everything you draw on them is easy to see. The common 4-in-1 module puts four of them side by side on one board, each driven by its own MAX7219, which gives you a 32×8 pixel canvas. That’s enough for scrolling messages, status indicators, simple games and animations. Because the four MAX7219 chips are daisy-chained, the whole display needs only three signal lines from the MCU: data, clock and chip select.
That simplicity hides a few traps. The MAX7219 doesn’t act on data as it arrives. It shifts each 16-bit command along the chain and latches it only when chip select goes high. Send too many commands in one chip-select burst and earlier ones are pushed out of the chain before they’re latched, so only part of the frame appears. Raise chip select before the SPI peripheral has finished shifting the last bit and the chips latch a corrupted word. Start talking to the modules before their 5 V supply has settled and they stay in shutdown, giving you a perfectly blank display with no obvious cause. Add the mismatch between a 3.3 V STM32 and a 5 V display driver, plus modules whose blocks run right-to-left or are mirrored, and it’s easy to lose an evening looking at a dark display or garbled text.
In this guide, we’ll build the driver step by step on an STM32 using the HAL and SPI1, and explain why each piece is there. We’ll start with the register map and how the daisy chain shifts data. Then we’ll write low-level routines that broadcast a command to every chip, address a single chip with No-Op padding, and refresh a whole frame with one latch per row. Along the way we’ll go through the debugging steps that isolate wiring, SPI configuration and initialisation problems, including the display-test register, which shows straight away whether the MCU is reaching the chips at all.
With a working driver in place, we’ll add a small graphics layer: a pixel function that hides the module order and bit orientation, so the rest of the code can treat the display as a plain 32×8 grid. On top of that we’ll build a smooth text scroller using a 5×7 font. Finally, we’ll write a set of frame-based animations: a bouncing ball, travelling sine waves, rain, a ripple of rectangles and Conway’s Game of Life. They run at a steady frame rate from a single loop in main.c.
To follow along, you’ll need an STM32 development board, a 4-in-1 MAX7219 LED matrix module, a few jumper wires and STM32CubeIDE. All of the user code lives inside CubeMX’s USER CODE blocks, so you can regenerate the project without losing anything. By the end, you’ll have a reusable MAX7219 driver, and you’ll understand what happens on the SPI lines well enough to adapt it to longer chains, other STM32 families or displays of your own design.
2. STM32CubeMX Setup.
Open STM32CubeMX as start a new project as follows:

Search for your STM32 MCU, select the MCU and click on Start New Project as follows:

Next, from connectivity, select SPI and configure it as follows:
- Set the mode to master half-duplex, no need for full duplex.
- Set the prescaler to get a speed between 1 and 4 Mbps. Here, we shall use 2Mbps.

Further mode, set PA0 as output, this shall be the CS pin.
Next, from Project Manager tab, Code Generation tab, check Generate peripheral initialization as pair of .c/.h files for each peripheral.

Next, from project tab, give the project a name, set toolchain/IDE to STM32CubeIDE and click on Generate Code.

Thats all for STM32CubeMX.
3. Hardware Connection:
The connection as follows:
| MAX7219 LED Matrix | STM32F411-Nucleo-64 |
| Vcc | 5V |
| GND | GND |
| Din | PA7 |
| CS | PA0 |
| CLK | PA5 |

4. Importing the Project to STM32CubeMX:
Open STM32CubeIDE, select your workspace and click on Launch.
From the IDE, click File and select STM32 Project Create/Import as follows:

Next, from Import STM32 Project, select STM32CubeMX/STM32CubeIDE Project and click on Next as follows:

Next, select the folder that contains the .ioc file and click on Finish as follows:

Note: Project name is for reference only.
5. Firmware Development:
We start by creating new source and header file with name of max7219.c and max7219.h respectively.
Open max7219.h header file.
Within the header file, include the main.h and spi.h as follows:
#include "main.h" #include "spi.h"
Next, define the CS pin, number of LED matrixes and the SPI handler as follows:
#define MAX7219_NUM_MATRICES 4 #define MAX7219_CS_PORT GPIOA #define MAX7219_CS_PIN GPIO_PIN_0 /* If you want a different SPI handle, redefine here or override in build */ #ifndef MAX7219_SPI_HANDLE #define MAX7219_SPI_HANDLE hspi1 #endif
Next, declare the registers of MAX7219 chip:
#define MAX7219_REG_NOOP 0x00 #define MAX7219_REG_DIGIT0 0x01 /* row 0 ... rows 1..8 use 0x01..0x08 */ #define MAX7219_REG_DECODE 0x09 #define MAX7219_REG_INTENSITY 0x0A #define MAX7219_REG_SCANLIMIT 0x0B #define MAX7219_REG_SHUTDOWN 0x0C #define MAX7219_REG_DISPLAYTEST 0x0F
Next, declare the following functions:
/* Bring up the whole daisy-chain. Call once after HAL/SPI init. */ void MAX7219_Init(void); /* Broadcast a single register write to every chip in the chain. */ void MAX7219_WriteAll(uint8_t reg, uint8_t data); /* Clear all matrices (all LEDs off). */ void MAX7219_Clear(void); /* Set global brightness, 0 (min) .. 15 (max). Broadcast to all chips. */ void MAX7219_SetIntensity(uint8_t intensity); /* Send a raw 16-bit command to a single chip in the chain. * index 0 = nearest chip (DIN side), index N-1 = farthest. * NOTE: this sends No-Ops for the other chips, so it is slow; for * animation-style updates use MAX7219_WriteFrame(). */ void MAX7219_SetRow(uint8_t matrixIndex, uint8_t row, uint8_t data); /* Fastest way to refresh everything: one CS-low burst containing * 16-bit writes for every row of every matrix. frame points to * [MAX7219_NUM_MATRICES][8] bytes, row 0 = top row. * Byte bit 7 = leftmost LED of that row. */ void MAX7219_WriteFrame(const uint8_t frame[MAX7219_NUM_MATRICES][8]); /* Internal helper exposed for convenience: send one 16-bit * (reg,data) pair with CS driven low/high around it. */ void MAX7219_Send(uint8_t reg, uint8_t data);
Hence, the header file as follows:
#ifndef MAX7219_H
#define MAX7219_H
#ifdef __cplusplus
extern "C" {
#endif
#include "main.h"
#include "spi.h"
/* ------------------------------------------------------------------ */
/* User configuration */
/* ------------------------------------------------------------------ */
#define MAX7219_NUM_MATRICES 4
#define MAX7219_CS_PORT GPIOA
#define MAX7219_CS_PIN GPIO_PIN_0
/* If you want a different SPI handle, redefine here or override in build */
#ifndef MAX7219_SPI_HANDLE
#define MAX7219_SPI_HANDLE hspi1
#endif
/* ------------------------------------------------------------------ */
/* MAX7219 register addresses */
/* ------------------------------------------------------------------ */
#define MAX7219_REG_NOOP 0x00
#define MAX7219_REG_DIGIT0 0x01 /* row 0 ... rows 1..8 use 0x01..0x08 */
#define MAX7219_REG_DECODE 0x09
#define MAX7219_REG_INTENSITY 0x0A
#define MAX7219_REG_SCANLIMIT 0x0B
#define MAX7219_REG_SHUTDOWN 0x0C
#define MAX7219_REG_DISPLAYTEST 0x0F
/* ------------------------------------------------------------------ */
/* Public API */
/* ------------------------------------------------------------------ */
/* Bring up the whole daisy-chain. Call once after HAL/SPI init. */
void MAX7219_Init(void);
/* Broadcast a single register write to every chip in the chain. */
void MAX7219_WriteAll(uint8_t reg, uint8_t data);
/* Clear all matrices (all LEDs off). */
void MAX7219_Clear(void);
/* Set global brightness, 0 (min) .. 15 (max). Broadcast to all chips. */
void MAX7219_SetIntensity(uint8_t intensity);
/* Send a raw 16-bit command to a single chip in the chain.
* index 0 = nearest chip (DIN side), index N-1 = farthest.
* NOTE: this sends No-Ops for the other chips, so it is slow; for
* animation-style updates use MAX7219_WriteFrame(). */
void MAX7219_SetRow(uint8_t matrixIndex, uint8_t row, uint8_t data);
/* Fastest way to refresh everything: one CS-low burst containing
* 16-bit writes for every row of every matrix. frame points to
* [MAX7219_NUM_MATRICES][8] bytes, row 0 = top row.
* Byte bit 7 = leftmost LED of that row. */
void MAX7219_WriteFrame(const uint8_t frame[MAX7219_NUM_MATRICES][8]);
/* Internal helper exposed for convenience: send one 16-bit
* (reg,data) pair with CS driven low/high around it. */
void MAX7219_Send(uint8_t reg, uint8_t data);
#ifdef __cplusplus
}
#endif
#endif /* MAX7219_H */Next, open max7219.c source file.
We start by including the max7219.h header file as follows:
#include "max7219.h"
Next, define a function that will wait until the SPI has finished transfer the data as follows:
static inline void max7219_wait_idle(void)
{
while (__HAL_SPI_GET_FLAG(&MAX7219_SPI_HANDLE, SPI_FLAG_BSY) != RESET) {
/* spin */
}
}Next, a function to send 16-bit data over SPI:
/* Send 16 bits as (reg, data) with CS already low. */
static inline void max7219_send_pair(uint8_t reg, uint8_t data)
{
HAL_SPI_Transmit(&MAX7219_SPI_HANDLE, ®, 1, HAL_MAX_DELAY);
HAL_SPI_Transmit(&MAX7219_SPI_HANDLE, &data, 1, HAL_MAX_DELAY);
}Next, functions to write to MAX7219:
void MAX7219_Send(uint8_t reg, uint8_t data)
{
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_RESET);
max7219_send_pair(reg, data);
max7219_wait_idle();
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_SET);
}
void MAX7219_WriteAll(uint8_t reg, uint8_t data)
{
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_RESET);
for (uint8_t i = 0; i < MAX7219_NUM_MATRICES; i++) {
max7219_send_pair(reg, data);
}
max7219_wait_idle();
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_SET);
}Now, the init function:
void MAX7219_Init(void)
{
/* Idle CS high */
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_SET);
/* Leave test mode */
MAX7219_WriteAll(MAX7219_REG_DISPLAYTEST, 0x00);
/* No BCD decoding - we want raw bitmaps */
MAX7219_WriteAll(MAX7219_REG_DECODE, 0x00);
/* All 8 rows active */
MAX7219_WriteAll(MAX7219_REG_SCANLIMIT, 0x07);
/* Medium brightness */
MAX7219_WriteAll(MAX7219_REG_INTENSITY, 0x08);
/* Normal operation (out of shutdown) */
MAX7219_WriteAll(MAX7219_REG_SHUTDOWN, 0x01);
/* Blank everything */
MAX7219_Clear();
}Clear display function:
void MAX7219_Clear(void)
{
for (uint8_t row = MAX7219_REG_DIGIT0;
row <= MAX7219_REG_DIGIT0 + 7; row++) {
MAX7219_WriteAll(row, 0x00);
}
}Set Intensity function:
void MAX7219_SetIntensity(uint8_t intensity)
{
if (intensity > 0x0F) intensity = 0x0F;
MAX7219_WriteAll(MAX7219_REG_INTENSITY, intensity);
}Set raw function:
void MAX7219_SetRow(uint8_t matrixIndex, uint8_t row, uint8_t data)
{
if (matrixIndex >= MAX7219_NUM_MATRICES) return;
if (row < MAX7219_REG_DIGIT0 || row > MAX7219_REG_DIGIT0 + 7) return;
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_RESET);
/* First bits shifted out travel farthest. Loop from the chip
* with the highest index down to 0. Only the target index gets
* the real write; everyone else gets a No-Op. */
for (int i = MAX7219_NUM_MATRICES - 1; i >= 0; i--) {
if (i == (int)matrixIndex) {
max7219_send_pair(row, data);
} else {
max7219_send_pair(MAX7219_REG_NOOP, 0x00);
}
}
max7219_wait_idle();
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_SET);
}Write frame function:
void MAX7219_WriteFrame(const uint8_t frame[MAX7219_NUM_MATRICES][8])
{
for (uint8_t row = 0; row < 8; row++) {
uint8_t reg = MAX7219_REG_DIGIT0 + row;
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_RESET);
/* Farthest chip's data first, nearest (M0) last */
for (int m = MAX7219_NUM_MATRICES - 1; m >= 0; m--) {
max7219_send_pair(reg, frame[m][row]);
}
max7219_wait_idle();
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_SET); /* latch this row */
}
}Hence, the source file as follows:
#include "max7219.h"
/* ------------------------------------------------------------------ */
/* Low-level helpers */
/* ------------------------------------------------------------------ */
/* Wait until SPI has finished shifting out the last bit.
* Very important before raising CS, otherwise MAX7219 latches
* a partial frame. */
static inline void max7219_wait_idle(void)
{
while (__HAL_SPI_GET_FLAG(&MAX7219_SPI_HANDLE, SPI_FLAG_BSY) != RESET) {
/* spin */
}
}
/* Send 16 bits as (reg, data) with CS already low. */
static inline void max7219_send_pair(uint8_t reg, uint8_t data)
{
HAL_SPI_Transmit(&MAX7219_SPI_HANDLE, ®, 1, HAL_MAX_DELAY);
HAL_SPI_Transmit(&MAX7219_SPI_HANDLE, &data, 1, HAL_MAX_DELAY);
}
/* ------------------------------------------------------------------ */
/* Public API */
/* ------------------------------------------------------------------ */
void MAX7219_Send(uint8_t reg, uint8_t data)
{
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_RESET);
max7219_send_pair(reg, data);
max7219_wait_idle();
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_SET);
}
void MAX7219_WriteAll(uint8_t reg, uint8_t data)
{
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_RESET);
for (uint8_t i = 0; i < MAX7219_NUM_MATRICES; i++) {
max7219_send_pair(reg, data);
}
max7219_wait_idle();
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_SET);
}
void MAX7219_Init(void)
{
/* Idle CS high */
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_SET);
/* Leave test mode */
MAX7219_WriteAll(MAX7219_REG_DISPLAYTEST, 0x00);
/* No BCD decoding - we want raw bitmaps */
MAX7219_WriteAll(MAX7219_REG_DECODE, 0x00);
/* All 8 rows active */
MAX7219_WriteAll(MAX7219_REG_SCANLIMIT, 0x07);
/* Medium brightness */
MAX7219_WriteAll(MAX7219_REG_INTENSITY, 0x08);
/* Normal operation (out of shutdown) */
MAX7219_WriteAll(MAX7219_REG_SHUTDOWN, 0x01);
/* Blank everything */
MAX7219_Clear();
}
void MAX7219_Clear(void)
{
for (uint8_t row = MAX7219_REG_DIGIT0;
row <= MAX7219_REG_DIGIT0 + 7; row++) {
MAX7219_WriteAll(row, 0x00);
}
}
void MAX7219_SetIntensity(uint8_t intensity)
{
if (intensity > 0x0F) intensity = 0x0F;
MAX7219_WriteAll(MAX7219_REG_INTENSITY, intensity);
}
void MAX7219_SetRow(uint8_t matrixIndex, uint8_t row, uint8_t data)
{
if (matrixIndex >= MAX7219_NUM_MATRICES) return;
if (row < MAX7219_REG_DIGIT0 || row > MAX7219_REG_DIGIT0 + 7) return;
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_RESET);
/* First bits shifted out travel farthest. Loop from the chip
* with the highest index down to 0. Only the target index gets
* the real write; everyone else gets a No-Op. */
for (int i = MAX7219_NUM_MATRICES - 1; i >= 0; i--) {
if (i == (int)matrixIndex) {
max7219_send_pair(row, data);
} else {
max7219_send_pair(MAX7219_REG_NOOP, 0x00);
}
}
max7219_wait_idle();
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_SET);
}
void MAX7219_WriteFrame(const uint8_t frame[MAX7219_NUM_MATRICES][8])
{
for (uint8_t row = 0; row < 8; row++) {
uint8_t reg = MAX7219_REG_DIGIT0 + row;
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_RESET);
/* Farthest chip's data first, nearest (M0) last */
for (int m = MAX7219_NUM_MATRICES - 1; m >= 0; m--) {
max7219_send_pair(reg, frame[m][row]);
}
max7219_wait_idle();
HAL_GPIO_WritePin(MAX7219_CS_PORT, MAX7219_CS_PIN, GPIO_PIN_SET); /* latch this row */
}
}
Next, open main.c file
In user code begin includes, include the following header files:
#include "max7219.h" #include <string.h>
Next part is for the animation, you may skip it to draw your own animation etc.
In user code begin PTD, declare the following:
typedef void (*AnimFn)(uint32_t t, uint8_t first);
In user code begin PD:
#define FRAME_MS 40u /* 25 fps */ #define ANIM_DURATION_MS 8000u /* how long each animation runs */ #define DISP_W (MAX7219_NUM_MATRICES * 8) /* 32 */ #define DISP_H 8 /* Orientation - set these to the same values that worked in scroll.c */ #define DISP_REVERSE_MODULES 1 /* 1 = matrix 0 (DIN side) is on the right */ #define DISP_MIRROR_X 0 /* 1 = bit 0 is the leftmost pixel
In user code begin PV:
static uint8_t frame[MAX7219_NUM_MATRICES][8]; static uint32_t rng_state = 0x1234ABCDu;
Next, in user code begin PFP:
static void anim_bounce(uint32_t t, uint8_t first); static void anim_wave (uint32_t t, uint8_t first); static void anim_rain (uint32_t t, uint8_t first); static void anim_ripple(uint32_t t, uint8_t first); static void anim_life (uint32_t t, uint8_t first);
Next, in user code begin 0:
/* ---------------- helpers ---------------- */
static uint32_t rnd(void) /* xorshift32 */
{
uint32_t x = rng_state;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return rng_state = x;
}
static void fb_clear(void)
{
memset(frame, 0, sizeof frame);
}
/* x: 0..31 left->right, y: 0..7 top->bottom */
static void px(int x, int y, uint8_t on)
{
if (x < 0 || x >= DISP_W || y < 0 || y >= DISP_H) return;
int m = x / 8;
int c = x % 8;
#if DISP_REVERSE_MODULES
m = MAX7219_NUM_MATRICES - 1 - m;
#endif
#if DISP_MIRROR_X
uint8_t mask = (uint8_t)(1u << c);
#else
uint8_t mask = (uint8_t)(0x80u >> c);
#endif
if (on) frame[m][y] |= mask;
else frame[m][y] &= (uint8_t)~mask;
}
static void rect(int x0, int y0, int x1, int y1)
{
for (int x = x0; x <= x1; x++) { px(x, y0, 1); px(x, y1, 1); }
for (int y = y0; y <= y1; y++) { px(x0, y, 1); px(x1, y, 1); }
}
/* ---------------- 1. bouncing 2x2 ball ---------------- */
static void anim_bounce(uint32_t t, uint8_t first)
{
static int x, y, dx, dy;
(void)t;
if (first) { x = 0; y = 0; dx = 1; dy = 1; }
x += dx;
y += dy;
if (x <= 0 || x >= DISP_W - 2) dx = -dx;
if (y <= 0 || y >= DISP_H - 2) dy = -dy;
px(x, y, 1); px(x + 1, y, 1);
px(x, y + 1, 1); px(x + 1, y + 1, 1);
}
/* ---------------- 2. two travelling sine waves ---------------- */
static void anim_wave(uint32_t t, uint8_t first)
{
/* round(3.5 + 3.5*sin(2*pi*k/32)), 32 steps per period */
static const uint8_t sinTab[32] = {
3,4,4,5,5,6,6,6,7,6,6,6,5,5,4,4,
3,2,2,1,1,0,0,0,0,0,0,0,1,1,2,2
};
(void)first;
for (int x = 0; x < DISP_W; x++) {
px(x, sinTab[(x + t) & 31u], 1); /* slow wave, moves left */
px(x, 7 - sinTab[(2u * x - t) & 31u], 1); /* fast wave, moves right */
}
}
/* ---------------- 3. rain ---------------- */
static void anim_rain(uint32_t t, uint8_t first)
{
static int8_t drop[DISP_W]; /* -1 = no drop in this column */
if (first) memset(drop, -1, sizeof drop);
if ((t & 1u) == 0) { /* update every 2nd frame */
for (int x = 0; x < DISP_W; x++) {
if (drop[x] >= 0) {
if (++drop[x] > DISP_H) drop[x] = -1;
} else if ((rnd() & 15u) == 0) {
drop[x] = 0;
}
}
}
for (int x = 0; x < DISP_W; x++) {
if (drop[x] >= 0) {
px(x, drop[x], 1); /* head */
px(x, drop[x] - 1, 1); /* tail */
}
}
}
/* ---------------- 4. ripple of rectangles from the centre ---------------- */
static void anim_ripple(uint32_t t, uint8_t first)
{
(void)first;
uint32_t step = (t / 4u) % 6u; /* 0 1 2 3 2 1 ... */
int k = (step <= 3u) ? (int)step : (int)(6u - step);
int w = 2 + 4 * k;
rect(16 - w, 3 - k, 15 + w, 4 + k);
if (k >= 2) { /* inner echo */
int k2 = k - 2, w2 = 2 + 4 * k2;
rect(16 - w2, 3 - k2, 15 + w2, 4 + k2);
}
}
/* ---------------- 5. Conway's Game of Life (wrapping edges) ---------------- */
static void life_seed(uint32_t g[DISP_H])
{
for (int y = 0; y < DISP_H; y++) g[y] = rnd() & rnd(); /* ~25% alive */
}
static void anim_life(uint32_t t, uint8_t first)
{
static uint32_t g[DISP_H]; /* bit x of g[y] = cell (x,y) */
static uint16_t gen;
if (first || gen > 100u) {
life_seed(g);
gen = 0;
} else if (t % 3u == 0) { /* one generation every 120 ms */
uint32_t n[DISP_H];
for (int y = 0; y < DISP_H; y++) {
n[y] = 0;
for (int x = 0; x < DISP_W; x++) {
int cnt = 0;
for (int dy = -1; dy <= 1; dy++) {
int yy = (y + dy + DISP_H) % DISP_H;
for (int dx = -1; dx <= 1; dx++) {
if (dx == 0 && dy == 0) continue;
int xx = (x + dx + DISP_W) % DISP_W;
cnt += (int)((g[yy] >> xx) & 1u);
}
}
uint32_t alive = (g[y] >> x) & 1u;
if (cnt == 3 || (alive && cnt == 2)) n[y] |= (1u << x);
}
}
if (memcmp(n, g, sizeof n) == 0) { /* static or dead -> reseed */
life_seed(g);
gen = 0;
} else {
memcpy(g, n, sizeof n);
gen++;
}
}
for (int y = 0; y < DISP_H; y++)
for (int x = 0; x < DISP_W; x++)
if ((g[y] >> x) & 1u) px(x, y, 1);
}
/* ---------------- animation table ---------------- */
static const AnimFn animations[] = {
anim_bounce,
anim_wave,
anim_rain,
anim_ripple,
anim_life,
};
#define NUM_ANIMS (sizeof animations / sizeof animations[0])
These are functions for the animation.
In user code begin 2 in main function:
HAL_Delay(100); /* let the modules power up */ MAX7219_Init(); MAX7219_SetIntensity(4); uint8_t anim = 0; uint8_t first = 1; uint32_t t = 0; uint32_t animStart = HAL_GetTick();
Finally, in user code begin 3 in while 1 loop:
uint32_t frameStart = HAL_GetTick();
fb_clear();
animations[anim](t, first);
MAX7219_WriteFrame(frame);
first = 0;
t++;
if (HAL_GetTick() - animStart >= ANIM_DURATION_MS) {
anim = (uint8_t)((anim + 1u) % NUM_ANIMS);
first = 1;
t = 0;
animStart = HAL_GetTick();
}
while (HAL_GetTick() - frameStart < FRAME_MS) {
/* hold a steady frame rate */
}Thats all for the firmware.
Save, build the project and run it as follows:

You may download the project from our github repository from here.
5. Results:
You should get the following when you flash your stm32 with same demo:
Thats all.
Happy coding 😉


Discussion