Shop OBEX P1 Docs P2 Docs Learn Events
74hc164 Schematic & Sample Code — Parallax Forums

74hc164 Schematic & Sample Code

everesteverest Posts: 141
edited 2009-09-03 06:05 in BASIC Stamp
Hello,

I need to free up some pins from my Stamp2 application. After doing some digging and research it appears that the simplest way to do this is via a 74hc164 for inputs and a 74HC595 for outputs. I dug and for the life of me I can't seem to find a simple example of how to build a circuit along with sample code. I just know there is a Nuts & Volts article out there waiting that covers this. . .I found one, but it looks like they are using a totally separate PIC microcontroller and I'd like to avoid that if I can.

Any suggestions/pointers?

-JEff

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2009-08-30 21:13
    For inputs, it's a 74HC165.· For outputs, it's a 74HC595.··StampWorks (<-- hot-link!) has the information you require.
  • dev/nulldev/null Posts: 381
    edited 2009-08-30 21:37
    74HC595

    Schematics (with 8 LED's connected):
    595.jpg

    Code:
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    
      pHCIO         CON     0                        ' 74HC595 data
      pHCCLK        CON     1                        ' 74HC595 clock
      pHCLtc        CON     2                        ' 74HC595 latch
    
    Main:
      HCBits = %01000100
      GOSUB WriteHC
      END
    
    WriteHC:
      SHIFTOUT pHCIO, pHCCLK, MSBFIRST, [noparse][[/noparse]HCBits]
      PULSOUT pHCLtc, 1
      PAUSE 50
      RETURN
    
    



    74HC165

    Schematics (with 8 switches):
    165.jpg

    Code:
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    ' This program demonstrates a simple method of turning three BASIC Stamp
    ' I/O pins into eight digital inputs with a 74HC165 shift register.
    
    ' -----[noparse][[/noparse] I/O Definitions ]-------------------------------------------------
    Clock PIN 0 ' shift clock (74HC165.2)
    SerData PIN 1 ' serial data (74HC165.7)
    Load PIN 2 ' output latch (74HC165.1)
    
    ' -----[noparse][[/noparse] Constants ]-------------------------------------------------------
    DelayTime CON 100
    
    ' -----[noparse][[/noparse] Variables ]-------------------------------------------------------
    switches VAR Byte ' switch data
    
    ' -----[noparse][[/noparse] Initialization ]--------------------------------------------------
    Reset:
        HIGH Load ' make output and high
        DEBUG CLS,
        "Switches 76543210", CR,
        "-------- --------", CR,
        "Status ........"
    
    ' -----[noparse][[/noparse] Program Code ]----------------------------------------------------
    Main:
        DO
            GOSUB Get_165 ' get switch inputs
            DEBUG CRSRXY, 10, 2, BIN8 switches ' display current status
            PAUSE DelayTime ' pad the loop a bit
        LOOP
    
    ' -----[noparse][[/noparse] Subroutines ]-----------------------------------------------------
    Get_165:
        PULSOUT Load, 5 ' load switch inputs
        SHIFTIN SerData, Clock, MSBPRE, [noparse][[/noparse]switches] ' shift them in
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • dev/nulldev/null Posts: 381
    edited 2009-08-30 22:05
    Correction:
    
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    
      pHCIO         CON     0                        ' 74HC595 data
      pHCCLK        CON     1                        ' 74HC595 clock
      pHCLtc        CON     2                        ' 74HC595 latch
      HCBits     VAR Byte
    
    Main:
      HCBits = %01000100
      GOSUB WriteHC
      END
    
    WriteHC:
      SHIFTOUT pHCIO, pHCCLK, MSBFIRST, [noparse][[/noparse]HCBits]
      PULSOUT pHCLtc, 1
      PAUSE 50
      RETURN
    
    



    Also, forgot RETURN statement in the 165 code snippet.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • everesteverest Posts: 141
    edited 2009-08-31 01:13
    Thanks! I found some excellent links that show exactly how this is done. . .looks very straightforward!!

    I do still have one remaining question though. . .is it possible to read the current position of the outputs controlled by the 75HC595? The examples provided and the ones I found show how to write and change the state, but not necessarily how to read the state of all the switches. I really need to be able to do both. . .

    -Jeff

    Post Edited (everest) : 8/31/2009 1:18:24 AM GMT
  • dev/nulldev/null Posts: 381
    edited 2009-08-31 07:44
    No you can't read the switches of a 74HC595. I don't know why you would? You should keep that information in a variable in your Stamp...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • dev/nulldev/null Posts: 381
    edited 2009-08-31 07:45
    If you REALLY need to do that, you could connect a 74HC165 to the 74HC595 and read the switches that way.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • dev/nulldev/null Posts: 381
    edited 2009-08-31 08:36
    All the outputs of the 74HC595 will be LOW at startup, there is no preserved state in the chip, so whenever you send data to it, you will have that info in your Stamp.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • everesteverest Posts: 141
    edited 2009-08-31 12:35
    Ugh. . .okay. That's just going to make the program a bit more complex. I also really need to determine if pin states are reset when I use the "RUN" command to switch program slots. I'm guessing they aren't since that would be a real pain, but I'm away from my Stamps now, so I can't test it. There's going to be a LOT that I'll need to keep track of, so I'm thinking I'll just use Scratch RAM.

    -Jeff
  • dev/nulldev/null Posts: 381
    edited 2009-08-31 12:48
    You can use EEPROM or ScratchPad RAM, your choice. The pins are not reset when you switch program slots. The only time pins are reset is when the power to the 74HC chip is recycled, or you apply a LOW signal to the Reset pin. It's quite easy to use. You keep track of all 8 outputs in a Byte variable, how hard can it be? smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • dev/nulldev/null Posts: 381
    edited 2009-08-31 12:58
    BTW: If you put your state byte in EEPROM you could preserve state when power is recycled.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • kleekrukleekru Posts: 32
    edited 2009-09-03 06:05
    I need to get back to basics on this post if possible... (I'm still in the "What's a Microcontroller" phase of my learning)...

    How would I write Activity #3 from Chapter#3 using· the 74HC165?· Here is the input from the book:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' Whats a Microcontroller - CH3ACT3PushbuttoncontrolledLED.bst
    ' Check· pushbutton state 10 times per second and blink LED when pressed.
    DO
    · DEBUG ? IN3
    · IF (IN3 = 1) THEN
    ··· HIGH 14
    ··· PAUSE 50
    ··· LOW 14
    ··· PAUSE 50
    · ELSE
    ··· PAUSE 100
    · ENDIF
    LOOP

    I have the·attached schematic wired up with my BS2... but frankly I don't know what my input labels will be (IN3 above will be what now that I have· an input going through the 74HC165 chip???).· Am I thinking about this the wrong way?·

    Any advice out there?· Please be specific... and dumb it down for me a bit.

    Thanks!
    Kevin
Sign In or Register to comment.