Max 7219 LED Driver
ลักษณของ 8x8 led matrix
การต่อ LED Matrix 1 ชุด
การทดลอง ควบคุม 8x8 led matrix 1 ชุด
1. ต่อวงจรตามรูป
2.ติดตั้ง library max7219 ลงใน libraries floder ของ sketch โดยดาวน์โหลด LedControlMS.rar จาก อินเตอร์เนทหรือจากไฟล์ด้านล่าง
สำหรับผู้ที่ยังติดตั้งไม่เป็นให้ทำตามขั้นตอน 2.1-2.4
การติดตั้ง library max7219 ให้ Arduino มองเห็น
2.1 ดาวน์โหลด LedControlMS.rar จาก ที่นี้
2.2 ขยายไฟล์ แล้วนำไปวางที่ โฟล์เดอร์ libraries ใน sketch
2.3 ปิดโปรแกรม Arduino แล้วเปิดใหม่
2.4 ตรวจว่าเห็น library โดยใช้เมนูไฟล์ แล้วเลือก example จะต้องพบกลุ่มตัวอย่าง LedControlMS ตามรูป
3. เขียนโปรแกรม
โปรแกรมที่ 1
-----------------------------------------------------------------------------------------------------------
#include "LedControlMS.h"
/*
Now we need a LedControl to work with.
***** These pin numbers will probably not work with your hardware *****
pin 12 is connected to the DataIn
pin 11 is connected to the CLK
pin 10 is connected to LOAD (CS)
We have only a single MAX72XX.
*/
#define NBR_MTX 1 //จำนวน LED Module
LedControl lc=LedControl(12,11,10, NBR_MTX);
void setup() {
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.shutdown(0,false);
/* Set the brightness to a low value */
lc.setIntensity(0,0);
/* and clear the display */
lc.clearDisplay(0);
delay(100);
lc.clearAll();
lc.setRow(0, 0, 0xff);
lc.setRow(0, 1, 0xfe);
lc.setRow(0, 2, 0xfc);
lc.setRow(0, 3, 0xf8);
lc.setRow(0, 4, 0xf0);
lc.setRow(0, 5, 0xe0);
lc.setRow(0, 6, 0xc0);
lc.setRow(0, 7, 0x80);
}
void loop(){
}
-----------------------------------------------------------------------------------------------------------
อธิบายความหมาย
ฟังก์ชั่น void setRow(int addr, int row, byte value);
addr เป็นตำแหน่งของโมดูล LED เริ่มจาก 0 กรณีที่มีตัวเดียวให้เป็น 0
row แถวที่ของ LED เริ่มจากซ้ายมือแถวที่ 0 ไปทางขวามือ
value ค่าที่ใช้กำหนดการติดดับของ LED แต่ละดวงในแถวนั้น ถ้าให้ เป็น 1 จะทำให้ LED ติด ถ้าให้เป็น 0 จะทำให้ LED ดับ
โดยนับบิต ที่ 0 ถึง 7 จากบนลงล่าง
ดังนั้นค่าตามโปรแกรมจะได้ รูปเป็น
นอกจากนี้ในไลบรารียังได้กำหนด font ใว้บ้างแล้ว สามารถดูได้จากตัวอย่างในไลบรารี
การต่อ LED หลายๆโมดูลเรียงกันสามารถต่อได้ดังนี้
ปรับปรุงโปรแกรม ทำเป็นฟังก์ชั่น
โปรแกรมที่ 2
-----------------------------------------------------------------------------------------------------------
#include "LedControlMS.h"
#define NBR_MTX 1
// LedControl(DIN,CLK,CS number);
LedControl lc=LedControl(12,11,10, NBR_MTX);
const unsigned char pat1[8] ={0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80};
void display(unsigned char *addr){
unsigned char data;
for (int i=0; i < 8; i++){
data = addr[i];
lc.setRow(0, i, data);
}
}
void setup() {
lc.shutdown(0,false); //The MAX72XX is in power-saving mode on startup,we have to do a wakeup call
lc.setIntensity(0,0); //Set the brightness to a low value
lc.clearDisplay(0); // and clear the display
delay(100);
lc.clearAll();
display(pat1);
}
void loop(){
}
-----------------------------------------------------------------------------------------------------------
ใช้ฟังก์ชั่น Display แสดงภาพเคลื่อนไหว โดยใช้ภาพหลายภาพ
โปรแกรมที่ 3
-----------------------------------------------------------------------------------------------------------
#include "LedControlMS.h"
#define NBR_MTX 1
// LedControl(DIN,CLK,CS number);
LedControl lc=LedControl(12,11,10, NBR_MTX);
const char cir1[]={0x00,0x00,0x00,0x0f,0x00,0x00,0x00,0x00};
const char cir2[]={0x00,0x00,0x00,0x08,0x04,0x02,0x01,0x00};
const char cir3[]={0x00,0x00,0x00,0x08,0x08,0x08,0x08,0x00};
const char cir4[]={0x00,0x00,0x00,0x08,0x10,0x20,0x40,0x00};
const char cir5[]={0x00,0x00,0x00,0x78,0x00,0x00,0x00,0x00};
const char cir6[]={0x40,0x20,0x10,0x08,0x00,0x00,0x00,0x00};
const char cir7[]={0x08,0x08,0x08,0x08,0x00,0x00,0x00,0x00};
const char cir8[]={0x01,0x02,0x04,0x08,0x00,0x00,0x00,0x00};
void display(unsigned char *addr){
unsigned char data;
for (int i=0; i < 8; i++){
data = addr[i];
lc.setRow(0, i, data);
}
}
void setup() {
lc.shutdown(0,false); //The MAX72XX is in power-saving mode on startup,we have to do a wakeup call
lc.setIntensity(0,0); //Set the brightness to a low value
lc.clearDisplay(0); // and clear the display
delay(100);
lc.clearAll();
}
void loop(){
for (int j=0; j < 8; j++){
display(cir1);
delay(200);
display(cir2);
delay(200);
display(cir3);
delay(200);
display(cir4);
delay(200);
display(cir5);
delay(200);
display(cir6);
delay(200);
display(cir7);
delay(200);
display(cir8);
delay(200);
}
}
---------------------------------------------------------------------------
เขียนอีกแบบให้สั้นและกระชับขึ้น max7219-fn-test-5-2.ino
#include "LedControlMS.h"
#define NBR_MTX 1
// LedControl(DIN,CLK,CS number);
LedControl lc=LedControl(12,11,10, NBR_MTX);
const char cir[64]={
0x00,0x00,0x00,0x0f,0x00,0x00,0x00,0x00, //picture 1
0x00,0x00,0x00,0x08,0x04,0x02,0x01,0x00, //picture 2
0x00,0x00,0x00,0x08,0x08,0x08,0x08,0x00, //picture 3
0x00,0x00,0x00,0x08,0x10,0x20,0x40,0x00, //picture 4
0x00,0x00,0x00,0x78,0x00,0x00,0x00,0x00, //picture 5
0x40,0x20,0x10,0x08,0x00,0x00,0x00,0x00, //picture 6
0x08,0x08,0x08,0x08,0x00,0x00,0x00,0x00, //picture 7
0x01,0x02,0x04,0x08,0x00,0x00,0x00,0x00, //picture 8
};
void display(unsigned char *addr){
unsigned char data;
for (int i=0; i < 8; i++){
data = addr[i];
lc.setRow(0, i, data);
}
}
void setup() {
lc.shutdown(0,false); //The MAX72XX is in power-saving mode on startup,we have to do a wakeup call
lc.setIntensity(0,0); //Set the brightness to a low value
lc.clearDisplay(0); // and clear the display
delay(100);
lc.clearAll();
}
void loop(){
for (int j=0; j < 8; j++){
display(cir+8*j);
delay(200);
}
}
-----------------------------------------------------------------------------------------------------------
ใช้ฟังก์ชั่น Display แสดงภาพเคลื่อนไหว แบบใช้ภาพเดียว ใช้วิธีเลื่อนภาพ
โปรแกรมที่ 4
-----------------------------------------------------------------------------------------------------------
#include "LedControlMS.h"
#define NBR_MTX 1
// LedControl(DIN,CLK,CS number);
LedControl lc=LedControl(12,11,10, NBR_MTX);
const unsigned char arrow[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x10,0x38,0x7c,0xfe,0x38,0x38,0x38,0x38,
0x38,0x38,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00};
void display(unsigned char *addr){
unsigned char data;
for (int i=0; i < 8; i++){
data = addr[i];
lc.setRow(0, i, data);
}
}
void setup() {
lc.shutdown(0,false); //The MAX72XX is in power-saving mode on startup,we have to do a wakeup call
lc.setIntensity(0,0); //Set the brightness to a low value
lc.clearDisplay(0); // and clear the display
delay(100);
lc.clearAll();
}
void loop(){
for (int j=0; j < 18; j++){
display(arrow+j);
delay(200);
}
}
-----------------------------------------------------------------------------------------------------------
สำหรับผู้ที่แปลแล้ว Error ให้ทดลองแปล ไฟล์ ในโปรแกรมที่ 4 max7219-fn-test-4.ino ด้านล่างนี้
-----------------------------------------------------------------------------------------------------------
โปรแกรมที่ 4
ใช้ฟังก์ชั่น displayChar ของ ไลบรารี่ แสดงเลข 0 ถึง 9
สำหรับฟังก์ชั่นนี้จะแสดงตัวอักษรอะไรได้บ้าง ดูได้จากตัวแปร const static byte alphabetBitmap[41][6] ในไฟล์ LedControlMS.h ที่อยู่ในโฟล์เดอร์ \Arduino\libraries\LedControlMS
-----------------------------------------------------------------------------------------------------------
#include "LedControlMS.h"
#define NBR_MTX 1 //จำนวน LED Module
//LedControl lc=LedControl(Datain,CLK,CS, NBR_MTX);
LedControl lc=LedControl(12,11,10, NBR_MTX);
void setup() {
lc.shutdown(0,false); // do a wakeup call
lc.setIntensity(0,1); // Set the brightness to a low value
lc.clearDisplay(0); // clear the display
delay(100);
}
void loop(){
for (int j=0; j <= 9; j++){
lc.displayChar(0, j);
delay(400);
}
}
-----------------------------------------------------------------------------------------------------------
โปรแกรมที่ 5
ใช้ฟังก์ชั่น displayChar ของ ไลบรารี่ แสดงตัวอักษร A ถึง Z
ควรศึกษาวิธีการอ้างอิง Address .ในตัวแปร Array alphabetBitmap[41][6]
ความหมายของ ('A'-0x41)+(15+j)
เนื่องจาก
รหัส ASCII ของตัวอักษร 'A' คือ 0x41
index ของตัวอักษร 'A' ใน ในตัวแปร Array alphabetBitmap[41][6] อยู่ที่ตำแหน่ง 15
ดังนั้นเพื่อต้องการให้การสังพิมพ์ด้วยการใช้รหัส ASCII จึงปรับให้ค่า เช่นถ้าต้องการแสดงตัว 'T' ก็ใช้คำสั่ง
lc.displayChar(0, ('T'-0x41+15));
หรือบวกลบกันก่อน
-0x41+ 15 = -65+15 = -50
lc.displayChar(0, ('T'-50))
ถ้าต้องการแสดงตัวอักษร 'R' ก็ใช้เป็น lc.displayChar(0, ('R'-50))
-----------------------------------------------------------------------------------------------------------
#include "LedControlMS.h"
#define NBR_MTX 1 //จำนวน LED Module
//LedControl lc=LedControl(Datain,CLK,CS, NBR_MTX);
LedControl lc=LedControl(12,11,10, NBR_MTX);
void setup() {
lc.shutdown(0,false); // do a wakeup call
lc.setIntensity(0,1); // Set the brightness to a low value
lc.clearDisplay(0); // clear the display
delay(100);
}
void loop(){
for (int j=0; j < 26; j++){
lc.displayChar(0, ('A'-0x41)+(15+j));
delay(400);
}
}
-----------------------------------------------------------------------------------------------------------