Shop OBEX P1 Docs P2 Docs Learn Events
How to send 7 cases and read 7 cases — Parallax Forums

How to send 7 cases and read 7 cases

NauketecNauketec Posts: 51
edited 2008-08-24 18:12 in BASIC Stamp
Howdy,

I have a project that uses 2 BS2P40 micros.

I need to send 0 to 7 cases out from one micro to be read by the other.
Unfortunately,· The pins chosen are off by a bit (no pun).·

The first micro uses ports (not pins) P1, P2, P3 to send data out, (there is nothing on PO, it was a mistake).

The·second micro uses ports (not pins) P0, P1, P2 to read data in. (P3 is used as an output)

I think what I need to use is the INA and OUTA commands but not sure how to filter out the unsed ports.

I would really appreciate any help but please use comments for I am a newbie.

Thanks

Daniel

Comments

  • ZootZoot Posts: 2,227
    edited 2008-08-21 22:33
    So you are setting the number you want as the 3 pins and just reading them all in one shot?

    DIRA
    INA
    OUTA

    Are your friends here. Each of the above represents some aspect of pins 0-3 (let's presume for discussion that you have the correct IO bank on the BS2p set.

    ' for reading on P1, P2, P3 -- the first micro
    myVal VAR Nib ' the number 0-7 coming in on the above pins
    
    DIRA = DIRA & %0001 ' this makes p1-p3 inputs and leaves p0 untouched
    ' dira bits are set if that pin is an output, cleared (0) if input
    
    DO
    myVal = INA >> 1   ' grab the state of 4 inputs pins (0-3) at once shift all the bits one place right -- %0110 would become %0011
    ' that pesky bit0 (pin0) just drops off the face of the earth
    DEBUG DEC1 myVal, CR
    LOOP
    
    ' for reading on P0, P1, P2 -- the second micro
    myVal VAR Nib ' the number 0-7 coming in on the above pins
    
    DIRA = DIRA & %1000 ' this makes p0-p2 inputs and leaves p3 untouched
    ' dira bits are set if that pin is an output, cleared (0) if input
    
    DO
    myVal = INA & %0111   ' positions of pin input bits are OK, but we don't want pin3, so mask it off
    ' that pesky bit0 (pin0) just drops off the face of the earth
    DEBUG DEC1 myVal, CR
    LOOP
    
    




    BTW -- If you need to comm. between to BS2p40s, personally I would use two pins on each and a high-baud SERIN/SEROUT with flow control (then you are much more flexible).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php


    Post Edited (Zoot) : 8/21/2008 10:56:54 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-08-21 22:36
    Daniel,

    You can use INA and OUTA to affect those lines. When reading INA the trick to ignoring the value of one of the bits depends on how you want to handle things. If your case since they literally are off by one bit you can simply shift the nibble left/right as necessary to properly align it with the next stage. On the end stage to ignore the bit you don’t want you simply mask it using bitwise AND. So you could do:

    Result = INA & %1110  this will mask bit 0 from the variable.
    Result = INA & %0111  this will mask bit 3 from the variable

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • NauketecNauketec Posts: 51
    edited 2008-08-21 23:15
    Hey Zoot - thanks for the help

    The first micro was to send out through ports P1 P2 P3 should I just change the INA comand for OUTA?
    and change the DIRA to %1110 ?

    I am waiting to see if I become your post # 1000

    thanks

    Danel
  • NauketecNauketec Posts: 51
    edited 2008-08-21 23:19
    Chris

    if this variable named Result masks the bits - is that result in bin also or DEC and do I need to leave out numbers - example:

    would the results be 0000 = DEC 0
    the next would be 0001 = can not be used
    the next usable be 0010 = DEC 2

    is that the correct truth table?

    Result = INA & %1110  this will mask bit 0 from the variable.
    Result = INA & %0111  this will mask bit 3 from the variable


    thanks Daniel
  • ZootZoot Posts: 2,227
    edited 2008-08-22 02:39
    If you just mask the bits, that would be the case. Shifting the bits into myval (for the micro on p1-p3) set the 3 bits you want "flush right" so you can read them "normally" i.e. 0-7.

    You would modify both DIRA and OUTA to "send" info -- DIRA will let you switch those pins to outputs, OUTA will let you set those now outpin pins to 1s (high) or 0s (low).

    Certainly make sure to connect the pins of the two Stamps together through a resistor (so if both pins on either end are *accidentally* made outputs and one is high and one is low you don't get a short. Or you could pull up all 3 lines with a 10k resistor, and just drive them low, i.e.

    PinState VAR DIRA
    PinState = %1000 ' all three inputs, pulled high, so other micro will see this as %0111
    OUTA = %0000 ' but remember this is not "seen" on the pins if they are inputs, only outputs
    ' now if you change pinstate, you are actually switching between OUTPUT LOW and INPUT.
    ' in input the line is allowed to be pulled HIGH by the external resistor
    ' the idea here is that if BOTH micros drive the line low, there is no short circuit
    ' if one or the other drives high while the other is low, the resistor sucks away the high load
    PinState = %1101 ' puts 010 on the lines
    PinState = %1000 ' puts 111 on the lines
    ' since it's the opposite of 1 = high, you can also NOT the value to make it easier to read
    PinState = ~%0111 ' puts 111 on the lines
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php


    Post Edited (Zoot) : 8/23/2008 8:29:32 AM GMT
  • NauketecNauketec Posts: 51
    edited 2008-08-22 06:55
    Hey Zoot and Jeff et al

    Here is the code I am working with if you choose to look at it or have a go at changing it for me to test.

    Thanks for all the help

    Daniel
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-08-22 15:04
    Masking the bits does just that. There is no change to the format of the value. Values are always stored in binary in memory. It is how we choose to view them that you refer to as the format, but in this case I have just taken the port and masked the bits. The value is the same whether you choose to represent it in decimal, hex or binary.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-08-23 00:17
    Hi Daniel, somewhere at the beginning of your "Main" program you should have P0 to P3 set to outputs (DIRA=%1111) because P0 is not used we don't really care. make a nibble variable that you can manipulate before you place it on the OUTA register. Once the variable is assigned a value shift the bits left 1 place so that they line up with P1 to P3.

    eg:

    LED_out VAR Nib

    LED_out=5· 'LED_out value now looks like this binary value %0101.......(P0=1,P1=0,P2=1 and P3=0)

    LED_out=LED_out << 1·· ''LED_out value now looks like this binary value %1010.......(P0=0,P1=1,P2=0 and P3=1)

    OUTA=LED_out 'Your output pins (P1 to P3) now contain the value 5

    Those 3 outputs will be read by the the other processor on P0 to P2

    eg:

    CheckMode:

    LED_in VAR Nib

    LED_in =INA & 7· 'We don't need P3 so we use the mask

    ON LED_in GOSUB mode7,mode6,mode5,mode4,mode3,mode2,mode1,mode0 'if LED_in=5 gosub mode2 etc

    mode1 mode2 etc are sub routines so at the end of each routine replace GOTO main with RETURN

    Jeff T.
  • NauketecNauketec Posts: 51
    edited 2008-08-24 18:12
    Hey· Jeff



    So much touble about these three pins I am electing to move the traces.· I hate putting in wires toa board but I left provisions with extra holes at each IO pin just in case.· So I am moving them now.

    If you could help me with the new code that would be great so I can put this issue to rest· I need to send out P0 P1 and P2 on one chip and make a debug so I can see the data and recieve data on the second micro om P0 P1 P2. and make a debug window to see it.· You are using Gosub my program is uses goto comands on one side I need pieces that go into the sections that state something like outa =·0, 1,2,3 4,.... 7 and on the otherside· reads 0 - 7 and sends it to a goto mode1,mode2,mode3 ... mode7

    Thanks again.
Sign In or Register to comment.