ON 20050715@11:16:24 AM at page: http://www.sxlist.com/techref/scenix/lib/io/osi2/serial/rs232at9600on16F84noint_sx.htm#38548.35 James Newton[JMN-EFP-786] published post 38548.35 dkemppai@yahoo.com shares this code:
I use this site a lot. Here is a donation for the masses…

I referenced some code, and ported to the SX28 running turbo mode on a 4Mhz XTAL. The code was based on the code here: “http://www.sxlist.com/techref/ubicom/lib/io/osi2/serial/rs232at9600on16F84noint_sx.htm"

The RX routine was improved to correctly sample the data byte coming in. The original authors code should have waited longer before sampling the data for reliable read. The code improvement was to wait for 1.5 bits on reception of the start bit. This places the sampling in the center of each incoming bit. 

The RX routine was also modified to transmit a full stop bit. This gives terminal programs proper time to sync up to incoming start bits in a framed data stream of multiple bytes.   

The code has been tested at 9600,19200,38400,57600 baud. A oscilloscope was used to determing the minimum timing error for each bit. The code could be modified for other baud rates also. 

Enjoy! 
-Dan





       
; *************************************************
; **   Serial TX routine                         **
; **   TXreg must have data to be transmitted    **
; *************************************************
; TXpin		- is the bit used for TX pin. 
; TXreg		- is the byte to be transmitted
; delay		- is a counter to set serial delay
; count		- counts bits transmitted. 
;
; This routine transmits RS232 Data in the standard 
; format "N81". This routine transmits a full Stop 
; bit before returning.
;
; The baud rate based on a 4Mhz Xtal is set by 
; changing the value within the Bauddelay Constant: 
; TXBaudDelay equ	$65	;9600 Baud
; TXBaudDelay equ	$31	;19200 Baud
; TXBaudDelay equ	$17	;38400 Baud
; TXBaudDelay equ	$0E	;57600 Baud
RS232out	clrb	TXpin	;send start bit
		mov	W, #9
		mov	count, W
		mov	W, #$0E
		mov	delay, W
txbaudwait	decsz	delay
		jmp	$-1
		mov	W, #$0E
		mov	delay, W
		decsz	count
		jmp	sendnextbit
		setb	TXpin	;send stop bit
		decsz	delay
		jmp	$-1
		RETP
sendnextbit	rr	TXreg
		sb	C	
		jmp	setlo
		setb	TXpin	
		jmp	txbaudwait
setlo		clrb	TXpin	
		jmp	txbaudwait


; *********************************************
; **   Serial RX routine                     **
; **   RXreg Contains Data Received 	     **
; *********************************************
; Note:	RXpin   - is the bit used for RX pin. 
; 	RXreg - is the byte to be transmitted
;	delay - is a counter to set serial delay
;	count - counts bits received. 
;
; This routine Receives RS232 Data in the 
; standard format "N81".
;
; The baud rate based on a 4Mhz Clock is set by 
; changing the value of Bauddelay Constant: 
; RXBaudDelay	equ	$65	;9600 Baud
; RXBaudDelay	equ	$31	;19200 Baud
; RXBaudDelay	equ	$17	;38400 Baud
; RXBaudDelay	equ	$0E	;57600 Baud
;
; The routine centers the reading of each bit at 
; the center of each bit being received. This off
; set is accomplished by changing the first delay 
; to 1.5 times the normal bit delay. 
;
; The original Code read Data at the start of the
; valid data bit. This lead to unreliable RX at higher
; data transmission speeds (38.5kbaud,57.6kbaud). 
;
; Note that this only works for values where 
; RXBaudDelay * 1.5 < $ff.
RS232in 	snb	RXpin		;Loop Until Data in
		jmp	$-1
		mov	W, #9		;Initialize counter
		mov	count, W
		mov	W, #$0E	
		mov	delay, W	
		clc			;Clear Carry!
		rr	delay		;Divide by 2
		mov	W, #$0E	;Move baudrt to W
		add	delay, W	;Add to Delay
rxbaudwait	decsz	delay		
		jmp	$-1	
		mov	W, #$0E
		mov	delay, W
		decsz	count
		jmp	recvnextbit
		retp	
recvnextbit	clrb	C
		snb	RXpin
		setb	C
		rr	RXreg
		jmp	rxbaudwait
|Delete 'P-' before: '' but after: 'dkemppai@yahoo.com shares this code:
Whoops, posted the wrong chunk of code....

4 lines had literal value, should have been constants.

RS232out	clrb	TXpin	;send start bit
		mov	W, #9
		mov	count, W
		mov	W, #TXBaudDelay
		mov	delay, W
txbaudwait	decsz	delay
		jmp	$-1
		mov	W, #TXBaudDelay
		mov	delay, W
		decsz	count
		jmp	sendnextbit
		setb	TXpin	;send stop bit
		decsz	delay
		jmp	$-1
		RETP
sendnextbit	rr	TXreg
		sb	C	
		jmp	setlo
		setb	TXpin	
		jmp	txbaudwait
setlo		clrb	TXpin	
		jmp	txbaudwait



RS232in	snb	RXpin		;Loop Until Data
		jmp	$-1		;Jump portion of Loop

		mov	W, #9		;Initialize counter
		mov	count, W
		mov	W, #RXBaudDelay	
		mov	delay, W	
		clc			;Clear Carry!
		rr	delay		;Divide by 2
		mov	W, #RXBaudDelay	;Move baudrt to W
		add	delay, W	;Add to Delay
rxbaudwait	decsz	delay		
		jmp	$-1	
		mov	W, #RXBaudDelay
		mov	delay, W
		decsz	count
		jmp	recvnextbit
		retp	
recvnextbit	clrb	C
		snb	RXpin
		setb	C
		rr	RXreg
		jmp	rxbaudwait



|Delete 'P-' before: '' but after: '