#include <SPI.h>
const int CS = 53;
const int decodeMode = 9;
const int intensity = 10;
const int scanLimit = 11;
const int shutDown = 12;
const int dispTest = 15;
byte ConvertData = 0;
//const byte character[8]={ 0x00,0xfc,0x12,0x11,0x11,0x12,0xfc,0x00};
//const byte character[8]={ 0x18,0x24,0x42,0x42,0x7e,0x42,0x42,0x42}; // 'A'
const byte character[8] = { 0x00, 0x18, 0x18, 0x1c, 0x18, 0x18, 0x18, 0x7e }; // '1'
// const uint64_t IMAGES[] = { //0~9
// 0x7e1818181c181800,
// 0x7e060c3060663c00,
// 0x3c66603860663c00,
// 0x30307e3234383000,
// 0x3c6660603e067e00,
// 0x3c66663e06663c00,
// 0x1818183030667e00,
// 0x3c66663c66663c00,
// 0x3c66607c66663c00,
// 0x3c66666e76663c00
// };
uint64_t aaa;
const uint64_t IMAGES[] = {
0x1122334455667788,
0x6666667e66663c00,
0x3e66663e66663e00,
0x3c66060606663c00,
0x3e66666666663e00,
0x7e06063e06067e00,
0x0606063e06067e00,
0x3c66760606663c00,
0x6666667e66666600,
0x3c18181818183c00,
0x1c36363030307800,
0x66361e0e1e366600,
0x7e06060606060600,
0xc6c6c6d6feeec600,
0xc6c6e6f6decec600,
0x3c66666666663c00,
0x06063e6666663e00,
0x603c766666663c00,
0x66361e3e66663e00,
0x3c66603c06663c00,
0x18181818185a7e00,
0x7c66666666666600,
0x183c666666666600,
0xc6eefed6c6c6c600,
0xc6c66c386cc6c600,
0x1818183c66666600,
0x7e060c1830607e00,
0x0000000000000000,
0x7c667c603c000000,
0x3e66663e06060600,
0x3c6606663c000000,
0x7c66667c60606000,
0x3c067e663c000000,
0x0c0c3e0c0c6c3800,
0x3c607c66667c0000,
0x6666663e06060600,
0x3c18181800180000,
0x1c36363030003000,
0x66361e3666060600,
0x1818181818181800,
0xd6d6feeec6000000,
0x6666667e3e000000,
0x3c6666663c000000,
0x06063e66663e0000,
0xf0b03c36363c0000,
0x060666663e000000,
0x3e403c027c000000,
0x1818187e18180000,
0x7c66666666000000,
0x183c666600000000,
0x7cd6d6d6c6000000,
0x663c183c66000000,
0x3c607c6666000000,
0x3c0c18303c000000
};
const int IMAGES_LEN = sizeof(IMAGES) / 8;
void setup() {
Serial.begin(115200);
Max7219_init();
}
void loop() {
#pragma region seg : 單一資料
for (int i = 7; i >= 0; i--) {
byte n = (IMAGES[0] >> i * 8) & 0xFF;
Serial.print(n, HEX);
}
Serial.println("");
Serial.println("bbb");
aaa = MSB_LSB_Interchange64bit(IMAGES[0]); //MSB change leftmost
Serial.println("ccc");
for (int i = 7; i >= 0; i--) {
byte n = (aaa >> i * 8) & 0xFF;
Serial.print(n, HEX);
}
Serial.println();
delay(2000000);
//displayImage(ConvertData);
#pragma endregion
#pragma region seg : 顯示整個字串資料
/*
for(i=0;i<IMAGES_LEN;i++)
{
displayImage(IMAGES[i]);
delay(1000);
}
*/
#pragma endregion
}
void sendCommand(byte command, byte value) {
digitalWrite(CS, LOW);
SPI.transfer(command);
SPI.transfer(value);
digitalWrite(CS, HIGH);
}
void Max7219_init() {
SPI.begin();
pinMode(CS, OUTPUT);
digitalWrite(CS, HIGH);
sendCommand(shutDown, 1);
sendCommand(dispTest, 0);
sendCommand(intensity, 1);
sendCommand(scanLimit, 7);
sendCommand(decodeMode, 0);
}
//8x8點矩陣畫面(8 byte array)
void displayImage(uint64_t data) {
byte tmp = 0;
for (int i = 0; i < 8; i++) {
// tmp = (data >> i * 8) & 0xFF;
// tmp = MSB_LSB_Interchange(tmp); //MSB change leftmost
sendCommand(i + 1, tmp);
}
}
byte MSB_LSB_Interchange(byte data) //Change the rightmost to MSB to the leftmost to MSB
{
int a = 0;
for (int i = 0; i < 8; i++) {
bitWrite(a, i, bitRead(data, 7 - i));
}
return a;
}
uint64_t MSB_LSB_Interchange64bit(uint64_t data) //Change the rightmost to MSB to the leftmost to MSB
{
byte a = 0;
byte tmp = 0;
uint64_t newdata = 0;
for (int i = 0; i < 8; i++) {
tmp = (data >> i * 8) & 0xFF;
for (int j = 0; j < 8; j++) {
bitWrite(a, j, bitRead(tmp, 7 - j));
}
Serial.println(a,HEX);
newdata = newdata | ((uint64_t)a << i * 8) ;
}
return newdata;
}