Shop OBEX P1 Docs P2 Docs Learn Events
BS2: Pls advice in a problem of communications — Parallax Forums

BS2: Pls advice in a problem of communications

merlinmerlin Posts: 40
edited 2011-08-04 19:21 in BASIC Stamp
Hi all, I am trying to communicate my BS2 with a device via serial protocol.
The device's manual declares:
"Data communications and control are accomplished using 3 wire synchronous serial interface:

Signal................Description..............................DEVICE =>BS2........................BS2=>DEVICE
DATA................Bidirectional Data Line.............. Data valid when SHS=0..........Data valid when MHS=0

MHS................Master Handshake...................... 0 = Data bit Valid....................0 = Data bit Accepted
........................(BS2 => Device)

SHS................Slave Handshake..........................0 = Data bit Accepted.............0 = Data bit Valid
........................(Device => BS2)

Data is transferred one bit at a time with full handshaking as follow:

1.- When BS2 has data to transmit, the BS2 sets DATA to the data value, verifies that SHS is in the HIGH state, then sets MHS to the LOW state to request a transfer.
2.- The Device senses the LOW state of MHS and reads DATA, which then seets SHS to the LOW state to acknowledge the DATA.
3.- BS2 senses the LOW state of SHS and sets MHS to HIGH state to indicate that DATA is no longer valid, and at the same time sets DATA HIGH releasing it.
4.- The Device then sets SHS to the HIGH state to indicate that the cycle is complete. Both device and BS2 are ready to transfer a new bit.


Due my lack of knowledge about this issues, I didn't get communication between the Device and BS2 using SHIFTIN / SHIFTOUT.
I really appreciate advices in the correct use of the SHIFTIN/OUT sentences in order to get Data transfer here.

Thanks in advance


daniel

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-08-02 21:16
    You can't use SHIFTIN and SHIFTOUT to implement this protocol. SHIFTIN and SHIFTOUT implement a simple clocked synchronous serial data protocol where the Stamp is always the "master". That's not what you have.

    You can implement what you need using discrete statements like this
    for bitNum = 1 to 8
    dataPin = dataValue >> 7
    dataValue = dataValue << 1
    waitForSHS1: if shsPin = 0 then goto waitForSHS1
    low mhsPin
    waitForSHS0: if shsPin = 1 then goto waitForSHS0
    high mhsPin
    high dataPin
    next bitNum
    
    shsPin, mhsPin, and dataPin are declared using the PIN declaration
    bitNum and dataValue are byte variables declared using the VAR declaration

    This will probably send a bit in about 3 to 5ms with a BS2.
  • merlinmerlin Posts: 40
    edited 2011-08-03 06:39
    Mike
    Thank you very much for the response. I would never arrives to this solution by myself, but now I see and I found a very logical approach. I will implement it in a few hours and hopefully I will report this issue as solved!
    I guess the DataIn and DataOut scenarios will be arranged with the aproppiate changes into values of shsPin and mhsPin as the Device protocol declares, is that rigth?
    You said one bit will take about 3 to 5 ms...you mean one bit or one byte?

    Best regards and thanks again

    daniel
  • Mike GreenMike Green Posts: 23,101
    edited 2011-08-03 07:33
    The code I gave just implements what you described. Look at it and work it through so you'll understand it. I did assume that the most significant bit of the data is sent first. If that's not true, the 2nd and 3rd lines will have to be changed to "dataPin = dataValue" and "dataValue = dataValue >> 1" respectively.

    One bit will probably take about 3 to 5ms. I base that on timings provided here. You click on the app-note link at the bottom of the page. A lot of simple operations take a couple of hundred microseconds on the BS2.
  • merlinmerlin Posts: 40
    edited 2011-08-04 19:21
    Mike:
    I'm having some problems on my practical implementation: I am trying to send a code to the device (Hex30) because the device HAS to respond to this code, but nothings happens.
    The code I wrote is:

    ' {$STAMP BS2px}
    ' {$PBASIC 2.5}

    bitNum VAR Byte
    dataValue VAR Byte
    shsPin PIN 2
    mhsPin PIN 1
    dataPin PIN 0

    dataPin = $30 ' asign a value to dataPin in order to be transferred
    HIGH shspin
    LOW mhspin
    GOSUB call_device

    END

    '
    sub-routine
    call_device:

    FOR bitNum = 1 TO 8
    dataPin = dataValue >> 7
    dataValue = dataValue << 1
    waitForSHS1: IF shsPin = 0 THEN GOTO waitForSHS1
    LOW mhsPin
    waitForSHS0: IF shsPin = 1 THEN GOTO waitForSHS0
    HIGH mhsPin
    HIGH dataPin
    NEXT
    RETURN

    Is this the rigth way to call the routine? Am I using the correct values of mhs and shs?

    Pls take a look to the code and give me another tip to follow....thanks and regards

    daniel
Sign In or Register to comment.