All projects
08Live
8x8 LED Matrix
Draw patterns and icons over SPI with the MAX7219 driver.
SPI
01
What you learn
SPI communication: instead of wiring 64 LEDs, you stream 16-bit words to a driver chip that does the multiplexing.
02
Parts you need
03
Wiring
| Module | Board | Note |
|---|---|---|
| VCC | VBUS (5V) | 5V for bright LEDs |
| GND | GND | — |
| DIN | PB6 | SPI data |
| CS | PB5 | Chip select (LOAD) |
| CLK | PB4 | SPI clock |
04
Code
main.c
1#include <stdint.h>2#include <stdbool.h>3#include "inc/hw_memmap.h"4#include "driverlib/sysctl.h"5#include "driverlib/gpio.h"6#include "driverlib/pin_map.h"7#include "driverlib/ssi.h"8 9// MAX7219 register addresses10#define REG_DECODE 0x0911#define REG_INTENSITY 0x0A12#define REG_SCANLIMIT 0x0B13#define REG_SHUTDOWN 0x0C14#define REG_DISPTEST 0x0F15 16static void delayMs(uint32_t ms) { SysCtlDelay((SysCtlClockGet() / 3000) * ms); }17 18// Send one 16-bit word (address<<8 | data) to the MAX721919static void maxWrite(uint8_t addr, uint8_t data) {20 SSIDataPut(SSI2_BASE, ((uint16_t)addr << 8) | data);21 while (SSIBusy(SSI2_BASE)) {}22}23 24// Push an 8-row frame (rows[0] = top row)25static void showFrame(const uint8_t rows[8]) {26 uint8_t i;27 for (i = 0; i < 8; i++) maxWrite(i + 1, rows[i]); // digit regs 1..828}29 30static const uint8_t HEART[8] = { 0x00, 0x66, 0xFF, 0xFF, 0xFF, 0x7E, 0x3C, 0x18 };31static const uint8_t SMILE[8] = { 0x3C, 0x42, 0xA5, 0x81, 0xA5, 0x99, 0x42, 0x3C };32static const uint8_t ARROW[8] = { 0x18, 0x3C, 0x7E, 0xFF, 0x18, 0x18, 0x18, 0x18 };33static const uint8_t CHECK[8] = { 0x00, 0x01, 0x02, 0x04, 0x88, 0x50, 0x20, 0x00 };34 35int main(void) {36 SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL |37 SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);38 39 SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI2);40 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);41 while (!SysCtlPeripheralReady(SYSCTL_PERIPH_SSI2)) {}42 while (!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB)) {}43 44 GPIOPinConfigure(GPIO_PB4_SSI2CLK); // PB4=CLK, PB5=CS, PB6=DIN45 GPIOPinConfigure(GPIO_PB5_SSI2FSS);46 GPIOPinConfigure(GPIO_PB6_SSI2TX);47 GPIOPinTypeSSI(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6);48 49 SSIConfigSetExpClk(SSI2_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0,50 SSI_MODE_MASTER, 1000000, 16); // mode 0, master, 1 MHz, 16-bit51 SSIEnable(SSI2_BASE);52 53 maxWrite(REG_DISPTEST, 0x00); // display test off54 maxWrite(REG_DECODE, 0x00); // no BCD decode -> raw pixels55 maxWrite(REG_SCANLIMIT, 0x07); // all 8 rows56 maxWrite(REG_INTENSITY, 0x06); // brightness 0..1557 maxWrite(REG_SHUTDOWN, 0x01); // wake up58 59 while (1) {60 showFrame(HEART); delayMs(900);61 showFrame(SMILE); delayMs(900);62 showFrame(ARROW); delayMs(900);63 showFrame(CHECK); delayMs(900);64 }65}05
Output
The simulation below runs in your browser — it mimics the real board.
- reg
- 0x01..0x08
- bus
- SPI 16-bit