Hi all This is my first attempt at rs232 comms. I've written a simple interrupt driven routine that does Half duplex, 19200,8,n,1 using a 4MHz crystal on a 16F84-4 (I would have used a 10Mhz pic but my supplier was out of stock at the time). I'd appreciate if you can offer advice and or criticism(preferably constructive) on my code. The routine simply echo's whatever is recieved. I've tested it and it appears to work fine but I'd like to know if it works for you. So if you have a 16F84 or similar test jig handy and a little time, I'd appreciate your comments (email privately if you like). There are a few obvious things I can fix up - like variable definitions based on freq & baud for the timeout values instead of hardcoded values(which is simpler)... later. Thanks Jacques Vrey Pin Designation: ---------------- RA4 - RX RA3 - TX Im using a MAX232 for the driver. ;;CODE BEGIN: ;;----------- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; RS232.ASM - Half duplex interrupt based serial comms at 19200,8,n,1 ;; Jacques Vrey ;; 04/01/2001 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; LIST P=PIC16F84 LIST F=INHX8M __CONFIG _CP_OFF & _PWRTE_ON & _WDT_OFF & _XT_OSC include "p16f84.inc" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; Define ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; RXTIMEOUT equ h'e3' ;RX Timeout Value for Timer TXTIMEOUT equ h'e1' ;TX Timeout Value for Timer STARTBITDELAY equ h'dd' ;Start Timeout Value for Timer COUNTER equ b'11111111' ;Register setup for counter TIMER equ b'11011111' ;Register setup for timer SAVED_W equ h'00'+h'0C' ;SRAM register SAVED_STATUS equ h'01'+h'0C' ;SRAM register SERIALSTATUS equ h'02'+h'0C' ;SRAM register BITPOS equ h'03'+h'0C' ;SRAM register LOOPCOUNTER equ h'04'+h'0C' ;SRAM register SENDBUF equ h'05'+h'0C' ;SRAM register RECBUF equ h'06'+h'0C' ;SRAM register #define TX_BIT porta,3 #define RX_BIT porta,4 #define TRANSMIT SERIALSTATUS,0 #define RECEIVE SERIALSTATUS,1 #define BYTEINBUFFER SERIALSTATUS,2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; End Define ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ORG 0 goto START ORG 4 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; Interrupt ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; INTERRUPT ;Disable Interrupts bcf intcon,gie ;Disable Global ;Check if TMR0 interrupt - if not then exit interrupt routine btfss intcon,t0if retfie ;Save status & w movwf SAVED_W swapf status,w movwf SAVED_STATUS ;Do things btfsc TRANSMIT goto TRANSMITBIT btfsc RECEIVE goto RECEIVEBIT goto STARTBIT ;Restore Registers RESTOREREGS swapf SAVED_STATUS,w movwf status swapf SAVED_W,f swapf SAVED_W,w bcf intcon,t0if ;Enable Interrupts bsf intcon,gie ;Enable Global retfie ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; End Interrupt ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; Enable Interrupts ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ENABLEINTERRUPTS clrf intcon ;Disable All bsf intcon,gie ;Enable Global bsf intcon,t0ie ;Enable TMR0 Interrupt return ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; End Enable Interrupts ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; Setup ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; SETUP clrf SERIALSTATUS clrf RECBUF clrf SENDBUF bsf status,rp0 ;Bank 1 movlw h'10' ;PortA RA4 as Input, rest Output movwf porta clrf portb ;PortB all Output movlw COUNTER ;TMR0 - Counter movwf tmr0 bcf status,rp0 ;Bank 0 bsf TX_BIT ;RA4 High clrf portb ;PortB all Low movlw h'ff' movwf tmr0 return ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; End Setup ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; Transmit Bit ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; TRANSMITBIT decfsz BITPOS,f goto TXNEXTBIT goto TXSTOPBIT TXNEXTBIT btfss SENDBUF,0 goto TXLOW bsf TX_BIT goto TXBITDONE TXLOW bcf TX_BIT TXBITDONE rrf SENDBUF,f movlw TXTIMEOUT movwf tmr0 goto RESTOREREGS TXSTOPBIT bsf TX_BIT movlw h'06' movwf LOOPCOUNTER STOPLOOP ;Loop to ensure valid stop bit decfsz LOOPCOUNTER,f goto STOPLOOP bsf status,rp0 ;Bank 1 movlw COUNTER movwf tmr0 bcf status,rp0 ;Bank 0 movlw h'ff' movwf tmr0 bcf TRANSMIT bcf BYTEINBUFFER goto RESTOREREGS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; End Transmit Bit ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; Receive Bit ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; RECEIVEBIT decfsz BITPOS,f goto NEXTBIT goto BYTEDONE NEXTBIT rrf RECBUF,f btfsc RX_BIT goto HIGHBIT bcf RECBUF,7 goto BITDONE HIGHBIT bsf RECBUF,7 BITDONE movlw RXTIMEOUT movwf tmr0 goto RESTOREREGS BYTEDONE bsf status,rp0 ;Bank 1 movlw COUNTER movwf tmr0 bcf status,rp0 ;Bank 0 movlw h'ff' movwf tmr0 bcf RECEIVE bsf BYTEINBUFFER goto RESTOREREGS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; End Receive Byte ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; Start Bit ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; STARTBIT bcf status,rp0 ;Bank 0 btfsc RX_BIT goto NOTSTARTBIT movlw 9 movwf BITPOS bsf RECEIVE bcf BYTEINBUFFER movlw STARTBITDELAY ;Initial Bit Delay movwf tmr0 bsf status,rp0 ;Bank 1 movlw TIMER movwf tmr0 bcf status,rp0 ;Bank 0 goto RESTOREREGS NOTSTARTBIT movlw h'ff' movwf tmr0 goto RESTOREREGS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; End Start Bit ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; Transmit Byte ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; TRANSMITBYTE bsf TRANSMIT bcf status,rp0 ;Bank 0 movlw TXTIMEOUT movwf tmr0 movlw 9 movwf BITPOS bcf TX_BIT bsf status,rp0 ;Bank 1 movlw TIMER movwf tmr0 bcf status,rp0 ;Bank 0 return ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; End Transmit Byte ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; Work ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; WORK btfss BYTEINBUFFER goto WORK btfsc TRANSMIT goto WORK movf RECBUF,w ;Comment this line to test transmit ; movlw h'40' ;Uncomment this line to transmit "@" movwf SENDBUF call TRANSMITBYTE goto WORK ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; End Work ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; Start ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; START call ENABLEINTERRUPTS call SETUP goto WORK ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;; End Start ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; END ;;CODE END. ;;--------- -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.