Shop OBEX P1 Docs P2 Docs Learn Events
Serial output — Parallax Forums

Serial output

ArchiverArchiver Posts: 46,084
edited 2003-11-06 06:49 in General Discussion
Does anyone know if it's possible to send a serial string of 5 bytes
with 1 startbit, 8 databits, 1 even paritybit and 1 stopbit with the
BS2 ? The bitrate should be 2400 bps.
Is it possible to get 2400 bps by using the low, high or toggle
commands and send it bit by bit - or do you have any other
suggestion ?

(The SEROUT command does not support 8 databits + parity bit !!)

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-11-05 17:11
    >Does anyone know if it's possible to send a serial string of 5 bytes
    >with 1 startbit, 8 databits, 1 even paritybit and 1 stopbit with the
    >BS2 ? The bitrate should be 2400 bps.
    >Is it possible to get 2400 bps by using the low, high or toggle
    >commands and send it bit by bit - or do you have any other
    >suggestion ?
    >
    >(The SEROUT command does not support 8 databits + parity bit !!)

    Each bit at 2400 baud is 417 microseconds. You might be able to send
    each byte with a subroutine such as the following. You have to enter
    the subroutine with the word variable serword already loaded with the
    11 bits that need to be transmitted, including the start bit, 8 data
    bits, parity bit (your program has to calculate parity), and the stop
    bit.

    ' word variable serit contains the assembled 11 bits
    ' right justified
    ' adjust the variable "padding" to get correct timing
    ' p1 is a dummy pulse that appears, just for timing.
    ' the out command takes about 220 microseconds,
    ' the pulsout takes another 220 microseconds, plus
    ' the length of the dummy pulse.

    Send_2400:
    output 0
    out0=serword.bit10 ' start bit
    pulsout 1,padding
    out0=serword.bit9
    pulsout 1,padding
    out0=serword.bit8
    pulsout 1,padding
    out0=serword.bit7
    pulsout 1,padding
    out0=serword.bit6
    pulsout 1,padding
    out0=serword.bit5
    pulsout 1,padding
    out0=serword.bit4
    pulsout 1,padding
    out0=serword.bit3
    pulsout 1,padding
    out0=serword.bit2
    pulsout 1,padding
    out0=serword.bit1
    pulsout 1,padding
    out0=serword.bit0 ' stop bit
    RETURN

    A FOR-NEXT loop probably would not work, because the FOR-NEXT logic
    takes about 800 microseconds.

    -- regards,
    Tracy
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-06 06:49
    > >Does anyone know if it's possible to send a serial string of 5 bytes
    >>with 1 startbit, 8 databits, 1 even paritybit and 1 stopbit with the
    >>BS2 ? The bitrate should be 2400 bps.
    >>Is it possible to get 2400 bps by using the low, high or toggle
    >>commands and send it bit by bit - or do you have any other
    >>suggestion ?
    >>
    >
    > >(The SEROUT command does not support 8 databits + parity bit !!)


    The question was how to transmit 8 bits, even parity, which is not
    one of the built-in modes for SEROUT. In actually playing with this
    a little more on a BS2, I found the following subroutine comes
    pretty close to 2400 baud, 417 microseconds per bit, and calculates
    the parity and sends out the data least significant bit first (rs232
    standard), with a start bit and a stop bit.

    '{$STAMP BS2}
    '{$PBASIC 2.5}
    ' word variable wx contains the assembled 11 bits
    ' right justified, including 8 bits & parity
    ' the subroutine send2400 adjusts the timing
    ' using ~~~ operators.

    wx VAR Word
    wx0 VAR wx.BIT0 ' alias first bit for implied array
    mybyte VAR byte ' byte to transmit
    i VAR nib ' index


    OUTPUT 2
    DO
    PULSOUT 1,10 ' 'scope trigger
    mybyte=$55 ' example value
    GOSUB send2400 ' send it over and over
    LOOP

    send2400:
    wx=mybyte
    FOR i=0 TO 7
    wx.bit8= wx0(i) ^ wx.bit8 ' calculate even parity
    NEXT
    wx = wx << 1 | 1 ' append start bit on right
    wx=wx ^ %11011011011 ' pre-compensate for inversions to follow
    OUT2= ~~~wx0(0) ' start bit
    OUT2= ~~~wx0(1) ' data bit 0
    OUT2= ~~~~wx0(2) ' the ~~~~ or ~~~ pad the time per bit
    OUT2= ~~~wx0(3)
    OUT2= ~~~wx0(4)
    OUT2= ~~~~wx0(5)
    OUT2= ~~~wx0(6)
    OUT2= ~~~wx0(7)
    OUT2= ~~~~wx0(8) ' data bit 7
    OUT2= ~~~wx0(9) ' even parity
    OUT2= ~~~wx0(10) ' stop bit
    RETURN

    A suggestion I made in an earlier post, using PULSOUT to pad the
    timing, simply takes too long on the BS2. It can't get down to 417
    microseconds per bit. It would work for 1200 baud, though!

    -- Tracy
Sign In or Register to comment.