Shop OBEX P1 Docs P2 Docs Learn Events
74HC165 Spin Test code, need some help — Parallax Forums

74HC165 Spin Test code, need some help

DavidMDavidM Posts: 626
edited 2007-02-11 12:18 in Propeller 1
Hi,

As part of my BCD THUMB-WHEEL experiment I have tried to get the 74HC165 Shift register to work , but I need some help.
I have a Breadboard made up with 8 tactile switches, a 74HC165 chip and resistors etc. I put this together based on the BS2 example. This is connected to my PROPELLOR DEMO BOARD, and the idea is to press one or more of the buttons and have the corresponding LED's turn on .

Can someone please check my code to see what I have missed.

I am not sure what value is required for the initial LOAD PIN Pulse..

I have written this all in one routine, just to make it easier for me for now.


'' DAVES SERIAL SHIFT REGISTER TEST FOR 74HC165 - Parallel IN To Serial OUT
 
CON
  _CLKMODE      =XTAL1 +PLL16X 
  _XINFREQ       =5_000_000
  
VAR
  BYTE ByteValue
  LONG DataPinNo
  LONG ClockPinNo
  LONG InBit
  LONG NoOfBits
  LONG LoadPinNo
  
PUB RunTest

 ClockPinNo    := 0 
 DataPinNo     := 1
 LoadPinNo     := 2
 NoOfBits      := 8
 
 DIRA[noparse][[/noparse]16..23]~~ ''This is just a flash of LED's to indicate my program is running in the PROP
 OUTA[noparse][[/noparse]16..23]~~
 WAITCNT(50_000_000+CNT)
 OUTA[noparse][[/noparse]16..23]~

 REPEAT

   ByteValue := ShiftIn ( LoadPinNo, DataPinNo , ClockPinNo , NoOfBits)
   
    CASE  ByteValue              ''Check what data against the values
  
      1 : outa[noparse][[/noparse]16]~~
      2 : outa[noparse][[/noparse]17]~~
      3 : outa[noparse][[/noparse]18]~~
      4 : outa[noparse][[/noparse]19]~~
      5 : outa[noparse][[/noparse]20]~~
      6 : outa[noparse][[/noparse]21]~~
      7 : outa[noparse][[/noparse]22]~~ 
      8 : outa[noparse][[/noparse]23]~~
      
   WAITCNT(10_000_000+CNT)
   OUTA[noparse][[/noparse]16..23]~
   
PUB ShiftIn ( vLoadPinNo, vDataPinNo ,vClockPinNo , vNoOfBits ) : vByteValue | vInBit
      
  DIRA[noparse][[/noparse]vLoadPinNo]~~            ''Pulse Clock First 
  OUTA[noparse][[/noparse]vLoadPinNo]~~
  WAITCNT(1_000 + CNT)       ''Wait for some time ( should change to pulse duration)
  OUTA[noparse][[/noparse]vLoadPinNo]~
  
  DIRA[noparse][[/noparse]vDataPinNo]~
  OUTA[noparse][[/noparse]vClockPinNo]:= 0
  DIRA[noparse][[/noparse]vClockPinNo]~~

  vByteValue:=0

  REPEAT vNoOfBits
  
    InBit:=INA[noparse][[/noparse]vDataPinNo]
    vByteValue := (vByteValue << 1) + vInBit
    !OUTA[noparse][[/noparse]vClockPinNo]
    !OUTA[noparse][[/noparse]vClockPinNo]
    WAITCNT(1000 + cnt)




Thanks

Dave M

Comments

  • inserviinservi Posts: 113
    edited 2007-02-11 09:18
    Hello,

    For the 74HC165, in the worst case the Clock puls time must be at least 100ns but can be fine at about 25ns (at 25 °C and ~ 4.5V) that mean:
    For 100ns: if you take clkfreq witch give you the frequencies of clock, you must wait clkfreq *0.0001 Sec = 8_000 (cnt+8000)
    for 25ns you must wait clkfreq *0.000025 Sec = 2_000

    A good way to write that is

    VAR
    ...
      word PulsSize 
    ...
    
    PUB main
    ...
      PulsSize := clkfreq / 8000 ' for 100 ns
     OR PulsSize := clkfreq / 40000 ' for 25 ns
    
    ...
     WAITCNT(PulsSize + cnt)
    ...
    
    
    


    dro

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    in medio virtus

    Post Edited (inservi) : 2/11/2007 9:22:27 AM GMT
  • DavidMDavidM Posts: 626
    edited 2007-02-11 09:41
    Thanks,

    I added the CLKFREQ /8000 +CNT to my code, I also believe that I am returning a value as the vByteValue is being calculated during the REPEAT cycle, I have added the return function anyhow?

    thanks

    dave M
  • OzStampOzStamp Posts: 377
    edited 2007-02-11 10:10
    Hi Dave.

    Your defining shiftin but do not have the obj loaded.
    Go back to basics...

    The HC165 needs a HIGH "1" on the load pin initially.
    Then it needs a quick pulse on the load pin hi and low

    Then set up a repeat loop of 8 times do the following
    Since the data needs to be sample in MSBPRE mode ( see BS2 sample code that you have)

    1 You add the data data = data + indata(from HC165)
    2 Move the data to the left <<1 ( it is ionitially 0 so zero shifted left is still zero first time)
    3 Now do a clk pulse HI for awhile low for awhile..
    4 return to to line 1

    Knock that up in SPIN use short wait constants.. the HC165 is lightning fast..it is a shift register ...

    Come on Dave you can do this.
    I have posted stuff before re sampling the ADC0831 chip very similar code look at that ...

    Ronald Nollet Australia
  • DavidMDavidM Posts: 626
    edited 2007-02-11 10:21
    Hi Ron,

    Do I need to have SHIFTIN defined as an object? this is just a sub routine, and besides My app compiles OK and I did some tests and found that my repeats and call are working OK..


    The Problem Is in the GUTS of the ShiftIn REPEAT loop,

    or with my circuit of my breadboard, I have just checked my bread board and all wiring is as per BS2 Example.


    regards

    Dave M
  • OzStampOzStamp Posts: 377
    edited 2007-02-11 11:25
    Hi Dave

    Attached a quick knock up translated BS2 code across to spin

    Hope you get the drift with this code.

    Not tested but it is close..

    Everybody will say use the BS2.functions and call the shiftin ..

    I agree but b4 you do that understand the way how this chip operates.

    Ronald Nollet Australia
  • DavidMDavidM Posts: 626
    edited 2007-02-11 12:18
    Hi Ron

    I used some of your code ( just a few lines) and guess what I WORKS!

    Well at least I got a LED to light UP, I have been trying all day,

    So I got exited and worked out the rest and I diddent even have to change any of my breadborad it was correct from first GO!

    here is my refined code for using a 74HC165 Serial Out to PARALLEL in SHIFT REGISTER.

    The circuit is based on the one in the BS2 example..



    '' DAVES SERIAL SHIFT REGISTER TEST FOR 74HC165 - Parallel IN To Serial OUT
    '' By Dave Metus IMIX MULTIMEDIA , Please feel free to comment and Use..
    '' 11/02/2007
    
    CON
      _CLKMODE     =XTAL1 +PLL16X 
      _XINFREQ     =5_000_000
      
    VAR
      BYTE ByteValue
      LONG DataPinNo
      LONG ClockPinNo
      LONG NoOfBits
      LONG LoadPinNo
      
    PUB RunTest
    
     ClockPinNo    := 0 
     DataPinNo     := 1
     LoadPinNo     := 2
     NoOfBits      := 8
     
     DIRA[noparse][[/noparse]16..23] ~~
    
     REPEAT
       ByteValue := ShiftIn ( LoadPinNo, DataPinNo , ClockPinNo , NoOfBits)
       OUTA[noparse][[/noparse]23..16] := ByteValue
       WAITCNT(1_000+CNT)
        
    PUB ShiftIn ( vLoadPinNo, vDataPinNo ,vClockPinNo , vNoOfBits ) :vByteValue | vInBit
          
      DIRA[noparse][[/noparse]vLoadPinNo] ~~                              '' SET the LOAD Pin to OUTPUT 
      OUTA[noparse][[/noparse]vLoadPinNo] := 0                            '' TOGGLE the LOAD Pin OFF
      WAITCNT(1000 + CNT)                              '' Wait for some time
      OUTA[noparse][[/noparse]vLoadPinNo] := 1                            '' TOGGLE the LOAD Pin ON 
      
      DIRA[noparse][[/noparse]vDataPinNo]  ~                              '' Set DATA Pin to OUTPUT
    
      DIRA[noparse][[/noparse]vClockPinNo] ~~                             '' SET the CLOCK Pin to OUTPUT
      OUTA[noparse][[/noparse]vClockPinNo] := 0                           '' TOGGLE the CLOCK Pin OFF
    
      REPEAT vNoOfBits                                 '' Start REPEAT LOOP for vNoOfBits ie 8  
                                                     
        vInBit:=INA[noparse][[/noparse]vDataPinNo]                        '' READ the DATA pin (bit value 1 or 0)
        vByteValue := (vByteValue << 1) + vInBit       '' SHIFT Left and Add to the BYTEVALUE the Next Bit 
        OUTA[noparse][[/noparse]vClockPinNo] := 1                         '' TOGGLE the CLOCK Pin ON
        OUTA[noparse][[/noparse]vClockPinNo] := 0                         '' TOGGLE the CLOCK Pin OFF
        WAITCNT(1000 + CNT)                            '' WAIT for some time and REPEAT
    
    
    



    Thanks RON and Others for the help, It was 90% correct!

    Now all I have to do is modify it slightly to work with my THUMBWHEELS

    regards

    Dave M
Sign In or Register to comment.