Shop OBEX P1 Docs P2 Docs Learn Events
Help with code — Parallax Forums

Help with code

eagletalontimeagletalontim Posts: 1,399
edited 2014-09-13 00:07 in Propeller 1
I am needing some help with the output of a 74HC165 that I have connected to my solar tracker. The code I am using is from an old project and I pretty much copied and pasted the code but don't quite understand how to pull 1 specific "switch" value from the switches variable.
VAR
   BYTE xInputs
   BYTE switches
   BYTE WestSwitch                              '  Max distance switch

PUB ReadSwitches
    xInputs := 0                                        ' clear variable to hold input states
    outa[Lever_LOAD] := 0
    outa[Lever_LOAD] := 1
    repeat 8
      xInputs <<= 1
      xInputs += ina[Lever_DATA]
      outa[Lever_CLK] := 1
      outa[Lever_CLK] := 0

    switches := xInputs                          ' Move updated values to new variable to prevent other code from picking up wrong data while processing inputs
    if switches[5] == 0                            ' ********* This is where I am confused.  **********
      WestSwitch := 1
    else
      WestSwitch := 0

Apparently xInputs or switches cannot be "accessed" like an array? There are 7 switch inputs and I want to check when any are pressed. I know I can use the CASE and then use "%0000_0010", but there are too many variations with switch press combinations. What am I doing wrong?


*** Edit:

I figured it out. I changed it to this :
VAR
  BYTE switches[8]

PUB ReadSwitches | i
    outa[Lever_LOAD] := 0
    outa[Lever_LOAD] := 1
    repeat i from 0 to 7
      switches[i] := ina[Lever_DATA]
      outa[Lever_CLK] := 1
      outa[Lever_CLK] := 0

    if switches[5] == 0
      WestSwitch := 1
    else
      WestSwitch := 0

I am going to bed.... it is WAY past my bed time.....2am....yeah....

Comments

  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-09-13 00:07
    No you cannot use an array element to read an individual bit. I have a method in my standard code that looks like this:
    pub rd_bit(value, pos)
    
    '' returns bit (0..1) of value.pos
    
      if ((pos => 0) and (pos =< 31))
        return (value >> pos) & 1
      else
        return 0
    


    If you want to assign the value of switches.bit5 to westswitch, you would do this:
    westswitch := 1 - rd_bit(switches, 5)
    


    If you don't want to be bothered with the method you can manually code it like this:
    westswitch := 1 - ((switches >> 5) & %1)
    


    I've attached my '165 object which will let you keep isolate the 165 from the application data (i.e., setting westswitch).
Sign In or Register to comment.