Advertisement
sonprao

LEDMATRIX

Feb 22nd, 2020
1,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.22 KB | None | 0 0
  1. #define COL_NUM 16 // number of columns (16)
  2. #define SPEED 20
  3.  
  4. #define LATCH 2
  5.  
  6. #define CLOCK_ROW 3
  7. #define DATA_ROW  4
  8.  
  9. #define CLOCK_COL 5
  10. #define DATA_COL 6
  11.  
  12. unsigned char charMatrix[26][8] = {
  13.     {0b00111111, 0b01010000, 0b10010000, 0b01010000, 0b00111111, 0b00000000, 0b00000000, 0b00000000},//A
  14.     {0b11111111, 0b10010001, 0b10010001, 0b10010001, 0b01101110, 0b00000000, 0b00000000, 0b00000000},//B
  15.     {0b01111110, 0b10000001, 0b10000001, 0b10000001, 0b10000001, 0b00000000, 0b00000000, 0b00000000},//C
  16.     {0b11111111, 0b10000001, 0b10000001, 0b10000001, 0b01111110, 0b00000000, 0b00000000, 0b00000000},//D
  17.     {0b11111111, 0b10010001, 0b10010001, 0b10010001, 0b10010001, 0b00000000, 0b00000000, 0b00000000},//E
  18.     {0b11111111, 0b10010000, 0b10010000, 0b10010000, 0b10010000, 0b00000000, 0b00000000, 0b00000000},//F
  19.     {0b01111110, 0b10000001, 0b10000001, 0b10001001, 0b01001110, 0b00001000, 0b00000000, 0b00000000},//G
  20.     {0b11111111, 0b00010000, 0b00010000, 0b00010000, 0b11111111, 0b00000000, 0b00000000, 0b00000000},//H
  21.     {0b10000001, 0b10000001, 0b11111111, 0b10000001, 0b10000001, 0b00000000, 0b00000000, 0b00000000},//I
  22.     {0b10000011, 0b10000001, 0b11111111, 0b10000000, 0b10000000, 0b00000000, 0b00000000, 0b00000000},//J
  23.     {0b11111111, 0b00011000, 0b00100100, 0b01000010, 0b10000001, 0b00000000, 0b00000000, 0b00000000},//K
  24.     {0b11111111, 0b00000001, 0b00000001, 0b00000001, 0b00000001, 0b00000000, 0b00000000, 0b00000000},//L
  25.     {0b11111111, 0b01000000, 0b00100000, 0b01000000, 0b11111111, 0b00000000, 0b00000000, 0b00000000},//M
  26.     {0b11111111, 0b01000000, 0b00100000, 0b00010000, 0b11111111, 0b00000000, 0b00000000, 0b00000000},//N
  27.     {0b01111110, 0b10000001, 0b10000001, 0b10000001, 0b01111110, 0b00000000, 0b00000000, 0b00000000},//O
  28.     {0b11111111, 0b10010000, 0b10010000, 0b10010000, 0b01100000, 0b00000000, 0b00000000, 0b00000000},//P
  29.     {0b01111110, 0b10000001, 0b10000001, 0b10000101, 0b01111110, 0b00000001, 0b00000000, 0b00000000},//Q
  30.     {0b11111111, 0b10011000, 0b10010100, 0b10010010, 0b01100001, 0b00000000, 0b00000000, 0b00000000},//R
  31.     {0b01100001, 0b10010001, 0b10010001, 0b10010001, 0b01001110, 0b00000000, 0b00000000, 0b00000000},//S
  32.     {0b10000000, 0b10000000, 0b11111111, 0b10000000, 0b10000000, 0b00000000, 0b00000000, 0b00000000},//T
  33.     {0b11111110, 0b00000001, 0b00000001, 0b00000001, 0b11111110, 0b00000000, 0b00000000, 0b00000000},//U
  34.     {0b11111100, 0b00000010, 0b00000001, 0b00000010, 0b11111100, 0b00000000, 0b00000000, 0b00000000},//V
  35.     {0b11111111, 0b00000010, 0b00000100, 0b00000010, 0b11111111, 0b00000000, 0b00000000, 0b00000000},//W
  36.     {0b11000011, 0b00100100, 0b00011000, 0b00100100, 0b11000011, 0b00000000, 0b00000000, 0b00000000},//X
  37.     {0b11100000, 0b00010000, 0b00001111, 0b00010000, 0b11100000, 0b00000000, 0b00000000, 0b00000000},//Y
  38.     {0b10000111, 0b10001001, 0b10010001, 0b10100001, 0b11000001, 0b00000000, 0b00000000, 0b00000000} //Z
  39. };
  40.  
  41. unsigned char numMatrix[10][8] = {
  42.     {0b01111110, 0b10000001, 0b10000001, 0b10000001, 0b01111110, 0b00000000, 0b00000000, 0b00000000},//0
  43.     {0b00100001, 0b01000001, 0b11111111, 0b00000001, 0b00000001, 0b00000000, 0b00000000, 0b00000000},//1
  44.     {0b01000011, 0b10000101, 0b10001001, 0b10010001, 0b01100001, 0b00000000, 0b00000000, 0b00000000},//2
  45.     {0b01000001, 0b10010001, 0b10010001, 0b10010001, 0b01101110, 0b00000000, 0b00000000, 0b00000000},//3
  46.     {0b11110000, 0b00010000, 0b00010000, 0b11111111, 0b00000000, 0b00000000, 0b00000000, 0b00000000},//4
  47.     {0b11110001, 0b10010001, 0b10010001, 0b10010001, 0b10001110, 0b00000000, 0b00000000, 0b00000000},//5
  48.     {0b01111110, 0b10010001, 0b10010001, 0b10010001, 0b10001110, 0b00000000, 0b00000000, 0b00000000},//6
  49.     {0b10000000, 0b10000000, 0b10011111, 0b10100000, 0b11000000, 0b00000000, 0b00000000, 0b00000000},//7
  50.     {0b01101110, 0b10010001, 0b10010001, 0b10010001, 0b01101110, 0b00000000, 0b00000000, 0b00000000},//8
  51.     {0b01100000, 0b10010001, 0b10010001, 0b10010001, 0b01111110, 0b00000000, 0b00000000, 0b00000000} //9
  52. };
  53.  
  54. unsigned char specialMatrix[5][8] = {
  55.     {0b00011000, 0b00100100, 0b01000010, 0b00100001, 0b01000010, 0b00100100, 0b00011000, 0b00000000},// HEART
  56.     {0b00000001, 0b00000110, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000},// ,
  57.     {0b00000001, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000},// .
  58.     {0b11111101, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000} // !
  59. };
  60. unsigned char buffer[COL_NUM];
  61.  
  62. //Tìm xem một kí tự ứng với đâu trong các mảng UP, NUM, SPECIALS)
  63. unsigned char* getArrFromChar(unsigned char ch) {
  64.     if ((ch >= 48) && (ch <= 57)) // Numbers
  65.         return numMatrix[ch - 48];
  66.  
  67.     if ((ch>= 65) && (ch <= 90)) // Characters
  68.         return charMatrix[ch - 65];
  69.  
  70.     switch (ch) { // Specials
  71.     case '$':  // Heart
  72.         return specialMatrix[0];
  73.     case ',':
  74.         return specialMatrix[1];
  75.     case '.':
  76.         return specialMatrix[2];
  77.     case '!':
  78.         return specialMatrix[3];
  79.     };
  80.     return NULL; // if nothing matches
  81. }
  82.  
  83. void moveLeft() {
  84.     // &buffer[1] because buffer[1] is *(buffer+1), and we need a pointer
  85.     memmove(buffer, &(buffer[1]), (COL_NUM - 1));
  86. }
  87. void show(unsigned char hold) {
  88.     for (unsigned char k = 0; k < hold; k++) {
  89.         for (unsigned char row = 0; row < 8; row++) {
  90.             unsigned char d[2] = {0, 0};
  91.  
  92.             for (unsigned char col = 0; col < COL_NUM; col++) {
  93.                 d[col/8] = (d[col/8] | ((buffer[col] >> (7-row)) & 1));
  94.                 if (col%8!=7)
  95.                     d[col/8]<<=1;
  96.             }
  97.  
  98.             // Turn off
  99.             digitalWrite(LATCH, LOW);
  100.  
  101.             // We are printing to row {{row}}
  102.             shiftOut(DATA_ROW, CLOCK_ROW, MSBFIRST, ~(_BV(row)));
  103.             //Serial.print(d[1], BIN);
  104.             //Serial.println(d[0], BIN);
  105.             // Hmm which is the right order then
  106.             shiftOut(DATA_COL, CLOCK_COL, LSBFIRST, d[1]);
  107.             shiftOut(DATA_COL, CLOCK_COL, LSBFIRST, d[0]);
  108.  
  109.             // Turn on
  110.             digitalWrite(LATCH, HIGH);
  111.  
  112.            //delay(5000);
  113.         }
  114.     }
  115. }
  116. void addCol(unsigned char col) {
  117.     moveLeft();
  118.     buffer[COL_NUM - 1] = col;
  119.     show(SPEED);
  120. }
  121.  
  122. //Thêm một kí tự, sau đó gọi hàm addCol để hiển thị từng phần của kí tự
  123. void addChar(char chr) {
  124.     unsigned char* arr = getArrFromChar(chr);
  125.     if (arr == NULL)
  126.         return;
  127.     for (unsigned char i = 0; i < 8; i++) {
  128.         if (arr[i] != 0) { // nice trick there m8
  129.             addCol(arr[i]);
  130.         }
  131.     }
  132.     addCol(0);
  133. }
  134.  
  135. //Thêm vào một cột vào mảng leds để hiển thị
  136.  
  137.  
  138. // Move everything to the left
  139.  
  140.  
  141. //Hiển thị mảng leds ra ma trận
  142.  
  143.  
  144. void parseString(const char* s) {
  145.     while (*s) {
  146.         if (*s == ' ') {
  147.             addCol(0);
  148.             addCol(0);
  149.         } else {
  150.             addChar(toupper(*s));
  151.         }
  152.         s++;
  153.     }
  154. }
  155.  
  156. void setup() {
  157.     Serial.begin(9600);
  158.  
  159.     pinMode(LATCH, OUTPUT);
  160.  
  161.     pinMode(CLOCK_COL, OUTPUT);
  162.     pinMode(DATA_COL, OUTPUT);
  163.  
  164.     pinMode(CLOCK_ROW, OUTPUT);
  165.     pinMode(DATA_ROW, OUTPUT);
  166. }
  167.  
  168. void loop() {
  169.     parseString("DANG XUAN MINH SON          ");
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement