Raspberry Pi written on the MAX7219 character driver

1. Meet the MAX7219

The first thing to get the MAX7219-driven LED matrix is ​​to connect and try to display the pattern. In addition to the GND and VCC, the MAX7219 requires only three additional pins to illuminate the matrix. Among them, DIN pin input data, CS (LOAD) pin control data input, CLK pin is used to distinguish each bit.

Raspberry Pi written on the MAX7219 character driver

The entire write flow of MAX is that the CS pin is first set to 0, indicating that writes are allowed. Then, 16 bits are sequentially written from the upper order. Each bit is written in such a way that DIN is first set to the bit value to be written, and then CLK generates a falling edge (the rising edge in the figure, I don't know why there is a difference) is read. The last CS pin is set to indicate the end of the write.

The timing diagram is as follows:

Raspberry Pi written on the MAX7219 character driver

Before running, an initialization is required, the behavior of which is to write a specific value to a few specific addresses. At least two addresses need to be written, the first one is 0x0b, and the 0x07 write indicates that all rows are scanned. The second is 0x0c, and writing 1 indicates entering the working mode.

Then each line on the dot matrix has its address. For example, the first line is 0x01 and the eighth line is 0x08. Each time an 8-bit binary number is written to the fixed line address, the pattern can be displayed on the specified line.

2. Raspberry Party GPIO Access - Virtual File System Access

Linux can access GPIO by accessing some files under sys/class/gpio and reading and writing these files.

! /bin/bash

# DIN, CS, GPIO port location of CLK

DIN=4

CS=3

CLK=2

# some file path

GPIO_BASE=/sys/class/gpio

GPIO_EXPORT=${GPIO_BASE}/export

GPIO_UNEXPORT=${GPIO_BASE}/unexport

BIN=(00000001 00000010 00000011 00000100 00000101 00000110 00000111 00001000)

# Generate the folder location of the specified GPIO pin

funcTIon GPIO(){

Echo ${GPIO_BASE}/gpio$1

}

# Export a pin to user mode

funcTIon GPIO_export(){

If [ -d `GPIO $1` ]; then

Echo GPIO pin $1 found in folder.

Else

Echo $1 》 ${GPIO_EXPORT}

Fi

}

# unexport a pin

funcTIon GPIO_unexport(){

If [ -d `GPIO $1` ]; then

Echo $1 》 ${GPIO_UNEXPORT}

Else

Echo GPIO pin $1 not found.

Fi

}

# Change the direction of a pin

funcTIon GPIO_direction(){

Echo $2 》 `GPIO $1`/direction

}

#Change the value of a pin

Function GPIO_set(){

Echo $2 》 `GPIO $1`/value

}

#Change the value of DIN

Function GPIO_DIN(){

GPIO_set $DIN $1

}

#Change the value of CS

Function GPIO_CS(){

GPIO_set $CS $1

}

#Change the value of CLK

Function GPIO_CLK(){

GPIO_set $CLK $1

}

# Send a byte value to MAX7219

Function Matrix_send_char(){

Local i=1

For ((i=1;i"=8;i++)); do

Chr=`expr substr $1 $i 1`

GPIO_DIN $chr

GPIO_CLK 1

GPIO_CLK 0

Done

}

# Send a complete signal to the MAX7219

Function Matrix_send_word(){

GPIO_CS 1

GPIO_CS 0

GPIO_CLK 0

Matrix_send_char $1

Matrix_send_char $2

GPIO_CS 1

}

# Initialize GPIO pins

Function GPIO_init(){

GPIO_export $DIN

GPIO_export $CS

GPIO_export $CLK

Sleep 2

GPIO_direction $DIN out

GPIO_direction $CS out

GPIO_direction $CLK out

}

# Clear GPIO pins

Function GPIO_clear(){

GPIO_unexport $DIN

GPIO_unexport $CS

GPIO_unexport $CLK

}

# Display data on the dot matrix

Function Matrix_render(){

Local i=1

For ((i=0;i"8;i++)); do

Echo $i $1

Matrix_send_word ${BIN[$i]} $1

Shift

Done

}

# Display using the data in the file

Function Matrix_render_file(){

Local tmp=(`cat $1`)

Matrix_render "${tmp[@]}"

}

# Use a pattern to clear the screen

Function Matrix_clear(){

Local STR=(

00000000

01100110

11111111

11111111

11111111

01111110

00111100

00011000

)

Matrix_render "${STR[@]}"

}

# Initialize the lattice

Function Matrix_init(){

#编码模式

Matrix_send_word 00001001 00000000

#亮

Matrix_send_word 00001010 00000011

#Scan number of digital tubes

Matrix_send_word 00001011 00000111

# Operating mode

Matrix_send_word 00001100 00000001

# Clear screen to display the default pattern after initialization

Matrix_clear

}

In the terminal:

Source matrix.sh

GPIO_init

Matrix_init

The effect is as follows:

Raspberry Pi written on the MAX7219 character driver

Pin Insulator

Pin insulator,Silicone rubber insulator,Composite insulator,Post insulator,Power fittings

TAIZHOU HUADONG INSULATED MATERIAL CO.,LTD , https://www.thim-insulator.com

This entry was posted in on