User Tools

Site Tools


user:bmurphy7:hpc0:ledmatrix

LEDMatrix

The Circuit:

The Code:

  1 /**********************************************************************
  2 * Filename    : LEDMatrix.c
  3 * Description : Control LEDMatrix by 74HC595
  4 * Author      : www.freenove.com
  5 * modification: 2019/12/27
  6 **********************************************************************/
  7 #include <wiringPi.h>
  8 #include <stdio.h>
  9 #include <wiringShift.h>
 10  
 11 #define   dataPin   0   //DS Pin of 74HC595(Pin14)
 12 #define   latchPin  2   //ST_CP Pin of 74HC595(Pin12)
 13 #define   clockPin 3    //SH_CP Pin of 74HC595(Pin11)
 14 // data of smile face
 15 unsigned char pic[]={0x1c,0x22,0x51,0x45,0x45,0x51,0x22,0x1c};
 16 //hardware data for 0-F
 17 unsigned char data[]={                                                                                     
 18     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // " "
 19     0x00, 0x00, 0x3E, 0x41, 0x41, 0x3E, 0x00, 0x00, // "0"
 20     0x00, 0x00, 0x21, 0x7F, 0x01, 0x00, 0x00, 0x00, // "1"
 21     0x00, 0x00, 0x23, 0x45, 0x49, 0x31, 0x00, 0x00, // "2"
 22     0x00, 0x00, 0x22, 0x49, 0x49, 0x36, 0x00, 0x00, // "3"
 23     0x00, 0x00, 0x0E, 0x32, 0x7F, 0x02, 0x00, 0x00, // "4"
 24     0x00, 0x00, 0x79, 0x49, 0x49, 0x46, 0x00, 0x00, // "5"
 25     0x00, 0x00, 0x3E, 0x49, 0x49, 0x26, 0x00, 0x00, // "6"
 26     0x00, 0x00, 0x60, 0x47, 0x48, 0x70, 0x00, 0x00, // "7"
 27     0x00, 0x00, 0x36, 0x49, 0x49, 0x36, 0x00, 0x00, // "8"
 28     0x00, 0x00, 0x32, 0x49, 0x49, 0x3E, 0x00, 0x00, // "9"  
 29     0x00, 0x00, 0x3F, 0x44, 0x44, 0x3F, 0x00, 0x00, // "A"
 30     0x00, 0x00, 0x7F, 0x49, 0x49, 0x36, 0x00, 0x00, // "B"
 31     0x00, 0x00, 0x3E, 0x41, 0x41, 0x22, 0x00, 0x00, // "C"
 32     0x00, 0x00, 0x7F, 0x41, 0x41, 0x3E, 0x00, 0x00, // "D"
 33     0x00, 0x00, 0x7F, 0x49, 0x49, 0x41, 0x00, 0x00, // "E"
 34     0x00, 0x00, 0x7F, 0x48, 0x48, 0x40, 0x00, 0x00, // "F"
 35     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // " "
 36 };
 37 //used to read all of the data in our "pictures"
 38 void _shiftOut(int dPin,int cPin,int order,int val){
 39     int i;
 40     for(i = 0; i < 8; i++){
 41         digitalWrite(cPin,LOW);
 42         if(order == LSBFIRST){
 43             digitalWrite(dPin,((0x01&(val>>i)) == 0x01) ? HIGH : LOW);
 44             delayMicroseconds(10);
 45         }
 46         else {//if(order == MSBFIRST){
 47             digitalWrite(dPin,((0x80&(val<<i)) == 0x80) ? HIGH : LOW);
 48             delayMicroseconds(10);
 49         }
 50         digitalWrite(cPin,HIGH);
 51         delayMicroseconds(10);
 52     }
 53 }
 54  
 55 int main(void)
 56 {
 57     int i,j,k;
 58     unsigned char x;
 59     
 60     printf("Program is starting ...\n");
 61     
 62     wiringPiSetup();
 63     
 64     pinMode(dataPin,OUTPUT);
 65     pinMode(latchPin,OUTPUT);
 66     pinMode(clockPin,OUTPUT);
 67     while(1){
 68         //display the smiling face for 5 seconds
 69         for(j=0;j<500;j++){
 70             x=0x80;
 71             for(i=0;i<8;i++){
 72                 digitalWrite(latchPin,LOW);
 73                 _shiftOut(dataPin,clockPin,MSBFIRST,pic[i]);// Shift data to the first stage
 74                 _shiftOut(dataPin,clockPin,MSBFIRST,~x);    // Then shift data to the second stage 
 75                 digitalWrite(latchPin,HIGH);                // Output data of both stages at the same time
 76                 x>>=1;                                      // Display the next column
 77                 delay(1);
 78             }
 79         } 
 80         // Go through each part of a data line
 81         for(k=0;k<sizeof(data)-8;k++){                     
 82             // How many "frames" each number will have
 83             for(j=0;j<20;j++){  
 84                x=0x80;                                      // Make certain we start at the first column
 85                 for(i=k;i<8+k;i++){
 86                     digitalWrite(latchPin,LOW);
 87                     _shiftOut(dataPin,clockPin,MSBFIRST,data[i]);
 88                     _shiftOut(dataPin,clockPin,MSBFIRST,~x);
 89                     digitalWrite(latchPin,HIGH);
 90                     x>>=1;
 91                     delay(1);
 92                 }
 93             }
 94         }
 95     }
 96     return 0;
 97 }

This project required two 74HC595s, an LED Matrix board, eight 220ohm resistors, and a ton of jumpers. After setting the circuit up and running the program the LED matrix board first lights up with a smily face then proceeds to have a scrolling display of all numbers from 0 to F. I went into the code and only had to shorten a few comments and rephrase them to be making more sense without the technical jargon. This time the variables were declared outside the loops so I did not have to move them.

user/bmurphy7/hpc0/ledmatrix.txt · Last modified: 2020/05/12 01:53 by 127.0.0.1