Shop OBEX P1 Docs P2 Docs Learn Events
Shift Register. A little confused. — Parallax Forums

Shift Register. A little confused.

PoundSign2PoundSign2 Posts: 129
edited 2013-10-12 17:50 in Propeller 1
Hello friends! I would like to incorporate a shift register with my Propeller, however I am unsure how the code should look. All I'm after is to understand how I can use the shift register with the Propeller and how I go about programming it in SPIN. My shift registers are the SN74HC595. Do I set a pin to send the latch high every, for this example, 5 seconds, and send data through the clock within those 5 seconds? I'm just confused how I use this amazing IC.

Comments

  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-10-10 15:35
    Shift registers are easy. Here's a bit of code for you to learn from.
    pub out(value, bits, shiftmode)
    
    '' Output to 74x595 (or multi-chip chain)
    
    
      if (shiftmode == LSBFIRST)                                    ' if lsb first
        value ><= bits                                              '  reverse bit order
    
    
      value <<= (32 - bits)                                         ' prep for msb output
    
    
      ' output bits, MSBFIRST 
      
      repeat bits
        outa[D_PIN] := (value <-= 1) & 1                            ' output a bit
        outa[C_PIN] := 1                                            ' clock the bit
        outa[C_PIN] := 0
    
    
      outa[LATCH] := 1                                              ' latch the outputs
      outa[LATCH] := 0
    


    Note: As I've been (fairly) accused of spoon-feeding newcomers, I'm not doing that any more. You'll need to study this a bit (use the data sheets), and setup your program with appropriate constants.

    BTW... when I worked for Parallax I wrote a book called StampWorks. It has a whole chapter on using the 74x595 -- it would be useful study material.
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2013-10-10 15:44
    Both of these SPI objects were tested specifically with the SN74HC595 and SN74HC165

    SPI Assembly - Serial Peripheral Interface
    http://obex.parallax.com/object/564/
    ·
    SPI Spin - Serial Peripheral Interface
    http://obex.parallax.com/object/566
  • PoundSign2PoundSign2 Posts: 129
    edited 2013-10-10 16:16
    Good stuff! Thank you guys for the help. :smile:
    I'll try to mess with the aforementioned code by JohnnyMac. That really gave me an understanding. Although Do you have to do...
    outa[PIN] := 1
    outa[PIN] := 0 
    


    In order to latch the bit/s? Is there a more, how do I say it...'better'...way to do this? Like auto-latch every so often or once 8 bits have been configured?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-10-10 16:48
    PoundSign2 wrote: »
    Good stuff! Thank you guys for the help. :smile:
    I'll try to mess with the aforementioned code by JohnnyMac. That really gave me an understanding. Although Do you have to do...
    outa[PIN] := 1
    outa[PIN] := 0 
    


    In order to latch the bit/s? Is there a more, how do I say it...'better'...way to do this? Like auto-latch every so often or once 8 bits have been configured?

    On the 74HC595 once the correct number of bits have been shifted out (8 bits per device, which can be daisy-chained) the latch line must be pulsed. That code effectively does that. On a 74HC165 you would pulse the latch line prior to reading the bits in.
  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-10-10 17:33
    Following up on Chris's point, the beauty of the 74x595 -- unlike some shift registers -- is that the outputs do not ripple when you're shifting bits. Your new bits are captured by an internal register, the latch pulse transfers this holding register to the outputs. This is especially helpful when using multiple devices; the code snippet I shared will handle up to 32 bits (four 74x595s in daisy-chain configuration).

    BTW.. before you call that code snippet, make sure you set the clock and latch pins to output/low state. The data pin should be an input.
  • cavelambcavelamb Posts: 720
    edited 2013-10-10 19:25
    Correct me if I'm wrong here, but the real challenging part of shift registers is setting a bit in an
    8-bit byte before shifting the 8 bits out?
  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-10-10 20:40
    Note that challenging -- this is bit of code out of my standard programming template.
    pub rd_bit(target, pos)
    
    '' returns bit value (0..1) of target.pos
    
    
      if ((pos => 0) and (pos =< 31))
        return (target >> pos) & 1
      else
        return 0
    
    
    
    
    pub wr_bit(target, pos, value)
    
    
    '' writes value.0 to target.pos
    
    
      if (value & 1)
        return target | (1 << pos)
      else
        return target & !(1 << pos)
    
    
    
    
    pub toggle_bit(target, pos)
    
    
    '' toggles target.pos
    
    
      if ((pos => 0) and (pos =< 31))
        return target ^ (1 << pos)    
      else
        return target
    


    To set bit3 of myValue it's as easy as:

    myValue := wr_bit(myValue, 3, 0)

    Of course, you don't have to use my method; you can set and clear manually

    myValue |= 1 << 3

    or...

    myValue &= !(1 << 3)

    Like most programming languages, Spin is fairly flexible.
  • kuronekokuroneko Posts: 3,623
    edited 2013-10-12 17:50
    Both of these SPI objects were tested specifically with the SN74HC595 and SN74HC165

    SPI Assembly - Serial Peripheral Interface
    http://obex.parallax.com/object/564/
    FWIW, version 1.2 still suffers from a [post=1061378]minor bug[/post].
Sign In or Register to comment.