X  
登录

还没有账号?立即注册

忘记密码?
登陆
X  
注册

已经有账号?马上登陆

获取验证码
重新获取(60s)
立即注册
统计
  • 建站日期:2021-03-10
  • 文章总数:68 篇
  • 评论总数:76 条
  • 分类总数:13 个
  • 最后更新:11月1日
文章 个人制作

使用 MAX7219和Arduino板控制 8×8 LED矩阵显示屏详细教程及代码下载

海盗船长
首页 个人制作 正文
广告

在本 Arduino 教程中,我们将学习如何使用 MAX7219 驱动程序和 Arduino 板控制 8×8 LED 矩阵。

MAX7219

现在让我们仔细看看 MAX7219 驱动器。该 IC 能够驱动 64 个单独的 LED,同时仅使用 3 条线与 Arduino 进行通信,而且我们可以链式排线连接多个驱动器和矩阵,并且仍然使用相同的 3 条线。

使用MAX7219和Arduino板控制8×8LED矩阵显示屏详细教程及代码下载
-电子爱好者
-第1
张图片
64 个 LED 由 IC 的 16 个输出引脚驱动。现在的问题是这怎么可能。那么同时点亮的最大 LED 数量实际上是八个。LED 排列为 8×8 组行和列。因此 MAX7219 在很短的时间内激活每一列,同时它还驱动每一行。因此,通过快速切换列和行,人眼只会注意到连续的光。


使用MAX7219和Arduino板控制8×8LED矩阵显示屏详细教程及代码下载
-电子爱好者
-第2
张图片

请注意普通 8×8 LED 矩阵的引脚如何在内部排列,因此如果您自己构建矩阵,则应考虑它。

另请注意,MAX7219 的通用分线板在 5V 和 IC 引脚编号 18 之间带有一个电阻。该电阻用于设置亮度或流向 LED 的电流。


使用MAX7219和Arduino板控制8×8LED矩阵显示屏详细教程及代码下载
-电子爱好者
-第3
张图片
IC数据表中的下表显示了我们应该根据LED的正向压降使用的电阻值。


使用MAX7219和Arduino板控制8×8LED矩阵显示屏详细教程及代码下载
-电子爱好者
-第4
张图片

电路原理图


现在让我们将 8×8 LED 矩阵模块连接到 Arduino 板。下面是电路原理图:


使用MAX7219和Arduino板控制8×8LED矩阵显示屏详细教程及代码下载
-电子爱好者
-第5
张图片
模块的 VCC 和 GND 连接到 Arduino 的 5V 和 GND 引脚,其他三个引脚 DIN、CLK 和 CS 连接到 Arduino 板的任何数字引脚。如果我们想连接多个模块,我们只需将前一个分线板的输出引脚连接到新模块的输入引脚。实际上这些引脚都是一样的,只是前一块板的 DOUT 引脚连接到新板的 DIN 引脚。

您可以从一些购买渠道链接获取本 Arduino 教程所需的组件:

8×8 LED MAX7219 点阵模块

HC-05 蓝牙模块 

Arduino 板

面包板和跳线 

基本 MAX7219 Arduino 代码

连接模块后,我们就可以查看第一个示例的 Arduino 代码了。我们将使用可从GitHub下载的 MaxMatrix 库。

 

/*
      8x8 LED Matrix MAX7219 Example 01
   by Dejan Nedelkovski, www.HowToMechatronics.com
   Based on the following library:
   GitHub | riyas-org/max7219  https://github.com/riyas-org/max7219
*/
#include <MaxMatrix.h>
int DIN = 7;   // DIN pin of MAX7219 module
int CLK = 6;   // CLK pin of MAX7219 module
int CS = 5;    // CS pin of MAX7219 module
int maxInUse = 1;
MaxMatrix m(DIN, CS, CLK, maxInUse); 
char A[] = {4, 8,
            B01111110,
            B00010001,
            B00010001,
            B01111110,
           };
char B[] = {4, 8,
            B01111111,
            B01001001,
            B01001001,
            B00110110,
           };
char smile01[] = {8, 8,
                  B00111100,
                  B01000010,
                  B10010101,
                  B10100001,
                  B10100001,
                  B10010101,
                  B01000010,
                  B00111100
                 };
char smile02[] = {8, 8,
                  B00111100,
                  B01000010,
                  B10010101,
                  B10010001,
                  B10010001,
                  B10010101,
                  B01000010,
                  B00111100
                 };
char smile03[] = {8, 8,
                  B00111100,
                  B01000010,
                  B10100101,
                  B10010001,
                  B10010001,
                  B10100101,
                  B01000010,
                  B00111100
                 };
void setup()
{
  m.init(); // MAX7219 initialization
  m.setIntensity(8); // initial led matrix intensity, 0-15
}
void loop() {
  // Seting the LEDs On or Off at x,y or row,column position
  m.setDot(6,2,true); 
  delay(1000);
  m.setDot(6,3,true);
  delay(1000);
  m.clear(); // Clears the display
  for (int i=0; i<8; i++){
    m.setDot(i,i,true);
    delay(300);
  }
  m.clear();
  // Displaying the character at x,y (upper left corner of the character)  
  m.writeSprite(2, 0, A);
  delay(1000);
  m.writeSprite(2, 0, B);
  delay(1000);
  m.writeSprite(0, 0, smile01);
  delay(1000);

m.writeSprite(0, 0, smile02);
delay(1000);

m.writeSprite(0, 0, smile03);
delay(1000);

for (int i=0; i<8; i++){
m.shiftLeft(false,false);
delay(300);
}
m.clear();
}

 
m.clear();
  // Displaying the character at x,y (upper left corner of the character)  
  m.writeSprite(2, 0, A);
  delay(1000);
  m.writeSprite(2, 0, B);
  delay(1000);
  m.writeSprite(0, 0, smile01);
  delay(1000);

m.writeSprite(0, 0, smile02);
delay(1000);

m.writeSprite(0, 0, smile03);
delay(1000);

for (int i=0; i<8; i++){
m.shiftLeft(false,false);
delay(300);
}
m.clear();
}

 
}

描述:所以首先我们需要包含 MaxMatrix.h 库,定义模块连接的引脚,设置我们使用的模块数量并定义 MaxMatrix 对象。

为了显示字符,我们需要在字符或字节数组中定义它们,这里我有几个例子。我们可以注意到这些位是如何形成实际上是零和一的字符的。在这种情况下,它们旋转了 90 度,但库示例建议以这种方式使用它们,以便以后更容易实现用于滚动文本的 shiftLeft 自定义函数。


使用MAX7219和Arduino板控制8×8LED矩阵显示屏详细教程及代码下载
-电子爱好者
-第6
张图片
在设置部分,我们只需要初始化模块并设置 LED 的亮度。在使用 setDot() 函数的循环部分中,我们可以将任何单个 LED 设置为在 X、Y 或行/列位置点亮,并使用 clear() 函数清除显示。


使用MAX7219和Arduino板控制8×8LED矩阵显示屏详细教程及代码下载
-电子爱好者
-第7
张图片
为了显示预定义的字符,我们使用 writeSprite() 函数,前两个参数是字符左上角的 X 和 Y 位置。最后使用 shiftLeft() 函数,我们将字符向左移动或滚动。

8×8 LED 矩阵滚动 Arduino 代码


接下来让我们看一下滚动文本示例,看看有什么不同。在代码下方,您将找到它的描述。

/*
      8x8 LED Matrix MAX7219 Scrolling Text Example
   Based on the following library:
   GitHub | riyas-org/max7219  https://github.com/riyas-org/max7219
*/
#include <MaxMatrix.h>
#include <avr/pgmspace.h>
PROGMEM const unsigned char CH[] = {
  3, 8, B00000000, B00000000, B00000000, B00000000, B00000000, // space
  1, 8, B01011111, B00000000, B00000000, B00000000, B00000000, // !
  3, 8, B00000011, B00000000, B00000011, B00000000, B00000000, // "
  5, 8, B00010100, B00111110, B00010100, B00111110, B00010100, // #
  4, 8, B00100100, B01101010, B00101011, B00010010, B00000000, // $
  5, 8, B01100011, B00010011, B00001000, B01100100, B01100011, // %
  5, 8, B00110110, B01001001, B01010110, B00100000, B01010000, // &
  1, 8, B00000011, B00000000, B00000000, B00000000, B00000000, // '
  3, 8, B00011100, B00100010, B01000001, B00000000, B00000000, // (
  3, 8, B01000001, B00100010, B00011100, B00000000, B00000000, // )
  5, 8, B00101000, B00011000, B00001110, B00011000, B00101000, // *
  5, 8, B00001000, B00001000, B00111110, B00001000, B00001000, // +
  2, 8, B10110000, B01110000, B00000000, B00000000, B00000000, // ,
  4, 8, B00001000, B00001000, B00001000, B00001000, B00000000, // -
  2, 8, B01100000, B01100000, B00000000, B00000000, B00000000, // .
  4, 8, B01100000, B00011000, B00000110, B00000001, B00000000, // /
  4, 8, B00111110, B01000001, B01000001, B00111110, B00000000, // 0
  3, 8, B01000010, B01111111, B01000000, B00000000, B00000000, // 1
  4, 8, B01100010, B01010001, B01001001, B01000110, B00000000, // 2
  4, 8, B00100010, B01000001, B01001001, B00110110, B00000000, // 3
  4, 8, B00011000, B00010100, B00010010, B01111111, B00000000, // 4
  4, 8, B00100111, B01000101, B01000101, B00111001, B00000000, // 5
  4, 8, B00111110, B01001001, B01001001, B00110000, B00000000, // 6
  4, 8, B01100001, B00010001, B00001001, B00000111, B00000000, // 7
  4, 8, B00110110, B01001001, B01001001, B00110110, B00000000, // 8
  4, 8, B00000110, B01001001, B01001001, B00111110, B00000000, // 9
  2, 8, B01010000, B00000000, B00000000, B00000000, B00000000, // :
  2, 8, B10000000, B01010000, B00000000, B00000000, B00000000, // ;
  3, 8, B00010000, B00101000, B01000100, B00000000, B00000000, // <
  3, 8, B00010100, B00010100, B00010100, B00000000, B00000000, // =
  3, 8, B01000100, B00101000, B00010000, B00000000, B00000000, // >
  4, 8, B00000010, B01011001, B00001001, B00000110, B00000000, // ?
  5, 8, B00111110, B01001001, B01010101, B01011101, B00001110, // @
  4, 8, B01111110, B00010001, B00010001, B01111110, B00000000, // A
  4, 8, B01111111, B01001001, B01001001, B00110110, B00000000, // B
  4, 8, B00111110, B01000001, B01000001, B00100010, B00000000, // C
  4, 8, B01111111, B01000001, B01000001, B00111110, B00000000, // D
  4, 8, B01111111, B01001001, B01001001, B01000001, B00000000, // E
  4, 8, B01111111, B00001001, B00001001, B00000001, B00000000, // F
  4, 8, B00111110, B01000001, B01001001, B01111010, B00000000, // G
  4, 8, B01111111, B00001000, B00001000, B01111111, B00000000, // H
  3, 8, B01000001, B01111111, B01000001, B00000000, B00000000, // I
  4, 8, B00110000, B01000000, B01000001, B00111111, B00000000, // J
  4, 8, B01111111, B00001000, B00010100, B01100011, B00000000, // K
  4, 8, B01111111, B01000000, B01000000, B01000000, B00000000, // L
  5, 8, B01111111, B00000010, B00001100, B00000010, B01111111, // M
  5, 8, B01111111, B00000100, B00001000, B00010000, B01111111, // N
  4, 8, B00111110, B01000001, B01000001, B00111110, B00000000, // O
  4, 8, B01111111, B00001001, B00001001, B00000110, B00000000, // P
  4, 8, B00111110, B01000001, B01000001, B10111110, B00000000, // Q
  4, 8, B01111111, B00001001, B00001001, B01110110, B00000000, // R
  4, 8, B01000110, B01001001, B01001001, B00110010, B00000000, // S
  5, 8, B00000001, B00000001, B01111111, B00000001, B00000001, // T
  4, 8, B00111111, B01000000, B01000000, B00111111, B00000000, // U
  5, 8, B00001111, B00110000, B01000000, B00110000, B00001111, // V
  5, 8, B00111111, B01000000, B00111000, B01000000, B00111111, // W
  5, 8, B01100011, B00010100, B00001000, B00010100, B01100011, // X
  5, 8, B00000111, B00001000, B01110000, B00001000, B00000111, // Y
  4, 8, B01100001, B01010001, B01001001, B01000111, B00000000, // Z
  2, 8, B01111111, B01000001, B00000000, B00000000, B00000000, // [
  4, 8, B00000001, B00000110, B00011000, B01100000, B00000000, //  backslash
  2, 8, B01000001, B01111111, B00000000, B00000000, B00000000, // ]
  3, 8, B00000010, B00000001, B00000010, B00000000, B00000000, // hat
  4, 8, B01000000, B01000000, B01000000, B01000000, B00000000, // _
  2, 8, B00000001, B00000010, B00000000, B00000000, B00000000, // `
  4, 8, B00100000, B01010100, B01010100, B01111000, B00000000, // a
  4, 8, B01111111, B01000100, B01000100, B00111000, B00000000, // b
  4, 8, B00111000, B01000100, B01000100, B00101000, B00000000, // c
  4, 8, B00111000, B01000100, B01000100, B01111111, B00000000, // d
  4, 8, B00111000, B01010100, B01010100, B00011000, B00000000, // e
  3, 8, B00000100, B01111110, B00000101, B00000000, B00000000, // f
  4, 8, B10011000, B10100100, B10100100, B01111000, B00000000, // g
  4, 8, B01111111, B00000100, B00000100, B01111000, B00000000, // h
  3, 8, B01000100, B01111101, B01000000, B00000000, B00000000, // i
  4, 8, B01000000, B10000000, B10000100, B01111101, B00000000, // j
  4, 8, B01111111, B00010000, B00101000, B01000100, B00000000, // k
  3, 8, B01000001, B01111111, B01000000, B00000000, B00000000, // l
  5, 8, B01111100, B00000100, B01111100, B00000100, B01111000, // m
  4, 8, B01111100, B00000100, B00000100, B01111000, B00000000, // n
  4, 8, B00111000, B01000100, B01000100, B00111000, B00000000, // o
  4, 8, B11111100, B00100100, B00100100, B00011000, B00000000, // p
  4, 8, B00011000, B00100100, B00100100, B11111100, B00000000, // q
  4, 8, B01111100, B00001000, B00000100, B00000100, B00000000, // r
  4, 8, B01001000, B01010100, B01010100, B00100100, B00000000, // s
  3, 8, B00000100, B00111111, B01000100, B00000000, B00000000, // t
  4, 8, B00111100, B01000000, B01000000, B01111100, B00000000, // u
  5, 8, B00011100, B00100000, B01000000, B00100000, B00011100, // v
  5, 8, B00111100, B01000000, B00111100, B01000000, B00111100, // w
  5, 8, B01000100, B00101000, B00010000, B00101000, B01000100, // x
  4, 8, B10011100, B10100000, B10100000, B01111100, B00000000, // y
  3, 8, B01100100, B01010100, B01001100, B00000000, B00000000, // z
  3, 8, B00001000, B00110110, B01000001, B00000000, B00000000, // {
  1, 8, B01111111, B00000000, B00000000, B00000000, B00000000, // |
  3, 8, B01000001, B00110110, B00001000, B00000000, B00000000, // }
  4, 8, B00001000, B00000100, B00001000, B00000100, B00000000, // ~
};
int DIN = 7;   // DIN pin of MAX7219 module
int CLK = 6;   // CLK pin of MAX7219 module
int CS = 5;    // CS pin of MAX7219 module
int maxInUse = 2;
MaxMatrix m(DIN, CS, CLK, maxInUse);
byte buffer[10];
char text[]= "HowToMechatronics.com  "; // Scrolling text
void setup() {
  m.init(); // module initialize
  m.setIntensity(15); // dot matix intensity 0-15
}
void loop() {
  printStringWithShift(text, 100); // (text, scrolling speed)
}
// Display=the extracted characters with scrolling
void printCharWithShift(char c, int shift_speed) {
  if (c < 32) return;
  c -= 32;
  memcpy_P(buffer, CH + 7 * c, 7);
  m.writeSprite(32, 0, buffer);
  m.setColumn(32 + buffer[0], 0);
  for (int i = 0; i < buffer[0] + 1; i++)
  {
    delay(shift_speed);
    m.shiftLeft(false, false);
  }
}
// Extract the characters from the text string
void printStringWithShift(char* s, int shift_speed) {
  while (*s != 0) {
    printCharWithShift(*s, shift_speed);
    s++;
  }
}


描述:这里我们必须为 PROGMEN 包含一个额外的库,它是变量修饰符,用于将数据存储在闪存而不是 SRAM 中。当我们有一个更大的静态变量数据库时,比如在这种情况下定义字母和字符,最好将它们存储在闪存中,因为它比 SRAM 的 2K 字节大得多,32K 字节。

使用MAX7219和Arduino板控制8×8LED矩阵显示屏详细教程及代码下载
-电子爱好者
-第8
张图片

接下来使用字符数组定义滚动文本,在循环部分中,自定义函数 printStringWithShift 以使用第二个参数定义的以毫秒为单位的滚动速度在 LED 矩阵上打印滚动文本。这个自定义函数做的第一件事是从文本字符串中提取字符,然后在 LED 矩阵上显示这些滚动字符。

 

用于通过蓝牙控制 8×8 LED 矩阵的 Android 应用程序


一旦我们了解了 MAX7219 的工作原理,现在我们可以制作第三个示例,这是一个实际的 Arduino 项目,我们将构建一个定制的 Android 应用程序,通过蓝牙通信来控制 LED 矩阵。在我们继续之前,我建议您查看我的详细教程,了解如何使用HC-05 蓝牙模块以及如何使用 MIT App Inventor 在线应用程序构建自定义Android 应用程序


使用MAX7219和Arduino板控制8×8LED矩阵显示屏详细教程及代码下载
-电子爱好者
-第9
张图片
这是 Arduino 代码,现在让我们看看与前一个示例相比的修改。

/*
      8x8 LED Matrix MAX7219 Scrolling Text
          Android Control via Bluetooth
   by Dejan Nedelkovski, www.HowToMechatronics.com
   Based on the following library:
   GitHub | riyas-org/max7219  https://github.com/riyas-org/max7219
*/
#include <MaxMatrix.h>
#include <SoftwareSerial.h>
#include <avr/pgmspace.h>
PROGMEM const unsigned char CH[] = {
  3, 8, B00000000, B00000000, B00000000, B00000000, B00000000, // space
  1, 8, B01011111, B00000000, B00000000, B00000000, B00000000, // !
  3, 8, B00000011, B00000000, B00000011, B00000000, B00000000, // "
  5, 8, B00010100, B00111110, B00010100, B00111110, B00010100, // #
  4, 8, B00100100, B01101010, B00101011, B00010010, B00000000, // $
  5, 8, B01100011, B00010011, B00001000, B01100100, B01100011, // %
  5, 8, B00110110, B01001001, B01010110, B00100000, B01010000, // &
  1, 8, B00000011, B00000000, B00000000, B00000000, B00000000, // '
  3, 8, B00011100, B00100010, B01000001, B00000000, B00000000, // (
  3, 8, B01000001, B00100010, B00011100, B00000000, B00000000, // )
  5, 8, B00101000, B00011000, B00001110, B00011000, B00101000, // *
  5, 8, B00001000, B00001000, B00111110, B00001000, B00001000, // +
  2, 8, B10110000, B01110000, B00000000, B00000000, B00000000, // ,
  4, 8, B00001000, B00001000, B00001000, B00001000, B00000000, // -
  2, 8, B01100000, B01100000, B00000000, B00000000, B00000000, // .
  4, 8, B01100000, B00011000, B00000110, B00000001, B00000000, // /
  4, 8, B00111110, B01000001, B01000001, B00111110, B00000000, // 0
  3, 8, B01000010, B01111111, B01000000, B00000000, B00000000, // 1
  4, 8, B01100010, B01010001, B01001001, B01000110, B00000000, // 2
  4, 8, B00100010, B01000001, B01001001, B00110110, B00000000, // 3
  4, 8, B00011000, B00010100, B00010010, B01111111, B00000000, // 4
  4, 8, B00100111, B01000101, B01000101, B00111001, B00000000, // 5
  4, 8, B00111110, B01001001, B01001001, B00110000, B00000000, // 6
  4, 8, B01100001, B00010001, B00001001, B00000111, B00000000, // 7
  4, 8, B00110110, B01001001, B01001001, B00110110, B00000000, // 8
  4, 8, B00000110, B01001001, B01001001, B00111110, B00000000, // 9
  2, 8, B01010000, B00000000, B00000000, B00000000, B00000000, // :
  2, 8, B10000000, B01010000, B00000000, B00000000, B00000000, // ;
  3, 8, B00010000, B00101000, B01000100, B00000000, B00000000, // <
  3, 8, B00010100, B00010100, B00010100, B00000000, B00000000, // =
  3, 8, B01000100, B00101000, B00010000, B00000000, B00000000, // >
  4, 8, B00000010, B01011001, B00001001, B00000110, B00000000, // ?
  5, 8, B00111110, B01001001, B01010101, B01011101, B00001110, // @
  4, 8, B01111110, B00010001, B00010001, B01111110, B00000000, // A
  4, 8, B01111111, B01001001, B01001001, B00110110, B00000000, // B
  4, 8, B00111110, B01000001, B01000001, B00100010, B00000000, // C
  4, 8, B01111111, B01000001, B01000001, B00111110, B00000000, // D
  4, 8, B01111111, B01001001, B01001001, B01000001, B00000000, // E
  4, 8, B01111111, B00001001, B00001001, B00000001, B00000000, // F
  4, 8, B00111110, B01000001, B01001001, B01111010, B00000000, // G
  4, 8, B01111111, B00001000, B00001000, B01111111, B00000000, // H
  3, 8, B01000001, B01111111, B01000001, B00000000, B00000000, // I
  4, 8, B00110000, B01000000, B01000001, B00111111, B00000000, // J
  4, 8, B01111111, B00001000, B00010100, B01100011, B00000000, // K
  4, 8, B01111111, B01000000, B01000000, B01000000, B00000000, // L
  5, 8, B01111111, B00000010, B00001100, B00000010, B01111111, // M
  5, 8, B01111111, B00000100, B00001000, B00010000, B01111111, // N
  4, 8, B00111110, B01000001, B01000001, B00111110, B00000000, // O
  4, 8, B01111111, B00001001, B00001001, B00000110, B00000000, // P
  4, 8, B00111110, B01000001, B01000001, B10111110, B00000000, // Q
  4, 8, B01111111, B00001001, B00001001, B01110110, B00000000, // R
  4, 8, B01000110, B01001001, B01001001, B00110010, B00000000, // S
  5, 8, B00000001, B00000001, B01111111, B00000001, B00000001, // T
  4, 8, B00111111, B01000000, B01000000, B00111111, B00000000, // U
  5, 8, B00001111, B00110000, B01000000, B00110000, B00001111, // V
  5, 8, B00111111, B01000000, B00111000, B01000000, B00111111, // W
  5, 8, B01100011, B00010100, B00001000, B00010100, B01100011, // X
  5, 8, B00000111, B00001000, B01110000, B00001000, B00000111, // Y
  4, 8, B01100001, B01010001, B01001001, B01000111, B00000000, // Z
  2, 8, B01111111, B01000001, B00000000, B00000000, B00000000, // [
  4, 8, B00000001, B00000110, B00011000, B01100000, B00000000, //  backslash
  2, 8, B01000001, B01111111, B00000000, B00000000, B00000000, // ]
  3, 8, B00000010, B00000001, B00000010, B00000000, B00000000, // hat
  4, 8, B01000000, B01000000, B01000000, B01000000, B00000000, // _
  2, 8, B00000001, B00000010, B00000000, B00000000, B00000000, // `
  4, 8, B00100000, B01010100, B01010100, B01111000, B00000000, // a
  4, 8, B01111111, B01000100, B01000100, B00111000, B00000000, // b
  4, 8, B00111000, B01000100, B01000100, B00101000, B00000000, // c
  4, 8, B00111000, B01000100, B01000100, B01111111, B00000000, // d
  4, 8, B00111000, B01010100, B01010100, B00011000, B00000000, // e
  3, 8, B00000100, B01111110, B00000101, B00000000, B00000000, // f
  4, 8, B10011000, B10100100, B10100100, B01111000, B00000000, // g
  4, 8, B01111111, B00000100, B00000100, B01111000, B00000000, // h
  3, 8, B01000100, B01111101, B01000000, B00000000, B00000000, // i
  4, 8, B01000000, B10000000, B10000100, B01111101, B00000000, // j
  4, 8, B01111111, B00010000, B00101000, B01000100, B00000000, // k
  3, 8, B01000001, B01111111, B01000000, B00000000, B00000000, // l
  5, 8, B01111100, B00000100, B01111100, B00000100, B01111000, // m
  4, 8, B01111100, B00000100, B00000100, B01111000, B00000000, // n
  4, 8, B00111000, B01000100, B01000100, B00111000, B00000000, // o
  4, 8, B11111100, B00100100, B00100100, B00011000, B00000000, // p
  4, 8, B00011000, B00100100, B00100100, B11111100, B00000000, // q
  4, 8, B01111100, B00001000, B00000100, B00000100, B00000000, // r
  4, 8, B01001000, B01010100, B01010100, B00100100, B00000000, // s
  3, 8, B00000100, B00111111, B01000100, B00000000, B00000000, // t
  4, 8, B00111100, B01000000, B01000000, B01111100, B00000000, // u
  5, 8, B00011100, B00100000, B01000000, B00100000, B00011100, // v
  5, 8, B00111100, B01000000, B00111100, B01000000, B00111100, // w
  5, 8, B01000100, B00101000, B00010000, B00101000, B01000100, // x
  4, 8, B10011100, B10100000, B10100000, B01111100, B00000000, // y
  3, 8, B01100100, B01010100, B01001100, B00000000, B00000000, // z
  3, 8, B00001000, B00110110, B01000001, B00000000, B00000000, // {
  1, 8, B01111111, B00000000, B00000000, B00000000, B00000000, // |
  3, 8, B01000001, B00110110, B00001000, B00000000, B00000000, // }
  4, 8, B00001000, B00000100, B00001000, B00000100, B00000000, // ~
};
int dIn = 7;   // DIN pin of MAX7219 module
int clk = 6;   // CLK pin of MAX7219 module
int cs = 5;    // CS pin of MAX7219 module
int maxInUse = 2;    // Number of MAX7219's connected
MaxMatrix m(dIn, cs, clk, maxInUse);
SoftwareSerial Bluetooth(8, 7); // Bluetooth
byte buffer[10];
char incomebyte;
int scrollSpeed = 100;
char text[100] = "HowToMechatronics.com  "; // Initial text message
int brightness = 15;
int count = 0;
char indicator;
void setup() {
  m.init(); // MAX7219 initialization
  m.setIntensity(brightness); // initial led matrix intensity, 0-15
  Bluetooth.begin(38400); // Default communication rate of the Bluetooth module
}
void loop() {
  // Printing the text
  printStringWithShift(text, scrollSpeed);

if (Bluetooth.available()) { // Checks whether data is comming from the serial port
indicator = Bluetooth.read(); // Starts reading the serial port, the first byte from the incoming data
// If we have pressed the "Send" button from the Android App, clear the previous text
if (indicator == '1') {
for (int i = 0; i < 100; i++) {
text[i] = 0;
m.clear();
}
// Read the whole data/string comming from the phone and put it into text[] array.
while (Bluetooth.available()) {
incomebyte = Bluetooth.read();
text[count] = incomebyte;
count++;
}
count = 0;
}
// Adjusting the Scrolling Speed
else if (indicator == '2') {
String sS = Bluetooth.readString();
scrollSpeed = 150 - sS.toInt(); // Milliseconds, subtraction because lower value means higher scrolling speed
}
// Adjusting the brightness
else if (indicator == '3') {
String sB = Bluetooth.readString();
brightness = sB.toInt();
m.setIntensity(brightness);
}
}
}
void printCharWithShift(char c, int shift_speed) {
if (c < 32) return;
c -= 32;
memcpy_P(buffer, CH + 7 * c, 7);
m.writeSprite(32, 0, buffer);
m.setColumn(32 + buffer[0], 0);
for (int i = 0; i < buffer[0] + 1; i++)
{
delay(shift_speed);
m.shiftLeft(false, false);
}
}
void printStringWithShift(char* s, int shift_speed) {
while (*s != 0) {
printCharWithShift(s, shift_speed);
s++;
}
}
void printString(char
s)
{
int col = 0;
while (*s != 0)
{
if (*s < 32) continue;
char c = *s - 32;
memcpy_P(buffer, CH + 7 * c, 7);
m.writeSprite(col, 0, buffer);
m.setColumn(col + buffer[0], 0);
col += buffer[0] + 1;
s++;
}
}

描述: 首先我们需要包含 SoftwareSerial.h 库,它将启用蓝牙通信并定义程序所需的一些变量。在设置部分,我们需要以每秒 38400 位的默认波特率初始化蓝牙。我将初始文本消息设置为“HowToMechatronics.com”,延迟滚动速度为 100 毫秒。

接下来,在循环部分,使用 Bluetooth.available() 函数检查是否有来自串行端口的传入数据,如果是,则使用 Bluetooth.read 函数开始读取串行端口,每次迭代读取一个字节。因此,第一个传入的字节将始终存储在“指示器”变量中,并根据它选择是否更改文本消息、滚动速度或 LED 矩阵的亮度。


使用MAX7219和Arduino板控制8×8LED矩阵显示屏详细教程及代码下载
-电子爱好者
-第10
张图片

如果我们查看 Android 应用程序代码块,我们可以注意到,当单击“发送”按钮时,首先发送指示字节,在本例中为“1”,这意味着我们要更改文本消息。为此,在 Arduino 端,我们将清除整个字符数组并清除 LED 矩阵显示。然后在“while”循环中,我们将读取串行端口中的其余数据,即在 Android 应用程序的文本框中键入的消息。

如果指示变量为“2”,则表示我们已更改滚动速度滑块的位置,因此我们将使用 Bluetooth.readString() 函数读取其新值并调整滚动速度。以同样的方式,我们调整 LED 的亮度。

您可以在此处下载 Android 应用程序:

点这里下载本文用到的代码以及程序文件

这几乎是本教程的所有内容,如果您有任何问题,可以使用下面的评论部分。

版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。
版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!

-- 展开阅读全文 --
工业数据采集
下一篇 »

发表评论