zxyy15717 发表于 2014-1-8 23:48:01

MAX7219控制8*8点阵,求大神指导!

本人新手,最近刚刚开始上手arduino,在一本教材上看到有一个max7219控制8*8点阵实现滚动消息的教程。

连接图如下:


代码如下:
#include <avr/pgmspace.h>
#include <TimerOne.h>
int DataPin = 2; // Pin 1 on MAX
int LoadPin = 3; // Pin 12 on MAX
int ClockPin = 4; // Pin 13 on MAX
byte buffer;

static byte font[] PROGMEM = {
// The printable ASCII characters only (32-126)
{B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000},
{B00000100, B00000100, B00000100, B00000100, B00000100, B00000100, B00000000, B00000100},
{B00001010, B00001010, B00001010, B00000000, B00000000, B00000000, B00000000, B00000000},
{B00000000, B00001010, B00011111, B00001010, B00011111, B00001010, B00011111, B00001010},
{B00000111, B00001100, B00010100, B00001100, B00000110, B00000101, B00000110, B00011100},
{B00011001, B00011010, B00000010, B00000100, B00000100, B00001000, B00001011, B00010011},
{B00000110, B00001010, B00010010, B00010100, B00001001, B00010110, B00010110, B00001001},
{B00000100, B00000100, B00000100, B00000000, B00000000, B00000000, B00000000, B00000000},
{B00000010, B00000100, B00001000, B00001000, B00001000, B00001000, B00000100, B00000010},
{B00001000, B00000100, B00000010, B00000010, B00000010, B00000010, B00000100, B00001000},
{B00010101, B00001110, B00011111, B00001110, B00010101, B00000000, B00000000, B00000000},
{B00000000, B00000000, B00000100, B00000100, B00011111, B00000100, B00000100, B00000000},
{B00000000, B00000000, B00000000, B00000000, B00000000, B00000110, B00000100, B00001000},
{B00000000, B00000000, B00000000, B00000000, B00001110, B00000000, B00000000, B00000000},
{B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000100},
{B00000001, B00000010, B00000010, B00000100, B00000100, B00001000, B00001000, B00010000},
{B00001110, B00010001, B00010011, B00010001, B00010101, B00010001, B00011001, B00001110},
{B00000100, B00001100, B00010100, B00000100, B00000100, B00000100, B00000100, B00011111},
{B00001110, B00010001, B00010001, B00000010, B00000100, B00001000, B00010000, B00011111},
{B00001110, B00010001, B00000001, B00001110, B00000001, B00000001, B00010001, B00001110},
{B00010000, B00010000, B00010100, B00010100, B00011111, B00000100, B00000100, B00000100},
{B00011111, B00010000, B00010000, B00011110, B00000001, B00000001, B00000001, B00011110},
{B00000111, B00001000, B00010000, B00011110, B00010001, B00010001, B00010001, B00001110},
{B00011111, B00000001, B00000001, B00000001, B00000010, B00000100, B00001000, B00010000},
{B00001110, B00010001, B00010001, B00001110, B00010001, B00010001, B00010001, B00001110},
{B00001110, B00010001, B00010001, B00001111, B00000001, B00000001, B00000001, B00000001},
{B00000000, B00000100, B00000100, B00000000, B00000000, B00000100, B00000100, B00000000},
{B00000000, B00000100, B00000100, B00000000, B00000000, B00000100, B00000100, B00001000},
{B00000001, B00000010, B00000100, B00001000, B00001000, B00000100, B00000010, B00000001},
{B00000000, B00000000, B00000000, B00011110, B00000000, B00011110, B00000000, B00000000},
{B00010000, B00001000, B00000100, B00000010, B00000010, B00000100, B00001000, B00010000},
{B00001110, B00010001, B00010001, B00000010, B00000100, B00000100, B00000000, B00000100},
{B00001110, B00010001, B00010001, B00010101, B00010101, B00010001, B00010001, B00011110},
{B00001110, B00010001, B00010001, B00010001, B00011111, B00010001, B00010001, B00010001},
{B00011110, B00010001, B00010001, B00011110, B00010001, B00010001, B00010001, B00011110},
{B00000111, B00001000, B00010000, B00010000, B00010000, B00010000, B00001000, B00000111},
{B00011100, B00010010, B00010001, B00010001, B00010001, B00010001, B00010010, B00011100},
{B00011111, B00010000, B00010000, B00011110, B00010000, B00010000, B00010000, B00011111},
{B00011111, B00010000, B00010000, B00011110, B00010000, B00010000, B00010000, B00010000},
{B00001110, B00010001, B00010000, B00010000, B00010111, B00010001, B00010001, B00001110},
{B00010001, B00010001, B00010001, B00011111, B00010001, B00010001, B00010001, B00010001},
{B00011111, B00000100, B00000100, B00000100, B00000100, B00000100, B00000100, B00011111},
{B00011111, B00000100, B00000100, B00000100, B00000100, B00000100, B00010100, B00001000},
{B00010001, B00010010, B00010100, B00011000, B00010100, B00010010, B00010001, B00010001},
{B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B00011111},
{B00010001, B00011011, B00011111, B00010101, B00010001, B00010001, B00010001, B00010001},
{B00010001, B00011001, B00011001, B00010101, B00010101, B00010011, B00010011, B00010001},
{B00011110, B00010001, B00010001, B00011110, B00010000, B00010000, B00010000, B00010000},
{B00001110, B00010001, B00010001, B00010001, B00010001, B00010101, B00010011, B00001111},
{B00011110, B00010001, B00010001, B00011110, B00010100, B00010010, B00010001, B00010001},
{B00001110, B00010001, B00010000, B00001000, B00000110, B00000001, B00010001, B00001110},
{B00011111, B00000100, B00000100, B00000100, B00000100, B00000100, B00000100, B00000100},
{B00010001, B00010001, B00010001, B00010001, B00010001, B00010001, B00010001, B00001110},
{B00010001, B00010001, B00010001, B00010001, B00010001, B00010001, B00001010, B00000100},
{B00010001, B00010001, B00010001, B00010001, B00010001, B00010101, B00010101, B00001010},
{B00010001, B00010001, B00001010, B00000100, B00000100, B00001010, B00010001, B00010001},
{B00010001, B00010001, B00001010, B00000100, B00000100, B00000100, B00000100, B00000100},
{B00011111, B00000001, B00000010, B00000100, B00001000, B00010000, B00010000, B00011111},
{B00001110, B00001000, B00001000, B00001000, B00001000, B00001000, B00001000, B00001110},
{B00010000, B00001000, B00001000, B00000100, B00000100, B00000010, B00000010, B00000001},
{B00001110, B00000010, B00000010, B00000010, B00000010, B00000010, B00000010, B00001110},
{B00000100, B00001010, B00010001, B00000000, B00000000, B00000000, B00000000, B00000000},
{B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00011111},
{B00001000, B00000100, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000},
{B00000000, B00000000, B00000000, B00001110, B00010010, B00010010, B00010010, B00001111},
{B00000000, B00010000, B00010000, B00010000, B00011100, B00010010, B00010010, B00011100},
{B00000000, B00000000, B00000000, B00001110, B00010000, B00010000, B00010000, B00001110},
{B00000000, B00000001, B00000001, B00000001, B00000111, B00001001, B00001001, B00000111},
{B00000000, B00000000, B00000000, B00011100, B00010010, B00011110, B00010000, B00001110},
{B00000000, B00000011, B00000100, B00000100, B00000110, B00000100, B00000100, B00000100},
{B00000000, B00001110, B00001010, B00001010, B00001110, B00000010, B00000010, B00001100},
{B00000000, B00010000, B00010000, B00010000, B00011100, B00010010, B00010010, B00010010},
{B00000000, B00000000, B00000100, B00000000, B00000100, B00000100, B00000100, B00000100},
{B00000000, B00000010, B00000000, B00000010, B00000010, B00000010, B00000010, B00001100},
{B00000000, B00010000, B00010000, B00010100, B00011000, B00011000, B00010100, B00010000},
{B00000000, B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B00001100},
{B00000000, B00000000, B00000000, B00001010, B00010101, B00010001, B00010001, B00010001},
{B00000000, B00000000, B00000000, B00010100, B00011010, B00010010, B00010010, B00010010},
{B00000000, B00000000, B00000000, B00001100, B00010010, B00010010, B00010010, B00001100},
{B00000000, B00011100, B00010010, B00010010, B00011100, B00010000, B00010000, B00010000},
{B00000000, B00001110, B00010010, B00010010, B00001110, B00000010, B00000010, B00000001},
{B00000000, B00000000, B00000000, B00001010, B00001100, B00001000, B00001000, B00001000},
{B00000000, B00000000, B00001110, B00010000, B00001000, B00000100, B00000010, B00011110},
{B00000000, B00010000, B00010000, B00011100, B00010000, B00010000, B00010000, B00001100},
{B00000000, B00000000, B00000000, B00010010, B00010010, B00010010, B00010010, B00001100},
{B00000000, B00000000, B00000000, B00010001, B00010001, B00010001, B00001010, B00000100},
{B00000000, B00000000, B00000000, B00010001, B00010001, B00010001, B00010101, B00001010},
{B00000000, B00000000, B00000000, B00010001, B00001010, B00000100, B00001010, B00010001},
{B00000000, B00000000, B00010001, B00001010, B00000100, B00001000, B00001000, B00010000},
{B00000000, B00000000, B00000000, B00011111, B00000010, B00000100, B00001000, B00011111},
{B00000010, B00000100, B00000100, B00000100, B00001000, B00000100, B00000100, B00000010},
{B00000100, B00000100, B00000100, B00000100, B00000100, B00000100, B00000100, B00000100},
{B00001000, B00000100, B00000100, B00000100, B00000010, B00000100, B00000100, B00001000},
{B00000000, B00000000, B00000000, B00001010, B00011110, B00010100, B00000000, B00000000}
};

void clearDisplay() {
for (byte x=0; x<8; x++) {
buffer = B00000000;
}
screenUpdate();
}
void initMAX7219() {
pinMode(DataPin, OUTPUT);
pinMode(LoadPin, OUTPUT);
pinMode(ClockPin, OUTPUT);
clearDisplay();
writeData(B00001011, B00000111); // scan limit set to 0:7
writeData(B00001001, B00000000); // decode mode off
writeData(B00001100, B00000001); // Set shutdown register to normal operation
writeData(B00001010, B00000001); // Values 0 to 15 only (4 bit)
}


void writeData(byte MSB, byte LSB) {
byte mask;
digitalWrite(LoadPin, LOW); // set loadpin ready to receive data
// Send out MSB
for (mask = B10000000; mask>0; mask>>=1) { //iterate through bit mask
digitalWrite(ClockPin, LOW);
if (MSB & mask){ // if bitwise AND resolves to true
digitalWrite(DataPin,HIGH); // send 1
}
else{ //if bitwise and resolves to false
digitalWrite(DataPin,LOW); // send 0
}
digitalWrite(ClockPin, HIGH); // clock high, data gets input
}
// send out LSB for data
for (mask = B10000000; mask>0; mask>>=1) { //iterate through bit mask
digitalWrite(ClockPin, LOW);
if (LSB & mask){ // if bitwise AND resolves to true
digitalWrite(DataPin,HIGH); // send 1
}
else{ //if bitwise and resolves to false
digitalWrite(DataPin,LOW); // send 0
}
digitalWrite(ClockPin, HIGH); // clock high, data gets input
}
digitalWrite(LoadPin, HIGH); // latch the data
digitalWrite(ClockPin, LOW);
}

void scroll(char myString[], int spd) {
byte firstChrRow, secondChrRow;
byte ledOutput;
byte chrPointer = 0; // Initialise the string position pointer
byte Char1, Char2; // the two characters that will be displayed
byte scrollBit = 0;
byte strLength = 0;
unsigned long time;
unsigned long counter;
// Increment count till we reach the string
while (myString) {strLength++;}
counter = millis();
while (chrPointer < (strLength-1)) {
time = millis();
if (time > (counter + spd)) {
Char1 = myString;
Char2 = myString;
for (byte y= 0; y<8; y++) {
firstChrRow = pgm_read_byte(&font);
secondChrRow = (pgm_read_byte(&font)) << 1;
ledOutput = (firstChrRow << scrollBit) | (secondChrRow >>(8 - scrollBit) );
buffer = ledOutput;

}
scrollBit++;
if (scrollBit > 6) {
scrollBit = 0;
chrPointer++;
}
counter = millis();
}
}
}
void screenUpdate() {
for (byte row = 0; row < 8; row++) {
writeData(row+1, buffer);
}
}
void setup() {
initMAX7219();
Timer1.initialize(10000); // initialize timer1 and set interrupt period
Timer1.attachInterrupt(screenUpdate);
}
void loop() {
clearDisplay();
scroll("ABCDEFG ", 45);
}


上传之后发现整个点阵全部亮了,对着datasheet把连线和代码检查了半天也觉得没什么问题,于是乎就卡在这里了。跪求各路大神指点迷津
















zoologist 发表于 2014-1-9 09:40:59

1.建议直接用 arduino拉点阵的几个脚,确定点阵是OK的
2.读取max7219的寄存器看看,个人感觉有可能是你使用的时序除了问题

仅供参考........

zxyy15717 发表于 2014-1-9 12:53:28

zoologist 发表于 2014-1-9 09:40 static/image/common/back.gif
1.建议直接用 arduino拉点阵的几个脚,确定点阵是OK的
2.读取max7219的寄存器看看,个人感觉有可能是你使用 ...

点阵没问题,我一个一个试了。

奇怪的是我后来简化了代码,没有用TimerOne库函数,而且先只寻址到7219的DIG0,连的是点阵上的第一列(点阵是共阴极),然后只送了第一列的电平高低进去,比如01100110,结果第一列还是全部亮。

而且更奇怪的是我在整个点阵全亮的时候去调scan limit和intensity,发现根本没有效果,实在是觉得有点诡异。。。。

zxyy15717 发表于 2014-1-9 13:43:15

zoologist 发表于 2014-1-9 09:40 static/image/common/back.gif
1.建议直接用 arduino拉点阵的几个脚,确定点阵是OK的
2.读取max7219的寄存器看看,个人感觉有可能是你使用 ...

而且现在突然有点想不通代码,因为7219上dig0~dig7都是是连的共阴点阵上的各列,共阴点阵上的各行又都是接地,而且共阴点阵的阳极是接在一起的,那只要从dig送一个数进去肯定整列的反应都是一样的啊,比如1就是整列亮,0就是整列灭。送一个byte的数进去应该没法控制一列里面某些行亮某些行亮啊,因为它们的阳极都接在一起。

不知道我的思维哪里出了问题。。。。越想越把自己绕进去了

zoologist 发表于 2014-1-9 14:02:16

zxyy15717 发表于 2014-1-9 13:43 static/image/common/back.gif
而且现在突然有点想不通代码,因为7219上dig0~dig7都是是连的共阴点阵上的各列,共阴点阵上的各行又都是接 ...

建议你画一下电路图吧......没准画着画着就恍然大悟了~

或者就在纸上画,手机拍照传上来大家看看

zxyy15717 发表于 2014-1-9 20:45:45

zoologist 发表于 2014-1-9 14:02 static/image/common/back.gif
建议你画一下电路图吧......没准画着画着就恍然大悟了~

或者就在纸上画,手机拍照传上来大家看看

电路图就是传上来这个图片

糯米基 发表于 2014-1-10 21:50:00

zxyy15717 发表于 2014-1-9 13:43 static/image/common/back.gif
而且现在突然有点想不通代码,因为7219上dig0~dig7都是是连的共阴点阵上的各列,共阴点阵上的各行又都是接 ...

同学,我猜你看的书是《Arduino从基础到实践》,我最近也看到了那部分,不过我没有动手做,无论是数码管还是点阵都是利用了管脚复用,比如说4位一体的数码管,我们一般只能一次点亮一个数码管,但是如果加快点亮的速度,那么由于人的视觉暂留,(一般是1/25秒),我们所看到的就是4个数码管都亮了。。。希望能够帮到你理解

zxyy15717 发表于 2014-1-10 22:36:47

糯米基 发表于 2014-1-10 21:50 static/image/common/back.gif
同学,我猜你看的书是《Arduino从基础到实践》,我最近也看到了那部分,不过我没有动手做,无论是数码管还 ...

这个我知道,就是TimerOne库的作用嘛,但是我是64个管同时亮,但是我在送电平的时候是有些管高电平有些管低电平的。后来我把代码简化了,没有任何变化就固定显示一个大写的A,还是64个管全部亮了,所以这个让我有点不理解。

还有就是我看论坛里其他人写的7219的帖子,他们都没有用TimerOne库多路复用。而且他们给的接线图是dig0~dig7连共阴极点阵的行,各seg的引脚连的列。这么连的话岂不是什么反应都不会有??

zxyy15717 发表于 2014-1-11 12:22:04

糯米基 发表于 2014-1-10 21:50 static/image/common/back.gif
同学,我猜你看的书是《Arduino从基础到实践》,我最近也看到了那部分,不过我没有动手做,无论是数码管还 ...

不如你也用7219试着做一做,看看有什么问题?可以接着讨论~ :)

zxyy15717 发表于 2014-1-29 21:27:06

自顶,没有做过7219驱动点阵的朋友吗??

Jingcheng_Li 发表于 2014-2-14 09:55:41

我也在做和你一样的东西,不过我用的代码是ledcontrol的example,也出了和你一样的问题,不过我发现点阵上其实是有变暗的,只是和亮处的区别太不明显了,几乎看不出来,但我检查了连线又没发现问题出现在哪。然后我用了几种代码也都是这样,不清楚为什么。

zoologist 发表于 2014-2-14 10:07:49

Jingcheng_Li 发表于 2014-2-14 09:55 static/image/common/back.gif
我也在做和你一样的东西,不过我用的代码是ledcontrol的example,也出了和你一样的问题,不过我发现点阵上其 ...

万用表测测,确保连接稳固(面包板吧?)

另外,接线一定要正确,不要搞反了

zxyy15717 发表于 2014-2-16 15:21:51

Jingcheng_Li 发表于 2014-2-14 09:55 static/image/common/back.gif
我也在做和你一样的东西,不过我用的代码是ledcontrol的example,也出了和你一样的问题,不过我发现点阵上其 ...

没有啊~~我后来就直接跳过那个project,先去做后面的东西了~~而且最后我发现了一个更诡异的事, 当我一行一行的接上试的时候,我发现经常是接到第四第五行的时候点阵会突然闪一下然后全灭。

我后来也是用的ledcontrol库编的东西,实在是被折腾得烦了,就先直接跳过了~以后再回过头接着折腾

eddiewwm 发表于 2014-2-16 16:35:39

點陣的接法有點特別,尤其看到MAX7219的 5,6等DIG腳,竟要分隔兩岸,就很有興趣這點陣的型號和接腳規格,能提供嗎?

Jingcheng_Li 发表于 2014-2-16 18:10:33

后来我发现是我行列接反了...现在我已经好了
页: [1] 2
查看完整版本: MAX7219控制8*8点阵,求大神指导!