BIt Banged Serial -- UART use is preferred
 
Library


#include <SERIAL.bas>        ' PBASIC library, these routines were written for ARMexpress, and included for general reference,UART functions should be used instead of these.

Library sources are in the BASIClib directory.

This library has some initialization code that can either be copied into your program or the code can be run inline as in the following-

code without a main:

#include <SERIAL.bas>
... user code

code with a main:

initSerial:
#include <SERIAL.bas>
return
...
main:
    gosub initSerial
 

B

  • bbBAUD

R

  • bbRXD
S
  • SERIN
  • SERINtimeout
  • SEROUT
T

  • bbTXD
 
Interface
 
DIM bbBAUD(16)
SERINtimeout = 500000            ' timeout for bit-banged serial input in microseconds  -- this is the 0.5 second default value
 
FUNCTION bbRXD(pin)
SUB bbTXD(pin, ch)

FUNCTION SERIN (pin, baud, posTrue, INcnt, BYREF INlist as string)
SUB SEROUT( pin, baud, posTrue, OUTcnt, BYREF OUTlist AS STRING)
 
Description
 
SERIN receives INlist bytes as asynchronous serial data on pin at a baudratePosTrue  if set to 0 then the data is inverted.

INcnt is the number of bytes that will be received. If INcnt is 0, then the string will be filled with bytes until a 0, CR or LF character is received.  Note that no bounds checking is performed on the input, and if a 0, CR, or LF is never received then this routine will hang.    As there is no bounds checking its possible to overwrite other variables, if less than 256 bytes have been allocated for the InputList string.

SERIN will timeout after 0.5 seconds and return -1 and place 255 in the next item in the INlist before the timeout.  These routines are "bit-banged" by the processor, so the processor is consumed during these operations.  Interrupts are also disabled during each byte for these operations.  The hardware UART0 can be used see RXD0 or DEBUGIN .  The timeout can be changed with SERINtimeout.

Baudrates can be up to 115.2 Kbaud for all pins on transmit.  Receive rates to 57Kb

DIM choice(10) as STRING

SERIN(1,9600,0, choice)        ' read a UserCode CR/LF terminated

SELECT VAL (choice)
  CASE 123 ...


SEROUT sends a string of characters out on pin as an asynchronous data stream.  baud and posTrue set the parameters for the transmission. OUTcnt is the number of bytes that will be transmitted.  If OUTcnt is 0, then OUTlist will be sent until a 0 is encountered (the 0 is not sent).


 
ch = bbRXD(pin)       ' read a character from pin as an asynchronous stream (bbBAUD must have been set before use)
 
bbRXD is a bit banged routine, so that the CPU will wait up to 0.5 seconds for a character to be received.  The timeout can be changed with SERINtimeout.
 
bbTXD(pin, "A")        ' send an "A" to pin as an asynchronous serial stream