Wanting to transmit data at 9600 baud I will have a period of 104us right >? That means that to transmit a "1" I would have to drive a pin high for >52us and low for 52us right ??? To transmit a "0" I would just drive the >pin low for 104us ? I know that regular RS232 is low marking but I will be >communicating with another PIC so mine will be high marking. ( if I HAVE to >I could modify the code to be low marking ) > Thanks again for the help. > >James and Iliana Holbrook >Austin Texas > I am enclosing code written a couple of years ago for serial TX. Note that THREE different routines are included: 9600, 19200, 38400 baud. You can change the code to be LO or HI marking by swapping the 2 instructions noted. These routines are based upon ideas from both Mike Harrison and Andy Warren and use only 15 code spaces and 1 register. The speed isn't exact, but is only 0.16% fast. The output polarity is true and can directly feed a RS232 driver (MAX232). For inverted data (to directly feed an RS232 (PIC) input), swap the bsf and bcf instructions. There are 3 different versions for 9600, 19200, and 38,400 baud. All routines assume 4 MHz clock. The routines use the data format 8N1. Increase the .10 by up to .15 if you need more stop bits (this will affect the cycle times mentioned). Note that W is used as the delay counter. ;TXBYTE: send 1 byte @ 9600 baud: 1 RAM, 15 (16) ROM, 1043 cycles ;enters with data in w, exits with w trashed. ;Copyright (C) 1995 Dwayne Reid. May be freely used so long as this ;copyright notice is retained. txbyte ; send byte in W at 9600 baud (4MHz osc), 8N1 movwf temp movlw .10 ; 8 data + 1 start + 1 stop (inc for more stop bits) clrc ; start bit txloop ; bit time = 104.167 uSec skpnc bsf serport,dout ; output bit = carry skpc bcf serport,dout dloop ; 95 clk cycle delay goto $+1 ; 2-cycle NOP in 1 instruction! addlw b'00010000' ; increment upper nibble skpc ; delay = 6n -1 (1 less when falls thru) goto dloop ; loop sixteen times addlw -1 ; dec w, valid z, c=1 if w>=0 after decrement rrf temp,F ; carry will be set - shifted in as stop bit skpz goto txloop ; txloop is 104 clk cycles return ;TXBYTE: send 1 byte @ 19200 baud: 1 RAM, 14 (15) ROM, 523 cycles ;enters with data in w, exits with w trashed. ;Copyright (C) 1995 Dwayne Reid. May be freely used so long as this ;copyright notice is retained. txbyte ; send byte in W at 19200 baud (4MHz osc), 8N1 movwf temp ; 8 data + 1 start + 1 stop (inc for more stop bits) movlw (b'01010000'+.10) ; allow dloop to execute only 11 times clrc ; start bit txloop ; bit time = 52.083 uSec skpnc bsf serport,dout ; output bit = carry skpc bcf serport,dout dloop ; 43 clk cycle delay addlw b'00010000' ; increment upper nibble skpc ; delay = 4n -1 (1 less when falls thru) goto dloop ; loop 11 times addlw -1 ; dec w, valid z, c=1 if w>=0 after decrement rrf temp,F ; carry will be set - shifted in as stop bit skpz goto txloop ; txloop is 52 clk cycles return ;TXBYTE: send 1 byte @ 38400 baud: 1 RAM, 15 (16) ROM, 263 cycles ;enters with data in w, exits with w trashed. ;Copyright (C) 1995 Dwayne Reid. May be freely used so long as this ;copyright notice is retained. txbyte ; send byte in W at 19200 baud (4MHz osc), 8N1 movwf temp ; 8 data + 1 start + 1 stop (inc for more stop bits) movlw (b'11000000' + .10) ; allow dloop to execute only 4 times clrc ; start bit txloop ; bit time = 26.042 uSec skpnc bsf serport,dout ; output bit = carry skpc bcf serport,dout goto $+1 ; 2 cycle NOP dloop ; 15 clk cycle delay addlw b'00010000' ; increment upper nibble skpc ; delay = 4n -1 (1 less when falls thru) goto dloop ; loop 4 times addlw -1 ; dec w, valid z, c=1 if w>=0 after decrement rrf temp,F ; carry will be set - shifted in as stop bit skpz goto txloop ; txloop is 26 clk cycles return I hope that people find these useful. As I said before, the core idea came from Mike Harrison. All I did was take a couple of suggestions from Andy Warren and some ideas of my own to shorten them and reduce RAM usage. Enjoy! Dwayne Reid Trinity Electronics Systems Ltd Edmonton, Alberta, CANADA (403) 489-3199 voice (403) 487-6397 fax