Shop OBEX P1 Docs P2 Docs Learn Events
Best way to implement an array of 64+ IR detectors? — Parallax Forums

Best way to implement an array of 64+ IR detectors?

outofstepoutofstep Posts: 3
edited 2010-09-22 07:11 in BASIC Stamp
I've got·a pro dev board and am using the 40 pin version of the stamp.

If it was a simple push button type input matrix, I could simply use the voltage divider setup of the POT command.

By default the IR detectors (link)·Vout +5 when not detecting, and 0 when they do detect; I am unsure of how to set up the circuit. Also, because the IR detectors are diodes, the POT setup shouldnt work (if running the IR detectors in parallel,·to make up for the constant on state)·because the voltage read stage should always read back 0.

The only way I've come up with is to use 8 8x3 encoders. However that will eat up 24 I/O lines. Is there a more elegant/efficient way to set this up?


I'm not sure if I explained everything correctly or not, just let me know if you need me to clarify something.



Post Edited (outofstep) : 3/2/2009 8:51:48 PM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-03-02 21:09
    The 74HC165 is a parallel input, serial output shift register that can be easily used with the Stamp's SHIFTIN statement and they can be cascaded. There are articles in the series of Nuts and Volts Columns downloadable from Parallax that discuss I/O expansion. They're also described (with sample code) in the StampWorks manual (Experiments #23 and #24), also downloadable from Parallax (www.parallax.com/Store/Microcontrollers/BASICStampModules/tabid/134/txtSearch/stampworks/List/1/ProductID/144/Default.aspx?SortField=ProductName%2cProductName).

    You can cascade 8 x 74HC165 to provide 64 inputs using only 3 I/O pins (clock, data, parallel load). The 74HC595 provides similar output expansion.

    Post Edited (Mike Green) : 3/2/2009 9:14:58 PM GMT
  • outofstepoutofstep Posts: 3
    edited 2009-03-06 16:52
    Thank you very much for your reply and the included information. Very helpful.
  • outofstepoutofstep Posts: 3
    edited 2009-03-06 17:38
    Okay, reading up on the SHIFTIN command it looks like I'll have to break it up into 4 parts. With this code snippet, can you tell me if I'm on the right track for shifting in 64 inputs?
    code said...

    tophi VAR Word
    toplo VAR Word
    bottomhi VAR Word
    bottomlo Var Word

    SHIFTIN 0, 1, MSBPRE, [noparse][[/noparse]tophi\16, toplo\16, bottomhi\16, bottomlo\16]
    -edit-

    It looks like I can break up the data coming in however I want. For an 8x8 matrix of sensors, I could assign an entire collumn or row a variable of its own.

    SHIFTIN 0,1, mode, [noparse][[/noparse]row1\8, row2\8, row3\8, row4\8, row5\8, row6\8, row7\8, row8\8]

    Post Edited (outofstep) : 3/6/2009 6:15:08 PM GMT
  • kleekrukleekru Posts: 32
    edited 2010-08-30 00:04
    OK, I'm cascading 74hc165's... the first IC is working fine... and I've used the schematic and code listed in the StampWorks manual to wire and code the second 74hc165... I've even used the suggestion above to create different variables for each chip and shift them in 8 at a time... or at least I think that is what I'm doing. Unfortunately, while all 8 bits are working fine for the first chip, when I try a switch on the 9th bit, I get nothing.

    Mike, you've been very helpful in some of the other posts. How do I change my code here to reference a switch hanging off of the 2nd (and eventually 3rd, 4th, 5th...) chip?

    Here is what I have:
    '
    [ I/O Definitions ]
    Clock PIN 0 ' shift clock (74HC165.2)
    SerData PIN 1 ' serial data (74HC165.7)
    Load PIN 2 ' output latch (74HC165.1)

    '
    [ Constants ]
    DelayTime CON 100

    '
    [ Variables ]
    pedals VAR Word
    xInputs VAR Word ' switch data

    '
    [ Initialization ]
    Reset:
    LOW Clock
    HIGH Load ' make output and high
    DEBUG CLS,
    "Switches FEDCBA9876543210", CR,
    "

    ", CR,
    "Status"

    '
    [ Program Code ]
    Main:
    DO
    GOSUB Get_165X2 ' get switch inputs
    DEBUG CRSRXY, 10, 2, BIN8 pedals, BIN8 xInputs ' display current status
    PAUSE delaytime
    IF pedals.BIT0 = 1 THEN
    HIGH 13
    LOW 14
    ELSEIF pedals.BIT1 = 1 THEN
    HIGH 14
    LOW 13
    ELSEIF pedals.BIT8 = 1 THEN 'THIS PART IS NOT WORKING for BIT8-15!!!
    HIGH 14
    HIGH 13
    ELSE
    LOW 14
    LOW 13
    ENDIF
    LOOP

    '
    [ Subroutines ]
    Get_165X2:
    PULSOUT Load, 5 ' load switch inputs
    SHIFTIN SerData, Clock, MSBPRE, [pedals\8, xInputs\8] ' shift them in (IS THIS RIGHT?)
    RETURN
  • Mike GreenMike Green Posts: 23,101
    edited 2010-08-30 06:09
    You're shifting 8 bits into each of 2 variables ("pedals\8","xInputs\8"). That's .BIT0 through .BIT7 of each. If you want to shift the whole 16 bits into one variable, leave off the "\8" and PBasic will assume you mean "\16" because the variables are WORDs. If you use "pedals,xInputs", that's 32 bits total and would assume you have 4 74HC165s.
  • kleekrukleekru Posts: 32
    edited 2010-08-30 07:46
    Thanks Mike. I also tried using a single variable (pedals\16) but then they dont seem to respond when I have a switch against one of the pins (I'm using pedals.BIT8 for example). I'm expecting this to be something simple but I've triple checked and completely rewired and I get the same lack of response. I've tried another chip, same thing. I'm using the same wiring and code as in the Stampworks manual but can't reference the 2nd IC. Grrr.
  • kleekrukleekru Posts: 32
    edited 2010-08-30 10:09
    Here's the code with the updates (still not working)

    '
    [ Variables ]
    pedals VAR Word
    xInputs VAR Word ' switch data

    '
    [ Initialization ]
    Reset:
    LOW Clock
    HIGH Load ' make output and high
    DEBUG CLS,
    "Switches FEDCBA9876543210", CR,
    "

    ", CR,
    "Status"

    '
    [ Program Code ]
    Main:
    DO
    GOSUB Get_165X2 ' get switch inputs
    DEBUG CRSRXY, 10, 2, BIN16 pedals ' display current status
    PAUSE delaytime
    IF pedals.BIT0 = 1 THEN
    HIGH 13
    LOW 14
    ELSEIF pedals.BIT1 = 1 THEN
    HIGH 14
    LOW 13
    ELSEIF pedals.BIT7 = 1 THEN
    HIGH 14
    HIGH 13
    ELSE
    LOW 14
    LOW 13
    ENDIF
    LOOP

    'LOW 12
    'HIGH 14
    'HIGH 13
    'PAUSE 50
    'LOW 14
    'LOW 13
    'ELSEIF switches.BIT0=1 AND switches.BIT1=1 AND switches.BIT5=1 THEN
    'SEROUT 15,6,["#0 P500 #1 P1400 #2 P1600 #3 P1400",13]
    'HIGH 13
    'HIGH 14
    'HIGH 12
    'ELSE
    'SEROUT 15,6,["#0 P750 #1 P1500 #2 P1500 #3 P1500",13]
    'LOW 13
    'LOW 14
    'LOW 12
    'ENDIF

    'PAUSE DelayTime ' pad the loop a bit
    'LOOP

    '
    [ Subroutines ]
    Get_165X2:
    PULSOUT Load, 5 ' load switch inputs
    SHIFTIN SerData, Clock, MSBPRE, [pedals] ' shift them in
    RETURN

    Switches FEDCBA9876543210

    Status 0000000001110000

    The switches on the left 8 bits are not working. I have a switch on the pin that lines up with what I thought would be #8 (1st input from the second chip).

    Any and all suggestions are greatly appreciated.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-08-30 11:06
    Almost all the time when something like this happens, it's due to a wiring error. Someone is referencing a diagram in a tutorial or manual and the diagram is correct, but they've copied it wrong or wired it wrong and they can't understand why it doesn't work. If you can, have someone else check your work or painstakingly go over your wiring, make a list of what's connected where, then compare that against the diagram in the text marking each wire as you match them up.
  • kleekrukleekru Posts: 32
    edited 2010-09-21 23:36
    Whew, I got it working with 2 74HC165's by completely re-wiring. Wasn't fun but it is nice to get some motivation again. On to the next step... adding another shift register. I suspect cascading a 3rd one is just like cascading the 2nd so I've wired it the same connecting Pin 9 of the 3rd to Pin 10 of the 2nd, 1 of the 3rd to 1 of the 2nd and 2 of the 3rd to 2 of the second....

    However, now I'm baffled by the code. Above outofstep suggests adding separate variables for each 74HC165:
    "pedals1 VAR Word
    pedals2 VAR Word

    SHIFTIN 0, 1, MSBPRE, [pedals1\16, pedals2\16]
    "

    If I use this approach, how should I write the following:
    1) The code to view all 24 (or more) inputs:
    DEBUG CRSRXY, 10, 2, BIN16 pedals1 ' ??how do I reference the pins in pedals 2??

    2) How do I reference each of the input pins on the 3rd register... I'm assuming I can use pedals1.BIT0 - pedals1.BIT15 ...or... pedals2.BIT0 - pedals2.BIT15

    Am I thinking about this correctly?
  • Mike GreenMike Green Posts: 23,101
    edited 2010-09-22 07:11
    1) DEBUG ..., BIN16 pedals1, BIN16 pedals2

    2) Correct. The Manual also discusses bit arrays. You can refer to consecutive bits using a subscript and this can span adjacent variables like "pedals1.bit0(i)".

    Each WORD variable has 16 bits.
Sign In or Register to comment.