Shop OBEX P1 Docs P2 Docs Learn Events
Javelin to BS2 communication. — Parallax Forums

Javelin to BS2 communication.

Kevin B SlaterKevin B Slater Posts: 49
edited 2006-08-03 18:41 in General Discussion
· I am connecting a Javelin to a BS2 using N&V article number 81, A Tale of Two Stamps, as a guide.· My question is can I use·2 pins·to do this.· I know that you have to declare a Uart on the Javelin as a receive or a transmit.··I believe I will have to use·three pins because of this, but was wanting to know if it could in fact be·done with two.

Thanks,
Kevin

Comments

  • Kevin B SlaterKevin B Slater Posts: 49
    edited 2005-06-27 12:58
    Here is an update in my experiments to get these two stamps to communicate.· It is not working, as the BS2 never drops·through the IF statement to flash the LED to let me know that I received the correct byte from the Javelin.· I have included my source code for both stamps and the schematic I used from the N&V article #81, changed to represent how I have the stamps connected.

    Thanks for any input.

    Kevin
  • bulkheadbulkhead Posts: 405
    edited 2006-08-02 17:42
    I know this is an old thread, but I'd like to know what is wrong with the posted code? I am trying to do the same thing with a bs2 and javelin. The only thing I can see is that the javelin's Uart is initialized for a handshaking pin while the bs2 is using a flow control pin, are these the same thing?

    And isn't it possible to just use one Uart with one pin to send and receive data from the bs2? I'm almost at 6 VP's and 16 pins and need efficiency.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-08-02 19:43
    You can use the setDirection() method to change a transmit uart into a receive uart
    and vice versa. That way you only require 1 pin.
    However, let the BS be the master and use a halfduplex request/acknowledge protocol,
    so the BS does not miss any transmission.
    Example:
    BS sends <COMMAND><data1><data2>...
    Javelin responds with <resp1><resp2>

    COMMAND,data1,data2,resp1,resp2 are bytes and to be defined by your
    protocol. How many bytes are transmitted or received is up to you.
    At least you should have commands for
    a. Data available (to inform javelin the BS has data for the javelin)
    b. Data required (to inform javelin the BS requires data from the javelin)
    Note that it is the BS that initiates transmission and that the javelin
    always responds.

    So the BS has a sequence
    · SEROUT ...
    · SERIN ...

    wheras the javelin has a sequence

    · if (BSIO.bytreAvailable()) {
    ··· receive·COMMAND and databytes
    ··· process command
    ··· BSIO.setDirection(Uart.dirTransmit);
    ··· send response
    ··· while (!BSIO.sendBufferEmpty()) ; //wait for transmission completed
    ··· BSIO.setDirection(Uart.dirReceive);
    · }

    Note that the javelin response may be the response of a previous processed·command
    so the BS does not have to wait for the current command to be processed.
    In that case you should index your commands and responses so responses can be matched to commands.
    Again, this all depends on how you define your protocol.

    regards peter
  • bulkheadbulkhead Posts: 405
    edited 2006-08-02 22:46
    Ok, I tried to set it up the way you described it. Currently I am just trying to get the javelin to receive 1 byte, add 1 to that, and send it back out. Right now both stamps have both ground and pin 0 connected (through 470 ohm resistor). Here is my javelin's code:
    import stamp.core.*;
    
    public class serTest
    {
    
      //Creates a Uart for Communication
      static Uart BSIO = new Uart(Uart.dirReceive,CPU.pin0,Uart.dontInvert,Uart.speed2400,Uart.stop1);
    
      public static void main()
      {
        int receivedByte;
        int transmitByte;
        System.out.println("Starting up...");
        while(true)
        {
          if(BSIO.byteAvailable())
          {
            receivedByte=BSIO.receiveByte();
            transmitByte=1+receivedByte;
            BSIO.setDirection(Uart.dirTransmit);
            BSIO.sendByte(transmitByte);
            while(!BSIO.sendBufferEmpty());
            BSIO.setDirection(Uart.dirReceive);
            System.out.print("byte");
            System.out.println(transmitByte);
    
          }
          CPU.delay(2000);
    
        }
      }
    }
    



    Now the main part of the bs2 code:
    Reset:
      receivedByte = 2
    
    
    ' -----[noparse][[/noparse] Program Code ]----------------------------------------------------
    
    Main:
      DEBUG "byte: "
      DEBUG receivedByte
      SEROUT JavIO,T2400,[noparse][[/noparse]DEC receivedByte]
      SERIN JavIO,T2400,[noparse][[/noparse]receivedByte]
      GOTO Main
    END
    



    I have attached the full BS2 code if it helps.

    I have some general questions too, should both the stamps match on inverted/ not inverted? Is one better than the other? Also, what is a good speed?

    I also noticed that if the BS2 doesn't get a response back after a second or so it executes the rest of the code. Isn't that a problem if my javelin's main loop takes, say 2 seconds, so it wont reach the "byteAvailable()" statement in time before the BS2 moves on?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-08-02 22:59
    Javelin code looks ok.
    For the BS code, try this

    Main:
    ··DEBUG·"byte:·"
    ··DEBUG·DEC receivedByte
    ··SEROUT·JavIO,T2400,[noparse][[/noparse]receivedByte]
    ··SERIN·JavIO,T2400,[noparse][[/noparse]receivedByte]
    ··GOTO·Main
    END

    You just want to send the receivedByte, not its decimal ascii representation.

    I am not sure if the BS2 supports a timeout on serin.
    If it does, you can use that to identify a not responding javelin.

    Do you get any results in the BS debug window?

    regards peter
  • bulkheadbulkhead Posts: 405
    edited 2006-08-02 23:46
    Ok, I made that change and it still didn't work, so I changed the javelin's Uart to Uart.invert and it works now. Here is my output:

    byte: 2byte: 4byte: 8byte: 16byte: 32byte: 64byte: 0byte: 0...

    Apparently, the "1+receivedByte" statement adds a 1 bit onto the end, and does not increment the number by 1. How can I fix this?

    Now I just want to make sure about some things before I set up my protocol. Here is what I have for the javelin:

    if (BSIO.byteAvailable()) {
    receiveByte() //the COMMAND
    while(byteAvailable())
    receiveByte() and store
    process command
    BSIO.setDirection(Uart.dirTransmit);
    sendByte( );
    sendByte( );
    ...
    sendByte( );
    while (!BSIO.sendBufferEmpty()) ; //wait for transmission completed
    BSIO.setDirection(Uart.dirReceive);
    }


    And for the BS2:

    SEROUT JavIO,T2400,[noparse][[/noparse]COMMAND,byte1,byte2,....]
    SERIN JavIO,T2400,[noparse][[/noparse]receivedbyte1,receivedbyte2,...]

    Would it work/be easier to always receive a preset number of bytes and to always transmit a set number of bytes so there is no need to send commands that say data is available or needed? I am trying to keep this as simple as possible.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-08-03 05:39
    I figured you should use invert mode.
    I also think you need a pullup resistor of 10k on the data line.
    In invert mode the rest level is high.
    So the BS starts with IO line high, then goes low (startbit),
    does the bits and then goes high (stopbit). Then the BS
    changes the IO to an input (SERIN). Because the javelin
    also is an input then, the IO line would float without the pullup.
    Due to the pullup the IO level remains high.
    When the javelin changes the IO to an output to transmit
    its response, it outputs a high (rest level), then low (startbit),
    does the bits and then high again (stopbit).

    So with a pullup and invert mode the IO level is always high
    when changing pin direction. Make sure the baudrate constant
    for the BS2 is also for inverted mode.

    Before deciding on the exact protocol, you must get the communication
    working. The 1+receivedByte simply adds 1 to the received value
    and does not double it (if 2 is received, the response is 3).
    Add a delay after SERIN in the BS. This allows for the javelin
    to change the uart direction back to receive.
    Use a pause 1000 for a 1 sec delay.

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-08-03 09:25
    I wrote a small demonstration program for half duplex protocol
    using a single javelin. Pin2 acts as master, pin3 as slave.

    Both pins 2 and 3 have 4.7k pullup resistor.
    Pins 2 and 3 are connected via 1k resistor.

    The program works only using Uart.dontInvert mode,
    which makes me believe the dontInvert mode has
    a high rest level (I thought invert mode had this).

    regards peter

    Post Edited (Peter Verkaik) : 8/3/2006 10:16:12 AM GMT
  • bulkheadbulkhead Posts: 405
    edited 2006-08-03 16:26
    Ok, thanks Peter. I tried your setup except with my bs2, and got the communication working with both on dontInvert and the two pull up resistors. Now to setup my protocol...
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-08-03 16:51
    For your protocol,
    what exactly do you want you the bs2 and the javelin to do?
    I assume you want to exchange data between the two
    but it is easiest if we know what you try to accomplish.

    regards peter
  • bulkheadbulkhead Posts: 405
    edited 2006-08-03 18:08
    My javelin stamp is controlling about 6 sensors and I am running out of I/O pins. I am trying to transfer some of the simpler tasks to my BS2. All I need is for the BS2 to read the states of 5 I/O pins that are connected to switches for user input, and give that data to my javelin stamp. I would also like it to operate some L293D motor controllers to turn on various motors and lights. This is yet another simple task, just putting pins on either HIGH or LOW, or perhaps using PULSOUT for a few predefined values.

    For sending the switch data, I only need 1 byte, and for receiving the data for the motorcontrollers, I could also do it with 1 byte (There will be fewer than 8 pins needed for motor control). If done this way, I would only need to transmit 1 byte to my javelin, and have it respond by sending another byte back.

    I guess I need to know how to make a data byte that would have bits reflecting the states of my 5 switches. For my javelin, I think I will use this statement for decoding the byte:

    (receivedByte & (1<<switchNumber) ) == 0

    And for the javelin's transmission back to the bs2 with the data for the motor controllers, I can just set up a simple number system code, like 1 = turnOn motor #1, 2 - turn on lights, 3 = turn on motor and lights, etc since there are only a few tasks to do. This would simplify it somewhat since the action of turning on a motor would require 3 pins.

    Any suggestions?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-08-03 18:41
    Yes, you could do it exchanging only 1 byte.
    The BS2 sends a byte holding button states, the javelin responds with a byte
    holding the L293D states. I checked the L293D datasheet and it only requires
    4 control bits + 2 enable bits. If permanently enabled, only 4 bits are required
    for the L293D, leaving 4 bits for other purposes.

    The BS2 may send at will, so it should send the button states frequently
    enough so the javelin can send updated L293D data to the BS2 often enough
    for your application.

    regards peter
Sign In or Register to comment.