PDA

View Full Version : Bit-Banged Serial Out



hippy
24-01-2006, 17:38
This code snippet might be of use to people who need low-speed serial outputs. This will output characters at 110 baud from an 08 on its Serial Out pin.

- MainLoop:
- FOR b2 = "A" TO "Z"
- GOSUB Tx
- NEXT
- b2 = CR : GOSUB Tx
- b2 = LF : GOSUB Tx
- PAUSE 1000
- GOTO MainLoop
-
- Tx:
- w0 = b2 ^ $FF * 2 | 1
- FOR b3 = 0 TO 9 ' 1 start, 8 data, 1 stop
- pins = bit0
- w0 = w0 / 2
- PULSOUT 1,613 ' 563 to 662 works on my PC
- NEXT
- RETURN

The value for PULSOUT was found by using a variable, changing it using a FOR-NEXT loop, printing its value out via the Tx routine and observing the output using a Terminal program. Then the mid-point was chosen. Take away 7 because the 16-bit constant is about 70uS slower to use than variables.

Uisng 'PULSOUT 1,20' gives 300 baud.

Better still ...

Tx:
w0 = b4 ^ $FF * 2 | $401 ' Use $801 for 2 stop bits
- TxBit:
- pins = bit0
- w0 = w0 / 2
- PULSOUT 1,645 ' 645=110 baud, 47=300 baud
- IF w0 <> 1 THEN TxBit
- RETURN

Edited by - hippy on 1/24/2006 6:10:58 PM