項目21-LED點陣-滾動信息

#include <TimerOne.h>
#include <avr/pgmspace.h>
int DataPin = 2; ?//DIN
int LoadPin = 3; ?//CS
int ClockPin = 4; //CLK
#define SCAN_LIMIT 0X0b //掃描長度寄存器
#define DECODE_MODE 0X09 ?//小數(shù)點模式寄存器
#define SHUTDOWN 0X0C ? //關(guān)閉模式寄存器
#define INTENSITY 0X0A //亮度寄存器
byte buffer[8];
const byte font[][8] PROGMEM = {
? {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,B00001000,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,B00011110},
? {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},
? {B00001110,B00010001,B00010001,B00010001,B00010001,B00010001,B00010001,B00001110},
? {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,B00001001,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,B00011100,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 initMAX7219() {
? pinMode(DataPin, OUTPUT);
? pinMode(LoadPin, OUTPUT);
? pinMode(ClockPin, OUTPUT);
? writeData(SCAN_LIMIT, B00000111);
? writeData(DECODE_MODE, B00000000);
? writeData(SHUTDOWN, B00000001);
? writeData(INTENSITY, B00001111);
}
//清除數(shù)據(jù)
void clearDisplay() {
? for (byte x=0; x<8; x++) {
? ? buffer[x] = B00000000;
? }
? screenUpdata();
}
//LED燈控制
void screenUpdata() {
? for (byte row = 0; row<8; row++) {
? ? writeData(row+1, buffer[row]);
? }
}
//傳入數(shù)據(jù)到寄存器
void writeData(byte msb, byte lsb) {
? digitalWrite(LoadPin, LOW);
? shiftOut(DataPin, ClockPin, MSBFIRST, (msb)); //傳入地址
? shiftOut(DataPin, ClockPin, MSBFIRST, (lsb)); //傳入?yún)?shù)
? digitalWrite(LoadPin, HIGH);
}
void scroll (char myString[], int rate) {
? byte ledOutput;
? byte firstChr, secondChr;
? byte Char1, Char2;
? byte strLength = 0;
? byte scrollBit = 0;
? byte chrIndex = 0; //初始化字符串位置指針
? unsigned long counter;
? unsigned long time;
? while (myString[strLength]){strLength++;} //獲取字符串長度
? counter = millis();
? while(chrIndex < strLength) {
? ? time = millis();
? ? if (time > (counter+rate)) {
? ? ? ?Char1 = constrain(myString[chrIndex],32,126);
? ? ? Char2 = constrain(myString[chrIndex+1],32,126);
? ? ? for (byte j = 0; j<8; j++) {
? ? ? ? firstChr = pgm_read_byte(&font[Char1-32][j]);
? ? ? ? secondChr = pgm_read_byte(&font[Char2-32][j]);
? ? ? ? ledOutput = (firstChr << scrollBit)|(secondChr >> (8-scrollBit));
? ? ? ? buffer[j] = ledOutput;
? ? ? }
? ? ? scrollBit++;
? ? ? if (scrollBit >6) {
? ? ? ? scrollBit = 0;
? ? ? ? chrIndex++;
? ? ? }
? ? ? counter = millis();
? ? }
? ?
? }
}
void setup() {
? initMAX7219();
? Timer1.initialize(10000);
? Timer1.attachInterrupt(screenUpdata);
}
void loop() {
? clearDisplay();
? scroll("Apple", 200);
? scroll("I LOVE YOU", 200);
}