Shop OBEX P1 Docs P2 Docs Learn Events
Trying to make very simple code to read 74HC165 — Parallax Forums

Trying to make very simple code to read 74HC165

Trying and trying but no good yet.
CON
  _clkmode = xtal1 + pll16x                             ' use crystal x 16
  _xinfreq = 5_000_000                                  ' 5 MHz cyrstal (sys clockP = 80 MHz)

     'Simple code to read in data from 74HC165 with DIP switch

  clockP = 19     'clock pin (low to high edge triggered)
  loadP = 18      'parallel load Pin (active low)
  dataP  = 16     'data pin Receives data from 165
 
obj     
  TV    : "TV_text"

VAR
  byte winput
  long data_in  
PUB main 
   tv.start(0)
   waitcnt(clkfreq +cnt)

   outa[loadP]~~    'preset load pin high
   dira[loadP]~~   ' make load pin output
   dira[clockP]~~   ' make clock pin output
   
   repeat
     winput:=get_data
     tv.bin(winput,8)
     waitcnt(clkfreq*6+cnt)      'time to change switches
     tv.cr
     
PUB get_data 
  data_in:=0 
  
 ' pull clock  low and single strobe to clear all
  outa[CLOCKP] := 0
  '##strobe low to latch inputs on 165
  outa[loadP] := 0    'load pin low  to load data from 8 inputs
  wait
  outa[loadP] := 1    ' Must go high here
  wait   
      '##loop through all inputs and stick into data_in 
       'MSBPRE
       repeat 8
         
         'data_in := (data_in << 1) | ina[dataP]
         data_in :=(data_in << 1) + ina[dataP]
         wait
         PostClock(clockP)
            
  return  data_in       

PUB wait
  waitcnt(clkfreq/100+cnt)

PUB PostClock(_clockP)
    
    !outa[clockP]
    wait
    !outa[clockP]                  
Can you tell me what's wrong. I don't want to use complicated objects and can't find anything much for 74HC165. This will be for a wind direction sensor with 8 magnets
Thanks
Aaron

Comments

  • You do have the clock inhibit pin tied low, correct?
  • JonnyMacJonnyMac Posts: 9,159
    edited 2020-10-31 00:03
    Here's how I do it.
    pub setup
    
      io.high(LD_165)                                               ' setup x165 io pins
      io.low(CLK_165)                                                
      io.input(DO_165)
    
    pub rd_165(bits)
    
      outa[LD_165] := 0                                             ' blip Shift/Load line    
      outa[LD_165] := 1
                                                                            
      repeat bits
        result := (result << 1) | ina[DO_165]                       ' get bit     
        outa[CLK_165] := 1                                          ' blip clock     
        outa[CLK_165] := 0
    
    FWIW, I think the post set (~~) ad post clear (~) operators are confusing, and in tests I have found they're not as fast as simply setting things directly. I've attached my IO object as I use it to initialize the SPI pins to the '165.

    Another note: You don't need to slow down the process by putting the delay in the clock; in Spin you'll never outrun that chip. In my HC-8+ projects I use this unrolled code to read 17 digital inputs. It's more code, but faster without the loop overhead.
    pri scan_ttl_ins : tempin                                       ' call only from background()                    
                                                                     
    '' Scan TTL and DMX address inputs                               
                                                                     
      outa[LD_165] := 0                                             ' blip Shift/Load line    
      outa[LD_165] := 1
    
      tempin := ina[DMX_A8]                                         ' bit16                                           
    
      ' unrolled for best speed
    
      tempin := (tempin << 1) | ina[DO_165]                         ' bit15
      outa[CLK_165] := 1                                            ' blip clock  
      outa[CLK_165] := 0                                                        
      tempin := (tempin << 1) | ina[DO_165]
      outa[CLK_165] := 1                   
      outa[CLK_165] := 0                   
      tempin := (tempin << 1) | ina[DO_165]
      outa[CLK_165] := 1                   
      outa[CLK_165] := 0                   
      tempin := (tempin << 1) | ina[DO_165]
      outa[CLK_165] := 1                   
      outa[CLK_165] := 0
      tempin := (tempin << 1) | ina[DO_165] 
      outa[CLK_165] := 1                    
      outa[CLK_165] := 0                    
      tempin := (tempin << 1) | ina[DO_165] 
      outa[CLK_165] := 1                    
      outa[CLK_165] := 0                    
      tempin := (tempin << 1) | ina[DO_165] 
      outa[CLK_165] := 1                    
      outa[CLK_165] := 0
      tempin := (tempin << 1) | ina[DO_165] 
      outa[CLK_165] := 1                    
      outa[CLK_165] := 0
      
      tempin := (tempin << 1) | ina[DO_165]                         ' bit7
      outa[CLK_165] := 1                    
      outa[CLK_165] := 0                    
      tempin := (tempin << 1) | ina[DO_165] 
      outa[CLK_165] := 1                    
      outa[CLK_165] := 0                    
      tempin := (tempin << 1) | ina[DO_165] 
      outa[CLK_165] := 1                    
      outa[CLK_165] := 0                    
      tempin := (tempin << 1) | ina[DO_165] 
      outa[CLK_165] := 1                    
      outa[CLK_165] := 0
      tempin := (tempin << 1) | ina[DO_165] 
      outa[CLK_165] := 1                    
      outa[CLK_165] := 0                    
      tempin := (tempin << 1) | ina[DO_165] 
      outa[CLK_165] := 1                    
      outa[CLK_165] := 0                    
      tempin := (tempin << 1) | ina[DO_165] 
      outa[CLK_165] := 1                    
      outa[CLK_165] := 0                                       
      tempin := (tempin << 1) | ina[DO_165]                         ' bit0                    
                                                             
      ttlpins := tempin.byte[0]                                     ' update global vars
      dmxaddr := tempin >> 8
    



  • Dave HeinDave Hein Posts: 6,347
    edited 2020-10-30 23:39
    You could make your wait time about a second, and connect LEDs with a series resister on various pins to see what they're doing.
  • AribaAriba Posts: 2,690
    The code looks like it should work. I think you can remove all those waits, Spin is slow enough for a 74HC chip.
    So it may be a hardware problem. Have you connected the 'clock inhibit' pin (15) to ground?

    Andy
  • OK, I made the changes recommended ( post set (~~) ad post clear (~) operators are confusing, remove all those waits, and set data input as Jon said {result := (result << 1) | ina[DO_165] })
    and still got all 1s in the output.
    As @ARIBA said " may be a hardware problem", I checked all connections and I found that for some reason I had a 10k pull up on the Q7 output pin. Removing that solved the problem!
    Thank you all much!
    Aaron
  • Duane DegnDuane Degn Posts: 10,588
    edited 2020-10-31 18:14
    Just in case you're interested, I also have a '165 object. I just adapted the "Stamps Works" code for the Basic Stamp to work with the Propeller. Jon's code is also posted in this thread.

    Edit: I should have read the last post more carefully. It looks like your code works fine. It looked fine when I read it as well.
Sign In or Register to comment.