Shop OBEX P1 Docs P2 Docs Learn Events
Serial Communication - Input from Pin — Parallax Forums

Serial Communication - Input from Pin

heroldherold Posts: 66
edited 2005-07-15 22:00 in General Discussion
I like to get the state from an input pin (high/low) checked as often as possible and transfer the data (0/1) via serial to the PC.

Is there any basic source code available for this?
«1

Comments

  • BeanBean Posts: 8,129
    edited 2005-07-06 05:46
    How often the state of the input pin can be checked is going to be dictated by the speed of the serial transfer.
    At 115200 baud that would be about 11,520 times a second.

    The code would be very simple:
    DEVICE SX28, OSCHS3, TURBO, STACKX, OPTIONX
    FREQ 50_000_000

    InPin VAR RB.0 ' Change this to whatever pin you want to monitor
    SOutPin VAR RB.1 ' Change this to whatever pin you want serial output from

    Temp VAR BYTE

    PROGRAM Start

    Start:
    · INPUT InPin

    Again:
    · Temp=InPin
    · SEROUT SOutPin, N115200, Temp
    GOTO Again
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    Product web site: www.sxvm.com

    "What's the difference between ignorance and apathy ?"
    "I don't know, and I don't care."


    Post Edited (Bean (Hitt Consulting)) : 7/6/2005 5:50:56 AM GMT
  • heroldherold Posts: 66
    edited 2005-07-06 05:58
    Thank you very much Bean, that will give me a good start on it.·How would the code look like when I get the state from pin0 to pin7 (8 Bit) and transfer it with the same rate to the serial port?
  • BeanBean Posts: 8,129
    edited 2005-07-06 06:18
    Even easier...
    DEVICE SX28, OSCHS3, TURBO, STACKX, OPTIONX
    FREQ 50_000_000

    InPort VAR RC ' Make this to either RB or RC
    SOutPin VAR RB.1 ' Change this to whatever pin you want serial output from

    PROGRAM Start

    Start:
    · SEROUT SOutPin, N115200, InPort
    GOTO Start
    Bean.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    Product web site: www.sxvm.com

    "What's the difference between ignorance and apathy ?"
    "I don't know, and I don't care."
    ·
  • heroldherold Posts: 66
    edited 2005-07-06 06:45
    Looks really easy! I will give it a try...

    Thank you very much for your support.
  • pjvpjv Posts: 1,903
    edited 2005-07-06 10:16
    Hi Bean;

    But I believe that at 115,200 bits per second, the byte transmission rate is 11,520 bytes per second. Remember, the are 10 bit times per byte.

    So at one sample per transmission, the input would be sampled and transmitted 11,520 times per second.

    Can a PC reliably keep up with that on a continuous streaming basis? I not certain, but I would not bet on that. Probably depends on the number of buffers in the PC's UART, and whatever else WINDOZE is doing.

    If the user indeed needs (only) the absolute fastest transmission out of an SX, that can be reliably accomplished continuously at 5 megabits per second, and in bursts at 10 megabits per second. That however is well beyond most (all?) PC's ability to keep up.

    Cheers,

    Peter (pjv)
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-07-06 15:28
    There is the USB2SER, the chip is capable of 12Mbps (~1.2MBps), though it needs an eeprom configuration chip (the USB2SER has a one time programmable eeprom, which means the shipped setting is permanent (though you maybe able to do some rework to replace it with a reprogrammable version, of course voiding your warantee in the process)).

    Post Edited (Paul Baker) : 7/6/2005 3:32:10 PM GMT
  • heroldherold Posts: 66
    edited 2005-07-08 06:27
    Hello Bean,

    I bought the USB-Serial adapter your company is featuring. Does it matter which pin I use for the output in order to receive data in the PC?

    If I send data the USB-serial device should be blinking (red/green leds) th same way when I do program it, right? I assume the output data is send trough the SK Key?




    Post Edited (herold) : 7/8/2005 7:02:41 AM GMT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-08 12:17
    You cannot send serial data to the PC through the SX-Key (except debugging data via the debugger); you'll need to connect the SX to the PC through a MAX232 or similar adapter. In many cases you can make a direct connection and program the SX in a mode that will be compatible with the PC, but it's best to switch to RS232 levels.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • BeanBean Posts: 8,129
    edited 2005-07-08 15:17
    Jon is correct. You need to dedicate one of the SX's I/O pins to serial output. Sometimes you can just hook it directly up to the serial port and sometimes it won't work (it depends on the hardware in the PC).
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    Product web site: www.sxvm.com

    "One experiment is worth a thousand theories"
    ·
  • heroldherold Posts: 66
    edited 2005-07-08 16:31
    Hello Jon & Bean,

    Thank you for the reply. Can I use the USB2SER module you offer:
    http://www.parallax.com/detail.asp?product_id=28024

    If
    not, is there any hardware available that I can buy to hook the SX board up to my PC via Serila or USB? When I ordered the kit I was under the impression I can do this with the kit.

    Thank you

    Thomas Herold
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-08 16:50
    Yes, you can use it to take the TTL output from your SX chip and send it to the PC through a USB connection. Just make sure you have the FTDI VCP driver installed so that you can use the USB port holding the USB2SER as a standard COM port.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • heroldherold Posts: 66
    edited 2005-07-08 17:03
    Thank you Jon,

    Is it the same driver that comes with the serial to USB adapter? I bought this adapter with the SX board.
    http://www.parallax.com/detail.asp?product_id=800-00030
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-08 17:55
    Yes.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • heroldherold Posts: 66
    edited 2005-07-14 00:44
    Hello Jon or Bean,

    I got my USB2Ser device today:
    http://www.parallax.com/detail.asp?product_id=28024

    and plugged it onto the SX board. But I can't get data out of it!

    In the documentation to the USB2SER it says that the red and green LED's for TX & RX activity light up.

    I use this simple basic program:

    DEVICE SX28, OSCHS3, TURBO, STACKX, OPTIONX
    FREQ 50_000_000

    InPin VAR RB.0 ' Change this to whatever pin you want to monitor
    SOutPin VAR RB.1 ' Change this to whatever pin you want serial output from

    Temp VAR BYTE

    PROGRAM Start

    Start:
    INPUT InPin

    Again:
    Temp=InPin
    SEROUT SOutPin, N115200, Temp
    GOTO Again
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-14 03:40
    1) Did you install the FTDI VCP driver?
    2) All pins are made inputs unless you use NOSTARTUP -- you don't need to make "InPin" an input.
    3) You have no pacing between bytes and are sending them at a pretty fair clip; could this be part of the problem.
    4) You're sending zeros and ones -- can your PC program deal with this?

    Update:

    I wrote a simple demo that works -- but I had to put a pacing delay between bytes.· The output of the SX (RA.1 in my demo) is connected to the RX pin on the USB2SER adapter.· I used Hyperterminal setup at 115200 baud with no flow control.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 7/14/2005 4:07:39 AM GMT
  • heroldherold Posts: 66
    edited 2005-07-14 05:35
    Hello Jon,

    Thanks a lot. I installed the driver again and was running your test program. The debugger shows the signal coming out on the programmed pin.

    Here is what I do:

    - I unplug the FTDI US232B USB to Serial Adapter which I used to program the chip
    - I plugin the USB2SER chip directly into the SX board (4pin connection)
    - I plugin the other side into the USB on my PC
    No blinking on the USB2SER, no data comes out. I also checked with Hyper terminal. Any ideas where to look?

    If the connection this way is not ok, how exactly do I need to make a connection from the SX board to the 4pin USB2SER adapter?

    Post Edited (herold) : 7/14/2005 6:59:28 AM GMT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-14 12:17
    You may damaged the USB2SER adapter as it is not compatible with the SX-Key programming port. As has been stated several times in this thread you must use a I/O pin for communications; in my demo program I'm using RA.1. I connect this to the USB2SER.RX pin with a wire (22-guage solid wire works well). I also connect the USB2SER.Vss pin to Vss of my circuit. With the proper serial mode (True), the program works fine.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • heroldherold Posts: 66
    edited 2005-07-14 19:36
    Hello Jon,

    I understand, is there anyway to test the USB2SER to find out if it is still working?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-14 20:03
    Sure, hook it up to your SX and send data to it, view with Hyperterminal. Then write another program that uses SERIN and connect the TX output of the USB2SER to the SX. It shouldn't take more than 15 minutes or so to determine if everything is working.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • heroldherold Posts: 66
    edited 2005-07-14 20:17
    I hooked it up as you said with connecting Ra.1 to RX Pin and also connecting the Vss to the Vss pin. The SX program is running fine, I tested the pin with my scope.

    The USB2SER does not show any LED's blinking and Hyperterminal is not showing anything as well. Meaning I damaged it?

    As soon as I connect the RX pin and the VSS the USB2SER should start blinking, right?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-14 21:28
    Well, according to the USB2SER docs (hint, hint), the red led should light when data is being received on the TX line (I know this seems backwards, but it is correct) and the green light when data is coming from the RX line -- that happens on my setup.· After looking at the schematic I think your adapter should have been saved by its design: there are 1K resistors in the TX and RX line.· The RES line got connected to the OSC1 pin on your SX, and as it goes to the collector of a transistor I doubt any damage was caused.

    I used the attached program to suss everything out.· Run the program with these connections to your USB2SER:

    RA.0
    TX
    RA.1
    RX
    ·Vss·
    Vss

    The program starts with a loop that just receives.· Fire up Hyperterminal, select the comm port assigned to your USB2SER, then open the connection -- you should have to press a key 10 times (the red LED will light with each keypress).· After that, when you press a key the SX will echo it back with a little ">> " prefix -- the green LED will light when this happens.· Note that at 115200 the LEDs are on nearly simultaneously.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • heroldherold Posts: 66
    edited 2005-07-14 22:00
    Hello Jon,

    Thank you so much for your support...

    I get 2 errors running your program:

    Line 71: Error 5, Pass 1: Byte Parameter expected RXBYTE
    Line 75: Error 5, Pass 1: Byte Parameter expected RXBYTE

    I am running version SX-Key version 3.1.
  • ForrestForrest Posts: 1,341
    edited 2005-07-14 22:30
    You probably have an old version of SX/B installed. The latest version 1.31 was released on July 8 and you can download it here (attached to the first message) http://forums.parallax.com/showthread.php?p=517621
  • heroldherold Posts: 66
    edited 2005-07-14 23:38
    Hello Jon,

    Again, thanks so much for all the help. After updating the compiler your Serin Serout Test program runs!

    The other sample programn 'High Speed Serout' still doesn't work. At least I know the USB2SER adapter is working and I may need to experiment with baud rates. Probably the communication is very sensitive at high speed.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-15 00:50
    Check your connections -- everything runs great on this end. I'm glad your USB2SER isn't damaged and that you're getting close.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • heroldherold Posts: 66
    edited 2005-07-15 01:30
    Hello Jon,

    I got it finally all going, thank you for your help and support. But guess what I have the same speed problem than I had with the fastest Stamp chip.

    I cannot get more than 2k into my PC, which is reaaly slow. With 115200 baud I should get at least 10k in speed.

    Also if I don't include a PAUSE 1 statement the transmission seems to be screwed up.

    I am running a compiled Realbasic aplication which reads the serial port at max speed.

    Where is the bottleneck? Do I have to program the SX chip in assembler to get more speed out?




    Post Edited (herold) : 7/15/2005 5:17:46 AM GMT
  • BeanBean Posts: 8,129
    edited 2005-07-15 13:53
    Herold,
    Please post the program you are using.
    If the "PAUSE 1" is inside the loop that will really slow down the speed, you could try "PAUSEUS 100" which is only 0.1ms.
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    Product web site: www.sxvm.com

    "One experiment is worth a thousand theories"
    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-15 14:10
    Herold,

    Part of the problem you're going to have is overrunning the PC buffer -- can your PC/program handle that much data?· What I found was without the delay in the loop, characters were getting corrupted.· You might use Bean's suggestion an change the PAUSE 1 to PAUSEUS 100.

    You're getting all the speed you can out of the SX -- the SX/B program is compiled to very clean assembly code.· What you may need is a special-purpose serial card on your PC.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • heroldherold Posts: 66
    edited 2005-07-15 17:57
    I have posted the program I use. It is very simplified, in the loop there is only the Serout command left.

    With this settings I get about 5kb in data per second, but the transmission is no more correct. But 5K is not even considered highspeed. I switched from the Basic Stamp BSX2e to this SX board as I though I get more speed out of it, but in terms of serial communiation it is not more.

    With a PAUSEUS 100 I get max 2kb in data, which I find is really slow. With a baud rate of 115200 how many bytes per second is possible?

    I cannot believe that my USB port cannot handle more than 2k per second as Jon stated. I am using the USB2SER interface, which can handle·up to 3MBit.

    Post Edited (herold) : 7/15/2005 6:02:16 PM GMT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-07-15 19:27
    Sounds like the problem is in the PC end of things....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.