Sunteți pe pagina 1din 2

; FILENAME: Serial_USART_Rec_Trans.

asm ; TITLE: USART Demo Demonstration ; HARDWARE: PICDEM-2 board ; FREQUENCY: 4MHz ; ;************************************************************ ; This program demonstrates basic functionality of the USART. ; ; Port B is connected to 8 LEDs. ; When the PIC18C452 RECEIVES a word of data from the USART ; the value is displayed on the LEDs, and ; it is RE-TRANSMITTED to the host computer. ; Set terminal program to 9600 baud, 1 stop bit, no parity list p=18f4520 ; set processor type list n=0 ; supress page breaks in list file include <P18f4520.INC> ;************************************************************ ; Reset and Interrupt Vectors org goto org goto 00000h ; Reset Vector Start 00008h ; Interrupt vector for USART ISR_Receive

;************************************************************ ; Program begins here org Start clrf clrf bcf bsf bsf movlw movwf bsf bsf bcf bsf bsf bsf LATB TRISB ; Clear PORTB output latches ; Config PORTB as all outputs for LEDs 0x20

; -------- Transmit line setup TRISC,6 ; Make RC6 =TX an output for Transmit TXSTA,TXEN TXSTA,BRGH .25 SPBRG ; Enable transmit ; Select high baud rate ; 9600 baud @4MHz

; -------- Receive line setup RCSTA,SPEN ; Enable Serial Port ; for Receive OR Transmit RCSTA,CREN ; Enable continuous reception ; -------- Receive Interrupt setup PIR1,RCIF ; Clear RCIF Interrupt Flag PIE1,RCIE ; Set RCIE Interrupt Enable INTCON,PEIE ; Enable peripheral interrupts INTCON,GIE ; Enable global interrupts

;************************************************************ ; Main loop Main goto Main ; loop to self doing nothing

;************************************************************ ; Interrupt Service Routine when a Byte is Received ISR_Receive: btfss goto movlw andwf btfss goto movf movwf movwf goto RcvError bcf bsf movlw movwf goto PIR1,RCIF OtherInt 0x06 RCSTA,W STATUS,Z RcvError RCREG,W LATB TXREG ISREnd ; Did USART cause interrupt? ; No, some other interrupt ; ; ; ; ; ; ; ; Mask out unwanted bits Check for errors Was either error status bit set? Found error, flag it Get input data Display on LEDs Echo character back go to end of ISR, restore context, return

RCSTA,CREN ; Clear receiver status RCSTA,CREN 0FFh ; Light all LEDs PORTB ISREnd ; go to end of ISR, restore context, return

OtherInt goto $ ; Find cause of interrupt and service it before returning from ; interrupt. If not, the same interrupt will re-occur as soon ; as execution returns to interrupted program. ISREnd RETFIE FAST End

S-ar putea să vă placă și