Shop OBEX P1 Docs P2 Docs Learn Events
Program BS2 and more then three 74HC595's — Parallax Forums

Program BS2 and more then three 74HC595's

KKDZ28KKDZ28 Posts: 10
edited 2011-02-08 06:40 in BASIC Stamp
I have a project I want to use many serial shift registers. I see the info in stamp works but Im getting confused one how to chain more then two together. Im still trying to learn the syntax of the coding. I have been trying to look at example code to see how its set. Can not find code that has more then 2 74HC595's. I want to use about 5 or 6 on one project and many many more on another. I cant get past doing two right now. The project I want to do for now to learn the basics is just a 32-64 LED sweep in one direction. I figured it would be easy and I would learn the code and how it works to move to bigger projects. I want to run about 5 chains of 32-64 LEDs in each chain. Im missing something and getting frustrated with myself. Can anyone help me out? How do I program more then two 595's? I have three hooked up now but the 3rd just does what the 1st one is doing. Im wiring the 3rd just like the 2nd one is off the 1st. I know Im missing something easy but I just need someone to bring it to light for me. How do I tell it I have that many 595's in the chain? How do you tell it about the other chains? Take 3 more pins and set clock2, SerData2 and latch2? Sorry for so many question but this as got me frustrated. I have serached the Forums and online but can not find the answers or example code to try to figure it out.

Thanks

Comments

  • bsnutbsnut Posts: 521
    edited 2011-01-25 01:52
    Ok, let me point you in the direction you need to go.

    The reason why you see only two together, is the Basic Stamp can only do Word size variables. You, need to look at the Basic Stamp Editor help file for SHIFTOUT command it will tell you what size the variable can be.

    You also need to get the data sheet for this shift register and it should show you how to connect more then two together.

    I will do some research on this as well, because I have seen it somewhere and suggest you to do the same thing.
  • bsnutbsnut Posts: 521
    edited 2011-01-25 06:44
    I found this on PICAXE forum and it was posted by Dr Acula and posts in this forum site as well. Here is the link there forum site for this post
    www.picaxeforum.co.uk/showthread.php?t=9116
    This circuit uses two serial to parallel chips (74HC595) to expand the number of outputs. It takes 3 outputs and converts to 16 outputs, and is also expandable to any arbitrary number and will work on any picaxe. This circuit is useful for driving two seven-segment displays or any task where lots of outputs are needed. The QH output (pin 9) can be cascaded to pin 14 of the next chip. All the bits are fed out one at a time and then the data is latched to the output pins.

    ' dual HC595 routine - 16 digital outputs
    ' uses any picaxe - pins 0,1,2
    ' pin0= latch clock HC595 leg 12 - low to high transition latches the data
    ' pin1 = serial data out = HC595 leg 14
    ' pin2= shift clock = HC595 leg 11 - low to high tranition stores the serial data out
    ' HC595 goes from left one to right one. Left one is MSB = the left 8 bits of the w0 word
    ' HC595 leg 11s in parallel
    ' HC595 leg 12s in parallel
    ' HC595 leg 9 of left one is the SQH bit output and goes into leg 14 of the right HC595
    ' HC595 reset legs 10 are both high
    ' HC595 output enable legs 13 are both low
  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-25 08:11
    To expand on bsnut's comments:

    You can attach more than two 74HC595s to a BS2. You just keep adding more on the end of the chain, just like the 2nd one. The SHIFTOUT statement can handle multiple variables, one for each 1 or 2 74HC595s. You can also have multiple chains of them. Now the caveats:

    1) As the chains get longer, it takes more time to load them up. The Stamp Manual gives the approximate clock frequency used.

    2) As you get much beyond 8 or 10 74HC595s in a chain, you may need buffering of the clock and load signals.

    3) Each chain requires at least 3 I/O pins (data, clock, load). The reset and output enable signals are often not used and tied to Vdd or Vss as appropriate. There are only 16 I/O pins.

    4) Remember that the Stamps have only 26 bytes of variables. You need somehow / somewhere to keep the data you're going to send out to the 74HC595s. You could compute it on-the-fly a byte at a time or read it in from some kind of external storage, but that does slow things down even further.
  • KKDZ28KKDZ28 Posts: 10
    edited 2011-01-27 18:01
    Thanks for taking the time to reply. I have been looking at the examples in Stamp Works and in the Basic Stamp Manual. Still not sure on the Syntax for the code. I see all over the place for only two 595's but can not find example code for more then two. What would it look like for say 5 of the 74HC595's or even 10 or 100? I can see how to wire it so that part is good. Its the code that has me stuck. On my test board I have just three wired right now. They all do the same thing. Chase in one direction then reset and do again. I want it to chase all the way down in a single chase line to each 595. Stamp works shows one way but the manual shows different coding. Does each 595 need a VAR? Not sure how that works down for the shiftout. Here is what the code Im trying.
    I know Im missing a lot but any help you can give to help would be great. Samples help me a lot to see how it works. I have over 60 of the 595 waiting to go into this project. Please help. :)
    Quick video too.
    th_VID00002-20110127-1851.jpg
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    [ Program Description ]

    '
    [ I/O Definitions ]
    Clock PIN 0 ' shift clock (74HC595.11)
    SerData PIN 1 ' serial data (74HC595.14)
    Latch PIN 2 ' output latch (74HC595.12)

    '
    [ Constants ]
    DelayTime CON 50

    '
    [ Variables ]
    patt VAR Word
    cycle VAR Nib

    '
    [ Initialization ]
    Reset:
    LOW Latch ' make output and low

    '
    [ Program Code ]
    Main:
    FOR cycle = 0 TO 5
    GOSUB Chase:
    NEXT
    PAUSE DelayTime ' hold
    GOTO Main

    Chase:
    patt = 1
    DO
    GOSUB Star ' put pattern on 74x595
    PAUSE DelayTime ' hold
    patt = patt << 1 ' shift pattern left
    LOOP UNTIL (patt = 32768)
    RETURN




    '
    [ Subroutines ]
    Star:
    SHIFTOUT SerData, Clock, mSBFIRST, [patt] ' send pattern to 595-1
    SHIFTOUT SerData, Clock, MSBFIRST, [patt] ' send pattern to 595-2
    SHIFTOUT SerData, Clock, MSBFIRST, [patt] ' send pattern to 595-3
    PULSOUT Latch, 5 ' latch outputs
    RETURN
  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-27 19:43
    You can use separate SHIFTOUT statements as you've shown or you can list more then one variable in the variable list. Normally each variable supplies 8 bits although you can override that such that, if you have a word variable, you can send up to 16 bits. See the manual chapter on SHIFTOUT for details. The main reason you don't see coding examples for more than one or two 74HC595s is that it's just more of the same (bits). Nothing special.
  • bsnutbsnut Posts: 521
    edited 2011-01-28 01:20
    I am going to expand on Mike's comment with some code.
    3) Each chain requires at least 3 I/O pins (data, clock, load). The reset and output enable signals are often not used and tied to Vdd or Vss as appropriate. There are only 16 I/O pins
    What, I suggest is make some minor change in the code that you have. This code should give you some idea what we saying.
    ' -----[ Initialization ]--------------------------------------------------
    Reset:
    LOW Latch ' make output and low
    LOW Latch1
    LOW Latch2
    
    ' -----[ Program Code ]----------------------------------------------------
    Main:
    GOSUB Chase
    GOSUB Chase1
    GOSUB Chase2
    PAUSE DelayTime ' hold
    GOTO Main
    
    Chase:
    patt = 1
    DO
    patt = patt << 1 ' shift pattern left
    GOSUB Star ' put pattern on 74x595
    PAUSE DelayTime ' hold
    LOOP UNTIL (patt = 16)
    RETURN
    
    Chase1:
    patt = 1
    DO
    patt = patt << 1 ' shift pattern left
    GOSUB Star1 ' put pattern on 74x595
    PAUSE DelayTime ' hold
    LOOP UNTIL (patt = 16)
    RETURN
    
    Chase2:
    patt = 1
    DO
    patt = patt << 1 ' shift pattern left
    GOSUB Star2 ' put pattern on 74x595
    PAUSE DelayTime ' hold
    LOOP UNTIL (patt = 16)
    RETURN
    
    ' -----[ Subroutines ]-----------------------------------------------------
    Star:
    SHIFTOUT SerData, Clock, mSBFIRST, [patt] ' send pattern to 595-1
    PULSOUT Latch, 5 ' latch outputs for LEDS 1 - 16
    
    Star1:
    SHIFTOUT SerData1, Clock1, MSBFIRST, [patt] ' send pattern to 595-2
    PULSOUT Latch1, 5 ' latch outputs for LEDS 17 - 32
    
    Star2:
    SHIFTOUT SerData2, Clock2, MSBFIRST, [patt] ' send pattern to 595-3
    PULSOUT Latch2, 5 ' latch outputs for LEDS 33 - 48
    RETURN
    
    You will also need to add I/O pins to your code like this to make the subroutine code work.
    ' -----[ I/O Definitions ]-------------------------------------------------
    Clock PIN 0 ' shift clock (74HC595.11) for LEDS 1 - 16
    SerData PIN 1 ' serial data (74HC595.14) for LEDS 1 - 16
    Latch PIN 2 ' output latch (74HC595.12) for LEDS 1 - 16
    Clock1 PIN 3 ' shift clock (74HC595.11) for LEDS 17 - 32
    SerData1 PIN 4 ' serial data (74HC595.14) for LEDS 17 - 32
    Latch1 PIN 5 ' output latch (74HC595.12) for LEDS 17 - 32
    Clock2 PIN 6 ' shift clock (74HC595.11) for LEDS 33 - 48
    SerData2 PIN 7 ' serial data (74HC595.14) for LEDS 33 - 48
    Latch2 PIN 8 ' output latch (74HC595.12) for LEDS 33 - 48
    

    This should get you going in the right direction
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-01-28 17:13
    If we turn to StampWorks (version 2), Experiment #23, we see that Q_h_n feeds Data_in_n+1. So, Q_h n+1 is conn'd to Data_in_n+2. Then, instead of shifting out two bytes you shiftout three bytes. They share clock and latch.


    (Not having subscript as a 'button' just totally sux for me.)
  • bsnutbsnut Posts: 521
    edited 2011-01-29 01:25
    Why did'nt I think of that PJ has hit the nail on head.
  • KKDZ28KKDZ28 Posts: 10
    edited 2011-01-29 13:32
    I tried that code on my sample circuit and got this????

    th_VID00003-20110129-1411.jpg

    Code was this:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    [ Program Description ]

    '
    [ I/O Definitions ]
    Clock PIN 0 ' shift clock (74HC595.11) for LEDS 1 - 16
    SerData PIN 1 ' serial data (74HC595.14) for LEDS 1 - 16
    Latch PIN 2 ' output latch (74HC595.12) for LEDS 1 - 16
    Clock1 PIN 3 ' shift clock (74HC595.11) for LEDS 17 - 32
    SerData1 PIN 4 ' serial data (74HC595.14) for LEDS 17 - 32
    Latch1 PIN 5 ' output latch (74HC595.12) for LEDS 17 - 32
    Clock2 PIN 6 ' shift clock (74HC595.11) for LEDS 33 - 48
    SerData2 PIN 7 ' serial data (74HC595.14) for LEDS 33 - 48
    Latch2 PIN 8 ' output latch (74HC595.12) for LEDS 33 - 48

    '
    [ Constants ]
    DelayTime CON 75

    '
    [ Variables ]
    patt VAR Word

    '
    [ Initialization ]
    Reset:
    LOW Latch ' make output and low
    LOW Latch1
    LOW Latch2

    '
    [ Program Code ]
    Main:
    GOSUB Chase
    GOSUB Chase1
    GOSUB Chase2
    PAUSE DelayTime ' hold
    GOTO Main

    Chase:
    patt = 1
    DO
    patt = patt << 1 ' shift pattern left
    GOSUB Star ' put pattern on 74x595
    PAUSE DelayTime ' hold
    LOOP UNTIL (patt = 16)
    RETURN

    Chase1:
    patt = 1
    DO
    patt = patt << 1 ' shift pattern left
    GOSUB Star1 ' put pattern on 74x595
    PAUSE DelayTime ' hold
    LOOP UNTIL (patt = 16)
    RETURN

    Chase2:
    patt = 1
    DO
    patt = patt << 1 ' shift pattern left
    GOSUB Star2 ' put pattern on 74x595
    PAUSE DelayTime ' hold
    LOOP UNTIL (patt = 16)
    RETURN

    '
    [ Subroutines ]
    Star:
    SHIFTOUT SerData, Clock, MSBFIRST, [patt] ' send pattern to 595-1
    PULSOUT Latch, 5 ' latch outputs for LEDS 1 - 16

    Star1:
    SHIFTOUT SerData1, Clock1, MSBFIRST, [patt] ' send pattern to 595-2
    PULSOUT Latch1, 5 ' latch outputs for LEDS 17 - 32

    Star2:
    SHIFTOUT SerData2, Clock2, MSBFIRST, [patt] ' send pattern to 595-3
    PULSOUT Latch2, 5 ' latch outputs for LEDS 33 - 48
    RETURN

    Any more code ideas?
  • bsnutbsnut Posts: 521
    edited 2011-01-30 01:20
    Did the code work out for you? What are you trying to chase one LED after another?

    This is from the Basic Stamp Editor Help file gives you idea about the SHIFTOUT command.
    Here is a simple example:

    SHIFTOUT 0, 1, MSBFIRST, [250]

    Here, the SHIFTOUT command will write to I/O pin 0 (the Dpin) and will generate a clock signal on I/O 1 (the Cpin). The SHIFTOUT command will generate eight clock pulses while writing each bit (of the 8-bit value 250) onto the data pin (Dpin). In this case, it will start with the most significant bit first as indicated by the Mode value of MSBFIRST.

    By default, SHIFTOUT transmits eight bits, but you can set it to shift any number of bits from 1 to 16 with the Bits argument. For example:

    SHIFTOUT 0, 1, MSBFIRST, [250\4]

    Will output only the lowest (rightmost) four bits (%1010 in this case). But what if you want to output the leftmost bits of a given value? By adding the right-shift operator (>>) to the code you can adjust the output as required:

    SHIFTOUT 0, 1, MSBFIRST, [(250 >> 2)\6]

    Will output the upper six bits (%111110 in this case).

    This what PJ Allen was talking about and it is done on one line.
    Some devices require more than 16 bits. To solve this, you can use a single SHIFTOUT command with multiple values. Each value can be assigned a particular number of bits with the Bits argument. As in:

    SHIFTOUT 0, 1, MSBFIRST, [250\4, 1045\16]

    The preceding example will first shift out four bits of the number 250 (%1010) and then 16 bits of the number 1045 (%0000010000010101). The two values together make up a 20 bit value.

    In the examples above, specific numbers were entered as the data to transmit, but, of course, the SHIFTOUT command will accept variables and expressions for the OutputData and even for the Bits argument
  • KKDZ28KKDZ28 Posts: 10
    edited 2011-01-30 09:42
    Yes, Im trying to chase one LED after another. Once I had that I wanted to play with PWM to add a fade trail or something but just doing the chase one LED after another has had be stuck and have not been able to move on yet. Im trying to add some shooting stars to a fiber optic ceiling I did for a friends nursery. Baby is coming in about a week so Im trying to finish before then. I still need to etch and build the boards for each shooting star. This is my gift to the baby.

    Im stuck on the syntax of how to get it to transfer to each 74HC595 to chase one LED down the full length of LEDs. It could be 24 for one and maybe 48-64 for another one or even more. I cant get it over 16 right now. In my 1st video the 16 will chase but the 3rd 74HC595 mirrors the 1st one.

    Can anyone show me the code for doing this many? Stampworks said I can chain dozens of 74HC595's to just 3 I/O pins. Yeah, I can but just cant figure how to trace one LED at a time down them all.

    Baby is coming soon so I dont have much time left. Any help would be great on the code.

    Thanks for the time you guys have given to help me so far.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-01-30 12:06
    All this panic.
    I think StampWorks should have stayed with Bytes in the example, instead of going the \16 route, but you're black-boxing the PBASIC.
    Well, I took a different tack from StampWorks's. I wrote modules to emphasize concepts, no shortcuts.

    Do you see, programming-wise, what's going on?

    PE - added pic
    563 x 403 - 28K
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-02-01 17:54
    Sporadic and/or non-existent follow-up
    Stink-a-roo!!
  • doggiedocdoggiedoc Posts: 2,240
    edited 2011-02-01 18:32
    Is this what you're trying to accomplish? I did something that I think you are describing with cascaded 74xxx164 serial in parallel out shift registers controlled by a BS2 program.

    Below is video and code.


    Here's the code I wrote for it back in 2005 -
    ' [----------------------------[ doggiedoc ]-----------------------------------]
    '
    '   File...... LEDsequencer.bs2
    '   Purpose... flashes LEDs in sequence
    '   Author.... Paul A. Willoughby
    '   E-mail.... doc@tcah.com
    '   Started... 9/05/2005
    '   Updated... 8/12/2007
    '
    '   {$STAMP BS2}
    '   {$PBASIC 2.5}
    '
    '   CREDITS...
    ' [----------------------------------------------------------------------------]
        'I really have to thank everyone on the Parallax forums, especially
        'Jon Williams for the help and ideas on using binary to break down
        'button inputs.  Original schematics for expandble LED sequencer
        'from Bill Bowden - circa 1998.
    
    
    ' [----------------------------------------------------------------------------]
    '                        [-- Program Description --]
    ' [----------------------------------------------------------------------------]
    '   This program pulses clock in data in on cascaded 74xxx164 serial in
    '   parallel out shift registers.  My original design is for 6 chips
    '   sequencing 48 LEDs, but the number of LEDs is dependent on number of ICs
    '   cascaded together. I haven't tried more than 6 at once.
    
    
    
    
    ' [----------------------------------------------------------------------------]
    '                          [-- Revision History --]
    ' [----------------------------------------------------------------------------]
    '   8/12/2007 - Revised constants and adjusted increments from 1 to 2 on
    '               delay loops and number of LEDs.
    
    
    
    ' [----------------------------------------------------------------------------]
    '                          [-- I/O  Definitions --]
    ' [----------------------------------------------------------------------------]
    BtnInputs         VAR   IND   'pins 12,13,14,15 to read button inputs
    
    
    
    ' [----------------------------------------------------------------------------]
    '                             [-- Constants  --]
    ' [----------------------------------------------------------------------------]
    LEDCount          CON   48 'number of LEDs (8 outputs per 74xxx165 IC)
    ClockPin          CON   8  'physically connected to pin 8
    MasterResetPin    CON   9  'pin 9
    DataPin           CON   1  'pin 1
    
    '
    
    
    
    
    ' [----------------------------------------------------------------------------]
    '                             [-- Variables  --]
    ' [----------------------------------------------------------------------------]
    
    counter           VAR   Word
    flashDelay        VAR   Byte
    theButtons        VAR   Nib
    btnCounter        VAR   Nib
    numLEDsOn         VAR   Byte
    
    
    ' [----------------------------------------------------------------------------]
    '                            [-- EEPROM  Data --]
    ' [----------------------------------------------------------------------------]
       'none
    
    
    
    ' [----------------------------------------------------------------------------]
    '                           [-- Initialization --]
    ' [----------------------------------------------------------------------------]
    'DEBUG CLS, "Program Running!"
    LOW MasterResetPin       ' clear all registers on IC
    HIGH DataPin             ' Set initial data output to 1
    HIGH MasterResetPin      ' enable all outputs on IC
    flashDelay = 10
    numLEDsOn = 1
    
    
    
    
    
    ' [----------------------------------------------------------------------------]
    '                            [-- Program Code --]
    ' [----------------------------------------------------------------------------]
    
    Main:
    
        DO
    
          GOSUB Get_Buttons
             'DEBUG CLS, "Inputs = ", IBIN4 theButtons     'show inputs through
                                                           'each loop to ensure
                                                           'Debounced properly
          SELECT theButtons
            CASE %1000
              GOSUB Go_Faster
            CASE %0100
              GOSUB Go_Slower
            CASE %0010
              GOSUB Set_numLEDsUp
            CASE %0001
              GOSUB Set_numLEDsDn
            ENDSELECT
    
          FOR counter = 1 TO (LEDCount / numLEDsOn)
            TOGGLE ClockPin
            PAUSE flashDelay
            LOW DataPin      'physically connected to pin 1
            TOGGLE ClockPin
            NEXT
          TOGGLE DataPin
    
         LOOP
      END
    
    
    
    ' [----------------------------------------------------------------------------]
    '                            [-- Subroutines  --]
    ' [----------------------------------------------------------------------------]
    
    Get_Buttons:
      theButtons = %0000
      FOR btnCounter = 1 TO 5
          theButtons = ~theButtons & BtnInputs
      NEXT
      RETURN
    
    Go_Faster:
        IF (flashDelay <= 0) THEN
            flashDelay = 0
        ELSE:
            flashDelay = (flashDelay - 2)
        ENDIF
      RETURN
    
    Go_Slower:
        IF (flashDelay >= 255) THEN
            flashDelay = 255
        ELSE:
            flashDelay = (flashDelay + 2)
        ENDIF
      RETURN
    
    Set_numLEDsUp:
        IF (numLEDsOn >= LEDCount) THEN
          numLEDsOn = LEDCount
        ELSE:
          numLEDsOn = (numLEDsOn + 1)
        ENDIF
      RETURN
    
    Set_numLEDsDn:
        IF (numLEDsOn <= (LEDCount / LEDCount)) THEN
          numLEDsOn = 1
        ELSE:
          numLEDsOn = (numLEDsOn - 1)
        ENDIF
      RETURN
    
  • KKDZ28KKDZ28 Posts: 10
    edited 2011-02-02 06:48
    Sorry for the late update. These -20 below temps had me busy with other things. I finally had time to test and that is what Im trying to do. Thank you so much for taking the time to show me this. I will make a post of the completed project when done. Im still playing with the code. Tried with four 74HC595's and once I added another shiftout for that it was good. Does it load the last 595 first like 595-4, 595-3, 595-2, 595-1? Im running a random command to delay the chases. Works great but had to add it to the 1st set of shiftout lines.

    I tried with breaking into two different lines too. 595a-1, 595a-2 and 595b-1, 595b-2. Both work independent taking 3 pins each set. Now I can start building my boards with more 595's!

    Thank you very much PJ Allen for the info. I think I will have some more questions later for everyone but you cleared up many things for me.
    Doggiedoc thanks for that info as well. That will help for my 2nd stage of the project.
    Thanks Mr Green and BSNut. Its nice to see code from other people.
    Wealth of knowledge on this forum. This programming peon knows .0001% of it now, maybe less.

    OH, how are you embedding code in those windows like that? So much clearer on the forum.

    Thanks
    Kris
  • doggiedocdoggiedoc Posts: 2,240
    edited 2011-02-05 05:07
    KKDZ28 wrote: »
    OH, how are you embedding code in those windows like that? So much clearer on the forum.

    You have to wrap the code in the code tags [noparse]
    ...
    
    [/noparse].
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-02-05 05:35
    Follow that picaxe thread - others chimed in and significantly decreased the size of the code. I guess the fundamental principle is you build some hardware with a whole series of 595s daisy chained, then you write some software to send out a byte, then you call that subroutine many times to send out many bytes.

    Can you post a schematic (just to double check it is ok) and post the latest code?

    addit - 'tis ok with the delayed postings. I see things have been rather cold in your domicile.
  • KKDZ28KKDZ28 Posts: 10
    edited 2011-02-08 06:40
    Finished up the fiber optic ceiling but still need to built the boards for the chaser LED's. I will post up the schematic and code soon. I know there is a better way of doing this but Im still learning. More to come.
Sign In or Register to comment.