Arduino与Proteus仿真实例-MAX7219级联驱动多个8x8LED点阵仿真

MAX7219级联驱动多个8x8LED点阵仿真

MAX7219/MAX7221是紧凑型串行输入/输出共阴极显示驱动器,可将微处理器(μPs)连接到多达8位的7段数字LED显示器、条形图显示器或64个独立LED。

在前面的文章中已经对7段数码管和MAX7219做了详细的介绍,请参考:

1、仿真电路原理图

在这里插入图片描述

2、仿真代码实现

1)Max7219 Led点阵驱动实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// MaxMatrix.h
/\*
\* MaxMatrix
\* Version 1.0 Feb 2013
\* Copyright 2013 Oscar Kin-Chung Au
\*/

#ifndef \_MaxMatrix\_H\_
#define \_MaxMatrix\_H\_

#include "Arduino.h"

#define max7219\_reg\_noop 0x00
#define max7219\_reg\_digit0 0x01
#define max7219\_reg\_digit1 0x02
#define max7219\_reg\_digit2 0x03
#define max7219\_reg\_digit3 0x04
#define max7219\_reg\_digit4 0x05
#define max7219\_reg\_digit5 0x06
#define max7219\_reg\_digit6 0x07
#define max7219\_reg\_digit7 0x08
#define max7219\_reg\_decodeMode 0x09
#define max7219\_reg\_intensity 0x0a
#define max7219\_reg\_scanLimit 0x0b
#define max7219\_reg\_shutdown 0x0c
#define max7219\_reg\_displayTest 0x0f

class MaxMatrix
{
private:
byte data;
byte load;
byte clock;
byte num;
byte buffer[80];

void reload();

public:
MaxMatrix(byte data, byte load, byte clock, byte num);

void init();
void clear();
void setCommand(byte command, byte value);
void setIntensity(byte intensity);
void setColumn(byte col, byte value);
void setColumnAll(byte col, byte value);
void setDot(byte col, byte row, byte value);
void writeSprite(int x, int y, const byte\* sprite);

void shiftLeft(bool rotate = false, bool fill_zero = true);
void shiftRight(bool rotate = false, bool fill_zero = true);
void shiftUp(bool rotate = false);
void shiftDown(bool rotate = false);
};

#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// MatMatrix.cpp
/\*
\* MaxMatrix
\* Version 1.0 Feb 2013
\* Copyright 2013 Oscar Kin-Chung Au
\*/


#include "Arduino.h"
#include "MaxMatrix.h"

MaxMatrix::MaxMatrix(byte _data, byte _load, byte _clock, byte _num)
{
data = _data;
load = _load;
clock = _clock;
num = _num;
for (int i=0; i<80; i++)
buffer[i] = 0;
}

void MaxMatrix::init()
{
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(load, OUTPUT);
digitalWrite(clock, HIGH);

setCommand(max7219_reg_scanLimit, 0x07);
setCommand(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
setCommand(max7219_reg_shutdown, 0x01); // not in shutdown mode
setCommand(max7219_reg_displayTest, 0x00); // no display test

// empty registers, turn all LEDs off
clear();

setIntensity(0x0f); // the first 0x0f is the value you can set
}

void MaxMatrix::setIntensity(byte intensity)
{
setCommand(max7219_reg_intensity, intensity);
}

void MaxMatrix::clear()
{
for (int i=0; i<8; i++)
setColumnAll(i,0);

for (int i=0; i<80; i++)
buffer[i] = 0;
}

void MaxMatrix::setCommand(byte command, byte value)
{
digitalWrite(load, LOW);
for (int i=0; i<num; i++)
{
shiftOut(data, clock, MSBFIRST, command);
shiftOut(data, clock, MSBFIRST, value);
}
digitalWrite(load, LOW);
digitalWrite(load, HIGH);
}


void MaxMatrix::setColumn(byte col, byte value)
{
int n = col / 8;
int c = col % 8;
digitalWrite(load, LOW);
for (int i=0; i<num; i++)
{
if (i == n)
{
shiftOut(data, clock, MSBFIRST, c + 1);
shiftOut(data, clock, MSBFIRST, value);
}
else
{
shiftOut(data, clock, MSBFIRST, 0);
shiftOut(data, clock, MSBFIRST, 0);
}
}
digitalWrite(load, LOW);
digitalWrite(load, HIGH);

buffer[col] = value;
}

void MaxMatrix::setColumnAll(byte col, byte value)
{
digitalWrite(load, LOW);
for (int i=0; i<num; i++)
{
shiftOut(data, clock, MSBFIRST, col + 1);
shiftOut(data, clock, MSBFIRST, value);
buffer[col \* i] = value;
}
digitalWrite(load, LOW);
digitalWrite(load, HIGH);
}

void MaxMatrix::setDot(byte col, byte row, byte value)
{
bitWrite(buffer[col], row, value);

int n = col / 8;
int c = col % 8;
digitalWrite(load, LOW);
for (int i=0; i<num; i++)
{
if (i == n)
{
shiftOut(data, clock, MSBFIRST, c + 1);
shiftOut(data, clock, MSBFIRST, buffer[col]);
}
else
{
shiftOut(data, clock, MSBFIRST, 0);
shiftOut(data, clock, MSBFIRST, 0);
}
}
digitalWrite(load, LOW);
digitalWrite(load, HIGH);
}

void MaxMatrix::writeSprite(int x, int y, const byte\* sprite)
{
int w = sprite[0];
int h = sprite[1];

if (h == 8 && y == 0)
for (int i=0; i<w; i++)
{
int c = x + i;
if (c>=0 && c<80)
setColumn(c, sprite[i+2]);
}
else
for (int i=0; i<w; i++)
for (int j=0; j<h; j++)
{
int c = x + i;
int r = y + j;
if (c>=0 && c<80 && r>=0 && r<8)
setDot(c, r, bitRead(sprite[i+2], j));
}
}

void MaxMatrix::reload()
{
for (int i=0; i<8; i++)
{
int col = i;
digitalWrite(load, LOW);
for (int j=0; j<num; j++)
{
shiftOut(data, clock, MSBFIRST, i + 1);
shiftOut(data, clock, MSBFIRST, buffer[col]);
col += 8;
}
digitalWrite(load, LOW);
digitalWrite(load, HIGH);
}
}

void MaxMatrix::shiftLeft(bool rotate, bool fill_zero)
{
byte old = buffer[0];
int i;
for (i=0; i<80; i++)
buffer[i] = buffer[i+1];
if (rotate) buffer[num\*8-1] = old;
else if (fill_zero) buffer[num\*8-1] = 0;

reload();
}

void MaxMatrix::shiftRight(bool rotate, bool fill_zero)
{
int last = num\*8-1;
byte old = buffer[last];
int i;
for (i=79; i>0; i--)
buffer[i] = buffer[i-1];
if (rotate) buffer[0] = old;
else if (fill_zero) buffer[0] = 0;

reload();
}

void MaxMatrix::shiftUp(bool rotate)
{
for (int i=0; i<num\*8; i++)
{
bool b = buffer[i] & 1;
buffer[i] >>= 1;
if (rotate) bitWrite(buffer[i], 7, b);
}
reload();
}

void MaxMatrix::shiftDown(bool rotate)
{
for (int i=0; i<num\*8; i++)
{
bool b = buffer[i] & 128;
buffer[i] <<= 1;
if (rotate) bitWrite(buffer[i], 0, b);
}
reload();
}

2)主程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "Arduino.h"
#include "MaxMatrix.h"

MaxMatrix::MaxMatrix(byte _data, byte _load, byte _clock, byte _num)
{
data = _data;
load = _load;
clock = _clock;
num = _num;
for (int i=0; i<80; i++)
buffer[i] = 0;
}

void MaxMatrix::init()
{
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(load, OUTPUT);
digitalWrite(clock, HIGH);

setCommand(max7219_reg_scanLimit, 0x07);
setCommand(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
setCommand(max7219_reg_shutdown, 0x01); // not in shutdown mode
setCommand(max7219_reg_displayTest, 0x00); // no display test

// empty registers, turn all LEDs off
clear();

setIntensity(0x0f); // the first 0x0f is the value you can set
}

void MaxMatrix::setIntensity(byte intensity)
{
setCommand(max7219_reg_intensity, intensity);
}

void MaxMatrix::clear()
{
for (int i=0; i<8; i++)
setColumnAll(i,0);

for (int i=0; i<80; i++)
buffer[i] = 0;
}

void MaxMatrix::setCommand(byte command, byte value)
{
digitalWrite(load, LOW);
for (int i=0; i<num; i++)
{
shiftOut(data, clock, MSBFIRST, command);
shiftOut(data, clock, MSBFIRST, value);
}
digitalWrite(load, LOW);
digitalWrite(load, HIGH);
}


void MaxMatrix::setColumn(byte col, byte value)
{
int n = col / 8;
int c = col % 8;
digitalWrite(load, LOW);
for (int i=0; i<num; i++)
{
if (i == n)
{
shiftOut(data, clock, MSBFIRST, c + 1);
shiftOut(data, clock, MSBFIRST, value);
}
else
{
shiftOut(data, clock, MSBFIRST, 0);
shiftOut(data, clock, MSBFIRST, 0);
}
}
digitalWrite(load, LOW);
digitalWrite(load, HIGH);

buffer[col] = value;
}

void MaxMatrix::setColumnAll(byte col, byte value)
{
digitalWrite(load, LOW);
for (int i=0; i<num; i++)
{
shiftOut(data, clock, MSBFIRST, col + 1);
shiftOut(data, clock, MSBFIRST, value);
buffer[col \* i] = value;
}
digitalWrite(load, LOW);
digitalWrite(load, HIGH);
}

void MaxMatrix::setDot(byte col, byte row, byte value)
{
bitWrite(buffer[col], row, value);

int n = col / 8;
int c = col % 8;
digitalWrite(load, LOW);
for (int i=0; i<num; i++)
{
if (i == n)
{
shiftOut(data, clock, MSBFIRST, c + 1);
shiftOut(data, clock, MSBFIRST, buffer[col]);
}
else
{
shiftOut(data, clock, MSBFIRST, 0);
shiftOut(data, clock, MSBFIRST, 0);
}
}
digitalWrite(load, LOW);
digitalWrite(load, HIGH);
}

void MaxMatrix::writeSprite(int x, int y, const byte\* sprite)
{
int w = sprite[0];
int h = sprite[1];

if (h == 8 && y == 0)
for (int i=0; i<w; i++)
{
int c = x + i;
if (c>=0 && c<80)
setColumn(c, sprite[i+2]);
}
else
for (int i=0; i<w; i++)
for (int j=0; j<h; j++)
{
int c = x + i;
int r = y + j;
if (c>=0 && c<80 && r>=0 && r<8)
setDot(c, r, bitRead(sprite[i+2], j));
}
}

void MaxMatrix::reload()
{
for (int i=0; i<8; i++)
{
int col = i;
digitalWrite(load, LOW);
for (int j=0; j<num; j++)
{
shiftOut(data, clock, MSBFIRST, i + 1);
shiftOut(data, clock, MSBFIRST, buffer[col]);
col += 8;
}
digitalWrite(load, LOW);
digitalWrite(load, HIGH);
}
}

void MaxMatrix::shiftLeft(bool rotate, bool fill_zero)
{
byte old = buffer[0];
int i;
for (i=0; i<80; i++)
buffer[i] = buffer[i+1];
if (rotate) buffer[num\*8-1] = old;
else if (fill_zero) buffer[num\*8-1] = 0;

reload();
}

void MaxMatrix::shiftRight(bool rotate, bool fill_zero)
{
int last = num\*8-1;
byte old = buffer[last];
int i;
for (i=79; i>0; i--)
buffer[i] = buffer[i-1];
if (rotate) buffer[0] = old;
else if (fill_zero) buffer[0] = 0;

reload();
}

void MaxMatrix::shiftUp(bool rotate)
{
for (int i=0; i<num\*8; i++)
{
bool b = buffer[i] & 1;
buffer[i] >>= 1;
if (rotate) bitWrite(buffer[i], 7, b);
}
reload();
}

void MaxMatrix::shiftDown(bool rotate)
{
for (int i=0; i<num\*8; i++)
{
bool b = buffer[i] & 128;
buffer[i] <<= 1;
if (rotate) bitWrite(buffer[i], 0, b);
}
reload();
}

3、仿真结果

在这里插入图片描述

文章来源: https://iotsmart.blog.csdn.net/article/details/120945813