Code for driving Matrices with 595 shift registers Arduino?

Post here to discuss all things Arduino!

Moderators: Chuckt, Garth, bitfogav

User avatar
Saimaster13
I practically live here!
I practically live here!
Posts: 176
Joined: Mon Aug 13, 2012 4:23 am
Location: Sarasota, Florida

Code for driving Matrices with 595 shift registers Arduino?

Post by Saimaster13 » Sat Sep 15, 2012 9:33 am

I have hooked up a 8x8 matrix to two '595s and applied some open source codes and they work. I have not, but I understand how to hook up transistors and transistor arrays to sink extra power. However, the codes do not do what I want.

I want to be able to call up different characters on the display in hexadecimal (such as when a button is pressed, the matrix shows the character: 0x1c, 0x22, 0x22, 0x02, 0x04, 0x08, 0x10, 0x3f which is a custom made "2" in hexadecimal.) How do I make so I can write hexadecimal to a matrix? Would it have something to do with something like this command line:

Code: Select all

int two[8] = {0x1c, 0x22, 0x22, 0x02, 0x04, 0x08, 0x10, 0x3f}; //set two[] to contain the hexadecimal values needed to make the character "2"
Or would there be a better way? I saw one code define all the characters that he wanted in his sketch with one (string? or would it be something different because it is a string inside of a string?

Code: Select all

static const byte ASCII[][5] ={
 {0x00, 0x00, 0x00, 0x00, 0x00} // 20  
,{0x00, 0x00, 0x5f, 0x00, 0x00} // 21 !
,{0x00, 0x07, 0x00, 0x07, 0x00} // 22 "
And how would I integrate either one (preferably the better one) into a sketch, and what would be the basic sketch?

By the way, thanks Brad for teaching me about hexadecimal and how to convert binary into it!
Joshua

User avatar
brad
Site Admin
Site Admin
Posts: 2576
Joined: Fri Mar 26, 2010 10:30 pm

Re: Code for driving Matrices with 595 shift registers Ardui

Post by brad » Sat Sep 15, 2012 9:47 am

Your first piece of code looks good to me (this is how I would do it, in fact - it's exactly how I currently do it!)

I guess the main question here is, what does your code look like to send the data to the 595's and onto the matrix?

User avatar
Saimaster13
I practically live here!
I practically live here!
Posts: 176
Joined: Mon Aug 13, 2012 4:23 am
Location: Sarasota, Florida

Re: Code for driving Matrices with 595 shift registers Ardui

Post by Saimaster13 » Sat Sep 15, 2012 10:15 am

Well, I do not really have a main code yet, I'm not sure what kind of code work work the best to add in a hexadecimal to matrix converter, and I have just been using other people's open source codes (unable to put hexadecimals into them.) Actually, I had a lot of trouble finding 595 codes controlling matrices on the Arduino, only ones I found were one that inverted the anode pins, and two which were specifically made for a certain game (they played either pong or snake.)
Joshua

User avatar
brad
Site Admin
Site Admin
Posts: 2576
Joined: Fri Mar 26, 2010 10:30 pm

Re: Code for driving Matrices with 595 shift registers Ardui

Post by brad » Sat Sep 15, 2012 10:16 am

I have to head out now to do some tile shopping but i'll get back to you!

User avatar
Saimaster13
I practically live here!
I practically live here!
Posts: 176
Joined: Mon Aug 13, 2012 4:23 am
Location: Sarasota, Florida

Re: Code for driving Matrices with 595 shift registers Ardui

Post by Saimaster13 » Sun Sep 16, 2012 1:56 pm

After further looking into it, I realized that I do not have a code that can run binary or hexadecimal strings. I do not know how to make the code from scratch, and I'm not really looking forward to it since I would be way over my head. It would be really nice if someone else had a code or knew how to make it.
Joshua

User avatar
bitfogav
Moderator
Moderator
Posts: 908
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Code for driving Matrices with 595 shift registers Ardui

Post by bitfogav » Sun Sep 16, 2012 7:52 pm

saimaster13 wrote:

Code: Select all

int two[8] = {0x1c, 0x22, 0x22, 0x02, 0x04, 0x08, 0x10, 0x3f}; //set two[] to contain the hexadecimal values needed to make the character "2"
The code above is perfect, what you need is some sort of main code that loops through each array index, and then enable the 74595 outputs, and lastly enable the matrix Cathodes to display that single array index onto the matrix. you will need to do this for each 8 parts of the array.

The Cathode data might look something like this:
Look closely at each part of this array and you can see a pattern? each 0 in the Binary code below will turn on a row/column of leds on the matrix.

Code: Select all

//(This is only an example)
Int CathodeData[8] = {B11111110, B11111101, B11111011, B11110111, B11101111, B11011111, B10111111, B01111111};
So your using two 74595's, one for the Anodes and one for the Cathodes.

For an example a For Loop which cantains something like this below.

Code: Select all

for (int i = 0 ; i < 8; i++ ){
  1. Send first index to Anode 74595 (Anode data).
  2. Enable the Anode 74595, Outputs the data on the 74595. 
  3. Send the first index from the CathodeData to the Cathode 74595.
  4. Enable the Cathode 74595.
  5. Small delay 1 ms?.
  6. Disable the Cathode 74595 and Anode 74595
  7. You may need to send B1111111 to the Cathode 74595 to make sure nothing is now displayed on the matrix. }
If you need me to explain what I mean by any of the above then just ask :)
If you don't know what Voltage your country is using, you shouldn't be doing electronics ;-)

User avatar
brad
Site Admin
Site Admin
Posts: 2576
Joined: Fri Mar 26, 2010 10:30 pm

Re: Code for driving Matrices with 595 shift registers Ardui

Post by brad » Sun Sep 16, 2012 9:04 pm

I haven't delved to far into the arduino world (I will be more soon though)

I am not sure of the exact code but you need to run a loop (like bitfogav was suggesting) that will send each bit out, display it (once eight bits have been sent), reset it and start over again.

Since the data is contained in bytes, you need to single out each bit in those bytes to send to one port pin.

In Basic I would do it like this:

If I had a byte named MyByte and it contained 00110101, I could send the data to one pin, Lets say PORTA pin 0, latch that data in (or shift it in) and then repeat the process until all eight bits are in there:

Code: Select all

for x = 0 to 7
     porta.bits(x) = MyByte.bits(x)
     latch = 1
     latch = 0
next

User avatar
Saimaster13
I practically live here!
I practically live here!
Posts: 176
Joined: Mon Aug 13, 2012 4:23 am
Location: Sarasota, Florida

Re: Code for driving Matrices with 595 shift registers Ardui

Post by Saimaster13 » Sat Sep 29, 2012 1:13 pm

Finally got it working. If anyone is interested in the code, I will clean it up and post it.
Joshua

User avatar
bitfogav
Moderator
Moderator
Posts: 908
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Code for driving Matrices with 595 shift registers Ardui

Post by bitfogav » Sat Sep 29, 2012 8:07 pm

I'm certainly interested in seeing your final code and how you done it, and maybe a picture or video of the matrix? :)
If you don't know what Voltage your country is using, you shouldn't be doing electronics ;-)

User avatar
brad
Site Admin
Site Admin
Posts: 2576
Joined: Fri Mar 26, 2010 10:30 pm

Re: Code for driving Matrices with 595 shift registers Ardui

Post by brad » Mon Oct 01, 2012 12:04 am

Count me in on being an interested party!

User avatar
Saimaster13
I practically live here!
I practically live here!
Posts: 176
Joined: Mon Aug 13, 2012 4:23 am
Location: Sarasota, Florida

Re: Code for driving Matrices with 595 shift registers Ardui

Post by Saimaster13 » Thu Oct 04, 2012 12:27 pm

Been busy lately, but I got the code ready to show.

The circuit:
This code uses 2 595 shift registers to drive an 8x8 LED matrix. The shift registers are chained together and the first one drives the anodes, the second drives the cathodes. If you do not know how to hook up the arduino and shift registers to an LED matrix, go to this site Arduino ShiftOut website but be sure to hook up the red LED shift register to the anodes and the green to the cathodes.


If you get a backwards or upside down image or something, please tell me, I have personally had a lot of problems with it, but I should be able to fix it (my main problem is that I am not sure how to hook up an LED matrix properly.)

Circuit explanation:
The circuit anodes are scanned one at a time (using the 0b00000001, 0b00000010 type data, thanks bitfogav!) and for each anode row, 8 cathodes are set to be on or off which, after all the anode columns are scanned, makes an image. The bits of the Cathode data string is actually altered. The 1s are turned to 0s and the 0s to 1s to make coding easier, and the image is mirrored (because my display showed a mirrored image when I hooked it up.) And I believe that is mostly what is going on, although I do not completely understand the ShiftOut portion of the code.


Good luck, I hope something I wrote makes sense.

I welcome any type of suggestions.


Code: Select all

//Original unmodified code found here: http://arduino.cc/en/Tutorial/ShftOut23
//Found on arduino ShiftOut tutorial website: http://arduino.cc/en/Tutorial/ShiftOut
//modified by Joshua Little



int latchPin = 2; //595 shift register pins
int clockPin = 0;
int dataPin = 1;

byte dataAnode;  //used to store individual values of the anode strings
byte dataCathode1; //used to store individual values of the cathode strings
byte anodeString[9] =    {0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000}; //scans the anode rowns
byte cathode1StringRaw[9] = {0x3c, 0x66, 0x22, 0x22, 0x7e, 0x22, 0x22, 0x22}; //the unaltered data for the matrix, put your desired data here (default is an "A" written in hexadecimal)
byte cathode1String[9]; // converted data for the matrix, 0s and 1s are switched and the image is morrored ( i believe)

void setup() {
  
  pinMode(latchPin, OUTPUT); //sets shift register pins as output
  pinMode(clockPin, OUTPUT);
    pinMode(dataPin, OUTPUT);
}

void loop() {
matrixDisplay(); //the matrix function
}



void matrixDisplay(){ //writes cathode1String[9] to display, code taken from ShiftOut on arduino website http://arduino.cc/en/Tutorial/ShiftOut
  binaryBitInverter(); //changes cathode 0s to 1s and 1s to 0s, also mirrors the image

  for (int j = 0; j < 8; j++) {
    //load the light sequence you want from array
    dataAnode = anodeString[j];
    dataCathode1 = cathode1String[j];
    //ground latchPin and hold low for as long as you are transmitting
    digitalWrite(latchPin, 0);
    //move 'em out
    shiftOut(dataPin, clockPin, dataAnode);   
    shiftOut(dataPin, clockPin, dataCathode1);
    //return the latch pin high to signal chip that it 
    //no longer needs to listen for information
    digitalWrite(latchPin, 1);
  }
}

void shiftOut(int myDataPin, int myClockPin, byte myDataOut) { //'595 matrix setup code, code taken from ShiftOut on arduino website http://arduino.cc/en/Tutorial/ShiftOut
  // This shifts 8 bits out MSB first, 
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut�
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights. 
  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result 
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000 
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {	
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}


void binaryBitInverter(){  //inverts each cathode bit and flips them
 for(int x= 0; x < 8; x++){
    for(int y = 0; y < 8; y++){
      if(bitRead(cathode1StringRaw[x], y) == 0){ //if string raw bit is low, it sets it high and vice-versa
        bitSet(cathode1String[x], (7-y));  // the (7-y) flips the bits
      }
      else{
        bitClear(cathode1String[x], (7-y));
      }
    }
 }
}
Joshua

User avatar
brad
Site Admin
Site Admin
Posts: 2576
Joined: Fri Mar 26, 2010 10:30 pm

Re: Code for driving Matrices with 595 shift registers Ardui

Post by brad » Fri Oct 05, 2012 8:28 am

Nice work and thanks for sharing the code!

If you like, I can modify my graphic converter program to output array data to suit an arduino. This program lets you import a graphic (which could be a picture or just a graphic containing text etc... The program will then convert it into array data for you to paste into your arduino code. At the moment I have made it for swordfish basic but I can modify it to suit arduino. Here's a couple of screenshots:

This first one shows a sonic graphic converted to 3-bit colour.
screen-capture-4.png
screen-capture-4.png (45.39 KiB) Viewed 8432 times
This second one shows a long string of text (again converted to 3-bit colour) which is ideal for using with an 8x8 matrix and then scrolling the text across. It also shows the pop-up that shows when conversion is complete (it will copy all the array data to the clipboard for you to then paste into you code.
screen-capture-6.png
screen-capture-6.png (27.41 KiB) Viewed 8432 times
It takes the hassel out of trying to convert an image or text by typing them out in hex or bin.

User avatar
Saimaster13
I practically live here!
I practically live here!
Posts: 176
Joined: Mon Aug 13, 2012 4:23 am
Location: Sarasota, Florida

Re: Code for driving Matrices with 595 shift registers Ardui

Post by Saimaster13 » Fri Oct 05, 2012 9:37 am

Uhh... that's looks pretty cool, but I do not think I am that far yet, I still don't know how to add a 3rd shift register to the code.
Joshua

User avatar
brad
Site Admin
Site Admin
Posts: 2576
Joined: Fri Mar 26, 2010 10:30 pm

Re: Code for driving Matrices with 595 shift registers Ardui

Post by brad » Sat Oct 06, 2012 9:37 pm

It will come in handy (one day)! Notice how there are three check boxes - RED GREEN BLUE. This allows you to select what colours to export to the data array.

If you wanted a simple little 8x8 graphic which was one colour, you would draw it up in paint (like this):
screen-capture-7.png
screen-capture-7.png (7.81 KiB) Viewed 8417 times
Then open the file in my conversion program, tick the red checkbox and then convert (it will scan each pixel and convert it to binary array data):
screen-capture-8.png
screen-capture-8.png (23.67 KiB) Viewed 8417 times
Finally, you just have to paste the data into your code!

Code: Select all

const RedData(8) as byte = (%01111110,%10000001,%10100101,%10000001,%10100101,%10011001,%10000001,%01111110)
Here's the same data but line by line (can you see the face...)

Code: Select all

const RedData(8) as byte = (
%01111110,
%10000001,
%10100101,
%10000001,
%10100101,
%10011001,
%10000001,
%01111110)

This is especially useful for big long graphics or text!

User avatar
Saimaster13
I practically live here!
I practically live here!
Posts: 176
Joined: Mon Aug 13, 2012 4:23 am
Location: Sarasota, Florida

Re: Code for driving Matrices with 595 shift registers Ardui

Post by Saimaster13 » Sun Oct 07, 2012 1:22 am

Can it convert into Hex too? Anyway, that looks pretty cool! Much much simpler than my drawing in paint then manually converting it. It'd be great to have it for the Arduino!
Joshua

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests