;------------------------------------------------------------------------------- ;----------------- Asynchronous Serial Receive Initialisation ----------------- ;------------------------------------------------------------------------------- serialinit bsf STATUS, RP1 ; Set RP1 to 0 bsf STATUS, RP0 ; Set RP0 to 1 ; Bank 3 Selected bsf BAUDCTL, BRG16 ; Sets 16-bit baud generator bcf STATUS, RP1 ; Set RP1 to 0 ; Bank 1 Selected bsf TRISB, 1 ; Both serial input and output must bsf TRISB, 2 ; be set as inputs. bsf TXSTA, BRGH ; Set high baud rate movlw d'8' movwf SPBRG movlw d'2' movwf SPBRGH ; Set baud rate to 9600 bcf TXSTA, SYNC ; Set to asynchronous mode bcf STATUS, RP0 ; Set RP0 to 0 ; Bank 0 Selected bsf INTCON, PEIE ; Turn on peripheral interrupts bsf RCSTA, SPEN ; Enable Serial Port bsf STATUS, RP0 ; Set RP0 to 1 ; Bank 1 Selected bsf PIE1, RCIE ; Turn on Serial Receive Interrupt bcf STATUS, RP0 ; Set RP0 to 0 ; Bank 0 Selected bsf RCSTA, CREN ; Turns on receive movf RCREG, w movf RCREG, w movf RCREG, w ; Clear contents to clear interrupt bsf INTCON, GIE ; Enable interrupts ;------------------------------------------------------------------------------- ;---------------------------- Interrupt Program ------------------------------ ;------------------------------------------------------------------------------- Interrupt movwf w_save movf STATUS, w clrf STATUS movwf status_save movf PCLATH, w movwf pclath_save movf RCREG, w movwf sertemp ; For example this line could set an ; action in motion such as save to movf pclath_save ; EEPROM or write to LCD. movwf PCLATH movf status_save, w movwf STATUS swapf w_save, f swapf w_save, w retfie