之前電腦上存了很多自己寫的關於arduino的有趣的小demo,因為重裝了系統,不小心誤刪了所有的文件(氣的半死),所以現在準備一有空就重寫一下之前寫過的東東,順帶再溫習一次,這次總不能再刪了吧,嘿嘿。 這次主要想記錄一下點陣的使用,方便以後查閱,也分享出來和伙伴們一起學習進步。這次在代碼中,用的最 ...
之前電腦上存了很多自己寫的關於arduino的有趣的小demo,因為重裝了系統,不小心誤刪了所有的文件(氣的半死),所以現在準備一有空就重寫一下之前寫過的東東,順帶再溫習一次,這次總不能再刪了吧,嘿嘿。
這次主要想記錄一下點陣的使用,方便以後查閱,也分享出來和伙伴們一起學習進步。這次在代碼中,用的最多的就是指針的使用,就當回顧知識點了。
用到的材料:一個16*16點陣(由4塊8*8的led點陣組成的),arduino的uno。
用到的庫文件:LedControl.h ,因為點陣是使用的max7219,這個庫很不錯。
先看看效果,下麵是視頻鏈接:
下麵直接上代碼,有興趣的小伙伴可以玩玩。
1 #include <LedControl.h> 2 const int DIN = 12; 3 const int CLK = 10; 4 const int CS = 11; 5 int maxLen;//字的總個數 6 int row;//縱向移動數組的行數 7 int col;//橫向移動數組的列數 8 //自定義函數 9 void showWordByArr(LedControl led, byte[][2]); //通過數組顯示 10 void showWordByPtr(LedControl led, byte(*word)[2]); //通過數組指針顯示 11 void showWords(LedControl led, byte(*words)[16][2]); //顯示一組文字 12 void showTopMoveWords(LedControl led, byte(*words)[16][2]); //向上移動顯示文字 13 void showLeftMoveWords(LedControl led, byte(*words)[16][2]); //向左移動顯示文字 14 15 //初始化對象 16 LedControl led = LedControl(DIN, CLK, CS , 4); 17 byte a[][2] = //實心五角星 18 { 19 0x01, 0x00, //第一,二個設備 20 0x01, 0x00, 21 0x03, 0x80, 22 0x03, 0x80, 23 0x03, 0x80, 24 0x07, 0xC0, 25 0xFF, 0xFE, 26 0x7F, 0xFC, 27 0x3F, 0xF8, //第三,四個設備 28 0x0F, 0xE0, 29 0x0F, 0xE0, 30 0x1F, 0xF0, 31 0x1E, 0xF0, 32 0x18, 0x30, 33 0x20, 0x08, 34 0x00, 0x00 35 }; 36 byte b[][2] = //空心五角星 37 { 38 0x01, 0x00, 0x01, 0x00, 0x02, 0x80, 0x02, 0x80, 0x02, 0x80, 0x04, 0x40, 0xFC, 0x7E, 0x40, 0x04, 39 0x30, 0x18, 0x08, 0x20, 0x08, 0x20, 0x11, 0x90, 0x16, 0x50, 0x18, 0x30, 0x20, 0x08, 0x00, 0x00 40 }; 41 byte c[][2] = //空心取反 42 { 43 0xFE, 0xFF, 0xFE, 0xFF, 0xFC, 0x7F, 0xFC, 0x7F, 0xFC, 0x7F, 0xF8, 0x3F, 0x00, 0x01, 0x80, 0x03, 44 0xC0, 0x07, 0xF0, 0x1F, 0xF0, 0x1F, 0xE0, 0x0F, 0xE1, 0x0F, 0xE7, 0xCF, 0xDF, 0xF7, 0xFF, 0xFF 45 }; 46 47 byte topWords[][16][2] = //慶祝祖國70周年 48 { 49 {0x00}, 50 { 51 0x01, 0x00, 0x00, 0x80, 0x3F, 0xFE, 0x20, 0x00, 0x20, 0x80, 0x20, 0x80, 0x20, 0x80, 0x2F, 0xFC, 52 0x20, 0x80, 0x21, 0x40, 0x21, 0x40, 0x22, 0x20, 0x42, 0x20, 0x44, 0x10, 0x88, 0x08, 0x10, 0x06 53 }, 54 { 55 0x20, 0x00, 0x13, 0xFC, 0x12, 0x04, 0xFA, 0x04, 0x0A, 0x04, 0x12, 0x04, 0x13, 0xFC, 0x38, 0x90, 56 0x54, 0x90, 0x94, 0x90, 0x10, 0x90, 0x11, 0x12, 0x11, 0x12, 0x12, 0x12, 0x14, 0x0E, 0x18, 0x00 57 }, 58 { 59 0x20, 0x00, 0x11, 0xF8, 0x11, 0x08, 0xF9, 0x08, 0x09, 0x08, 0x11, 0xF8, 0x11, 0x08, 0x39, 0x08, 60 0x55, 0x08, 0x95, 0xF8, 0x11, 0x08, 0x11, 0x08, 0x11, 0x08, 0x11, 0x08, 0x17, 0xFE, 0x10, 0x00 61 }, 62 { 63 0x00, 0x00, 0x7F, 0xFC, 0x40, 0x04, 0x40, 0x04, 0x5F, 0xF4, 0x41, 0x04, 0x41, 0x04, 0x4F, 0xE4, 64 0x41, 0x04, 0x41, 0x44, 0x41, 0x24, 0x5F, 0xF4, 0x40, 0x04, 0x40, 0x04, 0x7F, 0xFC, 0x40, 0x04 65 }, 66 { 67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x18, 0x44, 0x24, 0x44, 0x42, 0x08, 0x42, 0x08, 0x42, 68 0x10, 0x42, 0x10, 0x42, 0x10, 0x42, 0x10, 0x24, 0x10, 0x18, 0x00, 0x00, 0x00, 0x00 69 }, 70 { 71 0x00, 0x00, 0x3F, 0xF8, 0x21, 0x08, 0x21, 0x08, 0x2F, 0xE8, 0x21, 0x08, 0x21, 0x08, 0x3F, 0xF8, 72 0x20, 0x08, 0x27, 0xC8, 0x24, 0x48, 0x24, 0x48, 0x27, 0xC8, 0x40, 0x08, 0x40, 0x28, 0x80, 0x10 73 }, 74 { 75 0x10, 0x00, 0x10, 0x00, 0x1F, 0xFC, 0x20, 0x80, 0x20, 0x80, 0x40, 0x80, 0x1F, 0xF8, 0x10, 0x80, 76 0x10, 0x80, 0x10, 0x80, 0xFF, 0xFE, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80 77 }, 78 { 79 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 80 0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x10,0x00,0x20,0x00,0x00,0x00 81 } 82 83 }; 84 byte words[][16][2]= //願祖國繁榮昌盛,越來越好, 85 { 86 { 87 0x00,0x00,0x3F,0xFE,0x20,0x80,0x27,0xF8,0x24,0x08,0x27,0xF8,0x24,0x08,0x27,0xF8, 88 0x22,0x50,0x24,0x48,0x29,0x44,0x20,0x80,0x20,0x44,0x4A,0x42,0x4A,0x12,0x91,0xF0 89 }, 90 { 91 0x20,0x00,0x11,0xF8,0x11,0x08,0xF9,0x08,0x09,0x08,0x11,0xF8,0x11,0x08,0x39,0x08, 92 0x55,0x08,0x95,0xF8,0x11,0x08,0x11,0x08,0x11,0x08,0x11,0x08,0x17,0xFE,0x10,0x00 93 }, 94 { 95 0x00,0x00,0x7F,0xFC,0x40,0x04,0x40,0x04,0x5F,0xF4,0x41,0x04,0x41,0x04,0x4F,0xE4, 96 0x41,0x04,0x41,0x44,0x41,0x24,0x5F,0xF4,0x40,0x04,0x40,0x04,0x7F,0xFC,0x40,0x04 97 }, 98 { 99 0x20,0x20,0x3F,0x20,0x40,0x3E,0xBE,0x48,0x2A,0xA8,0xFF,0x28,0x4A,0x10,0x7F,0x28, 100 0x04,0x46,0x08,0x80,0x1F,0x00,0x02,0x10,0x3F,0xF8,0x01,0x08,0x15,0x20,0x22,0x10 101 }, 102 { 103 0x08,0x20,0x08,0x20,0xFF,0xFE,0x08,0x20,0x00,0x00,0x7F,0xFE,0x40,0x02,0x81,0x04, 104 0x01,0x00,0x7F,0xFC,0x03,0x80,0x05,0x40,0x09,0x20,0x31,0x18,0xC1,0x06,0x01,0x00 105 }, 106 { 107 0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,0x00,0x00, 108 0x3F,0xF8,0x20,0x08,0x20,0x08,0x3F,0xF8,0x20,0x08,0x20,0x08,0x3F,0xF8,0x20,0x08 109 }, 110 { 111 0x00,0x90,0x00,0x88,0x3F,0xFC,0x20,0x80,0x20,0x88,0x3E,0x48,0x22,0x50,0x22,0x24, 112 0x4A,0x54,0x44,0x8C,0x81,0x04,0x3F,0xF8,0x24,0x48,0x24,0x48,0xFF,0xFE,0x00,0x00 113 }, 114 { 115 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 116 0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x10,0x00,0x20,0x00,0x00,0x00 117 }, 118 { 119 0x10,0x28,0x10,0x24,0x10,0x20,0x7D,0xFC,0x11,0x20,0x11,0x24,0xFD,0x24,0x11,0x28, 120 0x11,0x28,0x51,0x10,0x5D,0x94,0x51,0x2C,0x70,0x44,0x50,0x00,0x4F,0xFE,0x80,0x00 121 }, 122 { 123 0x01,0x00,0x01,0x00,0x01,0x00,0x7F,0xFC,0x01,0x00,0x11,0x10,0x09,0x10,0x09,0x20, 124 0xFF,0xFE,0x03,0x80,0x05,0x40,0x09,0x20,0x31,0x18,0xC1,0x06,0x01,0x00,0x01,0x00 125 }, 126 { 127 0x10,0x28,0x10,0x24,0x10,0x20,0x7D,0xFC,0x11,0x20,0x11,0x24,0xFD,0x24,0x11,0x28, 128 0x11,0x28,0x51,0x10,0x5D,0x94,0x51,0x2C,0x70,0x44,0x50,0x00,0x4F,0xFE,0x80,0x00 129 }, 130 { 131 0x10,0x00,0x10,0xFC,0x10,0x04,0x10,0x08,0xFC,0x10,0x24,0x20,0x24,0x20,0x25,0xFE, 132 0x24,0x20,0x48,0x20,0x28,0x20,0x10,0x20,0x28,0x20,0x44,0x20,0x84,0xA0,0x00,0x40 133 }, 134 { 135 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 136 0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x10,0x00,0x20,0x00,0x00,0x00 137 } 138 }; 139 byte leftWords[][16][2]= //我愛你中國! 140 { 141 { 142 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 143 0x00,0x00,0x1A,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 144 }, 145 { 146 0x04,0x24,0x24,0x24,0x7F,0xC4,0x44,0x04,0x04,0xFF,0x04,0x44,0x34,0x05,0x04,0x00, 147 0x00,0x10,0x12,0x21,0xFE,0x40,0x82,0x02,0x04,0xC8,0x30,0x28,0x44,0x82,0x1F,0x00 148 }, 149 { 150 0x01,0x26,0x34,0x2C,0x24,0x24,0x37,0x4C,0x44,0x44,0x4C,0x74,0xC4,0x45,0x06,0x00, 151 0x00,0x82,0x84,0x89,0x91,0xE1,0xB2,0xAA,0xA4,0xA4,0xAA,0xB2,0x81,0x01,0x01,0x00 152 }, 153 { 154 0x00,0x01,0x06,0x1F,0xE0,0x02,0x04,0x18,0xF0,0x10,0x13,0x10,0x10,0x14,0x18,0x00, 155 0x80,0x00,0x00,0xFF,0x00,0x08,0x30,0xC0,0x02,0x01,0xFE,0x00,0x80,0x60,0x18,0x00 156 }, 157 { 158 0x00,0x00,0x0F,0x08,0x08,0x08,0x08,0xFF,0x08,0x08,0x08,0x08,0x0F,0x00,0x00,0x00, 159 0x00,0x00,0xF0,0x20,0x20,0x20,0x20,0xFF,0x20,0x20,0x20,0x20,0xF0,0x00,0x00,0x00 160 }, 161 { 162 0x00,0x7F,0x40,0x48,0x49,0x49,0x49,0x4F,0x49,0x49,0x49,0x48,0x40,0x7F,0x00,0x00, 163 0x00,0xFF,0x02,0x12,0x12,0x12,0x12,0xF2,0x12,0x52,0x32,0x12,0x02,0xFF,0x00,0x00 164 }, 165 { 166 0x00,0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 167 0x00,0x00,0x00,0xCC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 168 } 169 }; 170 171 172 void setup() { 173 for (int i = 0; i < 4; ++i) 174 { 175 led.shutdown(i, false);//關閉省電模式,這一步必須做,否則顯示不出來 176 led.clearDisplay(i);//清空數據 177 } 178 maxLen = sizeof(words)/sizeof(*words);//計算總的字數 179 row = 16 * (sizeof(topWords) / sizeof(*topWords)); 180 col = 16 * (sizeof(leftWords) / sizeof(*leftWords)); 181 } 182 183 void loop() { 184 // put your main code here, to run repeatedly: 185 showWordByPtr(led, b); //空心 186 delay(500); 187 for(int i =0;i<5;++i) 188 { 189 showWordByArr(led, a); //實心 190 delay(200); 191 showWordByArr(led, c); //空心取反 192 delay(200); 193 } 194 showWordByArr(led, a); //實心 195 delay(800); 196 showTopMoveWords(led, topWords); 197 delay(500); 198 showWords(led, words); 199 delay(100); 200 showLeftMoveWords(led, leftWords); 201 delay(1000); 202 } 203 204 void showWordByArr(LedControl led, byte word[][2]) 205 { 206 for (int i = 0; i < 4; ++i) 207 { 208 if (i < 2) 209 { 210 for (int j = 0; j < 8; ++j) 211 { 212 led.setRow(i, j, word[j][i]); 213 } 214