While programming challenges seem to be popular, with some very clever lateral-thinking going on, I thought I'd dig out my favorite chunk of optimised code - I wrote it quite a while ago, but even with much more PIC experience, I still can't think of any more optimisations, other than maybe omitting the first skpnc, but this would cause (admittedly short) output glitches. ....................................................................... Extremely compact routine for sending asynchronous serial data at 9600 baud (with 4mhz osc). Uses only 16 instructions and 2 registers. Its compactness makes it useful for temporarily adding to programs for sending debug data etc. Number of stop bits can be increased up to 6 if the receiver needs gaps between bytes. The speed isn't exactly 9600, but is only 0.16% fast. Output polarity is inverted, for direct feeding into a PC RS232 port. For use with an inverting RS232 driver, swap the bsf and bcf >instructions. txbyte ; send byte in W at 9600 baud (4MHz osc), 8N1 movwf temp movlw 0a ; 8 data + 1 start + 1 stop (increase for more stop ; bits) movwf cnt ; cnt is used as bit counter *and* a delay loop counter! clc ; initial '1' for start bit txloop skpnc ; _____ bcf serport,dout ; output bit = carry skpc bsf serport,dout movlw 010 ; delay loop increment - bits 4..7 only dloop ; bit time delay goto $+1 ; 2-cycle NOP addwf cnt ; increment upper 4 bits only skpc goto dloop ; loop sixteen times rrf temp ; Shift next bit out, carry set & shifted in as ; stop bit(s) decfsz cnt goto txloop retlw 0 ......................................................................... ____ ____ _/ L_/ Mike Harrison / White Wing Logic / wwl@netcomuk.co.uk _/ L_/ _/ W_/ Hardware & Software design / PCB Design / Consultancy _/ W_/ /_W_/ Industrial / Computer Peripherals / Hazardous Area /_W_/