级联下的传值方式
级联下, N个MAX7219相当于组成了一个8*N bit宽度的锁存器, 如果需要对第M个7219进行写入, 需要做M次寻址+写入后拉高CS, 才能到达这个7219. 如果仅仅对这个7219进行操作, 不操作其他7219, 那么在第1个寻址写入后, 做的寻址写入输入的都是空值.
对于级联的MAX7219, 如果要显示产生滚动的效果, 那么每次要操作的都是整行, 可以将其看成是一个(N8)8的点阵, 每次直接对一行写入N个byte.
连线
- CLK => P2.2
- CS => P2.1
- DIN => P2.0
- VCC => 5V
- GND => GND
代码示例
#include <reg52.h>
#include <intrins.h>
#define u8 unsigned char
#define u16 unsigned int
#define DECODE_MODE 0x09
#define INTENSITY 0x0A
#define SCAN_LIMIT 0x0B
#define SHUT_DOWN 0x0C
#define DISPLAY_TEST 0x0F
#define BLOCKS 4
sbit MAX7219_CLK = P2^2;
sbit MAX7219_CS = P2^1;
sbit MAX7219_DIN = P2^0;
u8 code bytes[] = {
0x3e,0x63,0x63,0x7f,0x63,0x63,0x63,0x63, //A
0x7e,0x63,0x63,0x7e,0x63,0x63,0x63,0x7e, //B
0x3e,0x63,0x63,0x60,0x60,0x63,0x63,0x3e, //C
0x3e,0x63,0x73,0x6b,0x67,0x63,0x63,0x3e, //0
0x0c,0x1c,0x3c,0x0c,0x0c,0x0c,0x0c,0x3f, //1
0x3e,0x63,0x63,0x06,0x0c,0x18,0x30,0x7f, //2
0x3e,0x63,0x63,0x0e,0x03,0x63,0x63,0x3e, //3
0x06,0x0e,0x1e,0x36,0x66,0x7f,0x06,0x06, //4
0x7f,0x60,0x60,0x7e,0x03,0x03,0x03,0x7e, //5
0x3e,0x63,0x60,0x7e,0x63,0x63,0x63,0x3e, //6
0x7f,0x03,0x03,0x06,0x0c,0x18,0x18,0x18, //7
0x3e,0x63,0x63,0x3e,0x63,0x63,0x63,0x3e, //8
0x3e,0x63,0x63,0x63,0x3f,0x03,0x63,0x3e, //9
};
u8 val[BLOCKS];
u8 character_len = sizeof(bytes) / 8;
void delay(u16 x)
{
u16 i,j;
for(i = 0; i < x; i++)
for(j = 0;j < 112; j++);
}
void Max7219_writeByte(u8 dat)
{
u8 i;
MAX7219_CS = 0;
for(i = 8; i >= 1; i--)
{
MAX7219_CLK = 0;
MAX7219_DIN = dat & 0x80; // &10000000, 取最高位
dat = dat << 1;
MAX7219_CLK = 1;
}
}
void Max7219_singeWrite(u8 index, u8 addr, u8 dat)
{
MAX7219_CS = 0;
Max7219_writeByte(addr);
Max7219_writeByte(dat);
while(index--)
{
Max7219_writeByte(0x00);
Max7219_writeByte(0x00);
}
MAX7219_CS = 1;
}
void Max7219_multiWrite(u8 addr, u8 len, u8* dat)
{
MAX7219_CS = 0;
while(len--)
{
Max7219_writeByte(addr);
Max7219_writeByte(*dat++);
}
MAX7219_CS = 1;
}
void Max7219_init(void)
{
u8 i;
for (i = 0; i < BLOCKS; i++)
{
Max7219_singeWrite(i, SHUT_DOWN, 0x01); // 0x00:shutdown, 0x01:normal
Max7219_singeWrite(i, DECODE_MODE, 0x00); // No decode
Max7219_singeWrite(i, INTENSITY, 0x03); // 0x00:min, 0x0F:max
Max7219_singeWrite(i, SCAN_LIMIT, 0x07); // Display 8 digits
Max7219_singeWrite(i, DISPLAY_TEST, 0x00); // 0x00:normal, 0x01:test mode
}
}
void main(void)
{
// pos:点阵右沿对应的val数组元素编号. 因为要无缝滚屏, 用右沿做求余不会产生跳变
// lpos: 点阵左沿对应的val数组元素编号, 每次根据点阵右沿和点阵宽度计算得到
u16 pos = 0, lpos = 0;
// cpos: 点阵左沿对应的文字编号
// bpos: 在这个文字中, 当前移动到第几个bit, 值从0-7
u8 i, j, cpos = 0, bpos = 0, tcpos = 0;
Max7219_init();
while(1)
{
lpos = pos + sizeof(bytes) - BLOCKS * 8;
cpos = lpos / 8; // 第几个字
bpos = lpos % 8; // 字的第几个bit
for (i = 0; i < 8; i++) // 对每一行, 对val的每个元素赋值
{
// 从第cpos个字的bpos位开始, 填满每个点阵对应的byte
for (j = 0; j < BLOCKS; j++)
{
// 高位部分
tcpos = (cpos + j) % character_len;
val[j] = bytes[tcpos * 8 + i] << bpos;
// 低位部分
tcpos = (cpos + j + 1) % character_len;
val[j] |= bytes[tcpos * 8 + i] >> (8 - bpos);
}
Max7219_multiWrite(i+1, BLOCKS, val);
}
// 每处理完一屏, pos右移一位
pos = (pos + 1) % sizeof(bytes);
delay(50);
}
}
参考
- 一个好用的8x8 LED编辑器 https://xantorohara.github.io/led-matrix-editor