☛ 功能說明
使用 Arduino 開發板控制 8×8 矩陣型 LED 顯示器動態左移顯示字元 A、B、C、D。
字串移動與字元移動的基本原理相同,所不同的是字元移動只有一個字元重複變化,因此只需要一個資料指標指向字元資料區即可,但是字串移動是一個字元接著一個字元移動,而且每個字元都可能不相同。 因此控制字串移動需要兩個資料指標,一個指向作用中的資料區,另一個指向下一個字元資料區。
☛ 使用材料
Arduino UNO R3 開發板 × 1、8×8 共陰極矩陣型 LED 顯示器 ( MAX7219 ) 模組 × 1 。
☛ 電路圖及麵包板接線圖
☛ 程式碼
#include<SPI.h> //使用 SPI 函式庫 const int slaveSelect=10; //MAX7219 數位接腳 10 接到 MAX7219 致能腳 const int decodeMode=9; //MAX7219 解碼模式暫存器 const int intensity=10; //MAX7219 亮度控制暫存器 const int scanLimit=11; //MAX7219 掃描限制暫存器 const int shutDown=12; //MAX7219 關閉模式暫存器 const int dispTest=15; //MAX7219 顯示測試暫存器 const int charNums=4; //總字元數 byte i,j,k=1; //陣列指標 byte temp; //位元組資料暫存區 byte ptr1[8]; //作用字元陣列資料區 byte ptr2[8]; //下一字元陣列資料區 byte character[charNums][8]= //字元資料區 { {0x00, 0x7e, 0x11, 0x11, 0x11, 0x7e, 0x00, 0x00}, //字元 A {0x00, 0x7f, 0x49, 0x49, 0x49, 0x36, 0x00, 0x00}, //字元 B {0x00, 0x3e, 0x41, 0x41, 0x41, 0x22, 0x00, 0x00}, //字元 C {0x00, 0x7f, 0x41, 0x41, 0x41, 0x3e, 0x00, 0x00} }; //字元 D void setup() { SPI.begin(); //初始化 SPI 介面 pinMode(slaveSelect,OUTPUT); //設定數位接腳 10 為輸出模式 digitalWrite(slaveSelect,HIGH); //除能 MAX7219 sendCommand(shutDown,1); //MAX7219 正常工作 sendCommand(dispTest,0); //關閉 MAX7219 顯示測試 sendCommand(intensity,1); //設定 MAX7219 亮度為 1 sendCommand(scanLimit,7); //設定 MAX7219 掃描位數為8位 sendCommand(decodeMode,0); //設定 MAX7219 不解碼 for(i=0;i<8;i++) //載入第一個字元至 ptr1 陣列中 ptr1[i]=character[0][i]; } void loop() { for(i=0;i<8;i++) //載入第 k 個字元至 ptr2 陣列中 ptr2[i]=character[k][i]; for(i=0;i<8;i++) //每個字元左移 8 行 { display(); //顯示字元 delay(500); //延遲0.5秒 shiftLeft(); //左移 1 行 } if(k==charNums-1) //最後一個字元? k=0; //為最後一個字元,指向第一個字元 else k++; //不是最後字元, 指向下一個字元 } void display(void) //顯示字元函式 { for(j=0;j<8;j++) sendCommand(j+1,ptr1[j]); } void shiftLeft(void) //左移函式 { temp=ptr2[0]; for(j=0;j<7;j++) { ptr1[j]=ptr1[j+1]; ptr2[j]=ptr2[j+1]; } ptr1[7]=temp; } void sendCommand(byte command, byte value) { digitalWrite(slaveSelect,LOW); //致能 MAX7219 SPI.transfer(command); //傳送第一位元組資料至 MAX7219 SPI.transfer(value); //傳送第二位元組資料至 MAX7219 digitalWrite(slaveSelect,HIGH); //除能 MAX7219 }
☛ 練習
⑴ 設計 Arduino 程式,使用 Arduino 開發板控制 8×8 矩陣型 LED 顯示器動態右移顯示字元 A、B、C、D。
#include<SPI.h> //使用 SPI 函式庫 const int slaveSelect=10; //MAX7219 數位接腳 10 接到 MAX7219 致能腳 const int decodeMode=9; //MAX7219 解碼模式暫存器 const int intensity=10; //MAX7219 亮度控制暫存器 const int scanLimit=11; //MAX7219 掃描限制暫存器 const int shutDown=12; //MAX7219 關閉模式暫存器 const int dispTest=15; //MAX7219 顯示測試暫存器 const int charNums=4; //總字元數 byte i,j,k=1; //陣列指標 byte temp; //位元組資料暫存區 byte ptr1[8]; //作用字元陣列資料區 byte ptr2[8]; //下一字元陣列資料區 byte character[charNums][8]= //字元資料區 { {0x00, 0x7e, 0x11, 0x11, 0x11, 0x7e, 0x00, 0x00}, //字元 A {0x00, 0x7f, 0x49, 0x49, 0x49, 0x36, 0x00, 0x00}, //字元 B {0x00, 0x3e, 0x41, 0x41, 0x41, 0x22, 0x00, 0x00}, //字元 C {0x00, 0x7f, 0x41, 0x41, 0x41, 0x3e, 0x00, 0x00} }; //字元 D void setup() { SPI.begin(); //初始化 SPI 介面 pinMode(slaveSelect,OUTPUT); //設定數位接腳 10 為輸出模式 digitalWrite(slaveSelect,HIGH); //除能 MAX7219 sendCommand(shutDown,1); //MAX7219 正常工作 sendCommand(dispTest,0); //關閉 MAX7219 顯示測試 sendCommand(intensity,1); //設定 MAX7219 亮度為 1 sendCommand(scanLimit,7); //設定 MAX7219 掃描位數為8位 sendCommand(decodeMode,0); //設定 MAX7219 不解碼 for(i=0;i<8;i++) //載入第一個字元至 ptr1 陣列中 ptr1[i]=character[0][i]; } void loop() { for(i=0;i<8;i++) //載入第 k 個字元至 ptr2 陣列中 ptr2[i]=character[k][i]; for(i=0;i<8;i++) //每個字元左移 8 行 { display(); //顯示字元 delay(500); //延遲0.5秒 shiftRight(); //右移 1 行 } if(k==charNums-1) //最後一個字元? k=0; //為最後一個字元,指向第一個字元 else k++; //不是最後字元, 指向下一個字元 } void display(void) //顯示字元函式 { for(j=0;j<8;j++) sendCommand(j+1,ptr1[j]); } void shiftRight(void) //右移函式 { temp=ptr2[7]; for(j=7;j>0;j--) { ptr1[j]=ptr1[j-1]; ptr2[j]=ptr2[j-1]; } ptr1[0]=temp; } void sendCommand(byte command, byte value) { digitalWrite(slaveSelect,LOW); //致能 MAX7219 SPI.transfer(command); //傳送第一位元組資料至 MAX7219 SPI.transfer(value); //傳送第二位元組資料至 MAX7219 digitalWrite(slaveSelect,HIGH); //除能 MAX7219 }
⑵ 設計 Arduino 程式,使用電腦鍵盤控制 8×8 矩陣型 LED 顯示器動態移位顯示字元 A、B、C、D。 按下 R 鍵則字元右移,按下 L 鍵則字元左移。
#include<SPI.h> //使用 SPI 函式庫 const int slaveSelect=10; //MAX7219 數位接腳 10 接到 MAX7219 致能腳 const int decodeMode=9; //MAX7219 解碼模式暫存器 const int intensity=10; //MAX7219 亮度控制暫存器 const int scanLimit=11; //MAX7219 掃描限制暫存器 const int shutDown=12; //MAX7219 關閉模式暫存器 const int dispTest=15; //MAX7219 顯示測試暫存器 const int charNums=4; //總字元數 byte i,j,k=1; //陣列指標 byte temp; //位元組資料暫存區 byte ptr1[8]; //作用字元陣列資料區 byte ptr2[8]; //下一字元陣列資料區 byte character[charNums][8]= //字元資料區 { {0x00, 0x7e, 0x11, 0x11, 0x11, 0x7e, 0x00, 0x00}, //字元 A {0x00, 0x7f, 0x49, 0x49, 0x49, 0x36, 0x00, 0x00}, //字元 B {0x00, 0x3e, 0x41, 0x41, 0x41, 0x22, 0x00, 0x00}, //字元 C {0x00, 0x7f, 0x41, 0x41, 0x41, 0x3e, 0x00, 0x00} }; //字元 D int ch; //鍵盤按鍵值 void setup() { Serial.begin(9600); //串列埠初始化,設定鮑率 9600bps SPI.begin(); //初始化 SPI 介面 pinMode(slaveSelect,OUTPUT); //設定數位接腳 10 為輸出模式 digitalWrite(slaveSelect,HIGH); //除能 MAX7219 sendCommand(shutDown,1); //MAX7219 正常工作 sendCommand(dispTest,0); //關閉 MAX7219 顯示測試 sendCommand(intensity,1); //設定 MAX7219 亮度為 1 sendCommand(scanLimit,7); //設定 MAX7219 掃描位數為8位 sendCommand(decodeMode,0); //設定 MAX7219 不解碼 for(i=0;i<8;i++) //載入第一個字元至 ptr1 陣列中 ptr1[i]=character[0][i]; } void loop() { if(Serial.available()) //鍵盤按下任意鍵?? ch=Serial.read(); //讀取按鍵值 for(i=0;i<8;i++) //載入第 k 個字元至 ptr2 陣列中 ptr2[i]=character[k][i]; for(i=0;i<8;i++) //每個字元左移 8 行 { display(); //顯示字元 delay(500); //延遲0.5秒 if(ch=='r' || ch=='R') shiftRight(); //右移 1 行 if(ch=='l' || ch=='L') shiftLeft(); //左移 1 行 } if(k==charNums-1) //最後一個字元? k=0; //為最後一個字元,指向第一個字元 else k++; //不是最後字元, 指向下一個字元 } void display(void) //顯示字元函式 { for(j=0;j<8;j++) sendCommand(j+1,ptr1[j]); } void shiftRight(void) //右移函式 { temp=ptr2[7]; for(j=7;j>0;j--) { ptr1[j]=ptr1[j-1]; ptr2[j]=ptr2[j-1]; } ptr1[0]=temp; } void shiftLeft(void) //左移函式 { temp=ptr2[0]; for(j=0;j<7;j++) { ptr1[j]=ptr1[j+1]; ptr2[j]=ptr2[j+1]; } ptr1[7]=temp; } void sendCommand(byte command, byte value) { digitalWrite(slaveSelect,LOW); //致能 MAX7219 SPI.transfer(command); //傳送第一位元組資料至 MAX7219 SPI.transfer(value); //傳送第二位元組資料至 MAX7219 digitalWrite(slaveSelect,HIGH); //除能 MAX7219 }