PIC18 Serial Port Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan chanhl@mail.cgu.edu.twcgu
Serial vs. parallel data transfer 2
Simplex, half-, and full-duplex transfers 3
Data framing in asynchronous serial communication Older systems: ASCII characters were 7-bit and number of stop bits were 2 (gives device sufficient time to organize itself before transmission of next byte) Modern systems: 8 ASCII bits and 1 stop bit ASCII A (41H) Parity bit odd: 1 is added for A or even: 0 is added for A 4
Data transfer rate (baud rate) Early IBM PC/XT was 100 ~ 9600 bps (bits per second) Pentium-based PC is as high as 56K bps Generally limited to 100K bps 5
RS232 connectors DB-9 6
RS232 standard RS232 voltage levels 1 : -3 ~ -25 V 0 : 3 ~ 25 V Line driver (voltage converter) Convert RS232 voltage levels to TTL voltage levels MAX232 MAX233 7
PIC18 connection to RS232 8
PIC18 UART (universal asynchronous receiver/transmitter) programming SPBGR (serial port baud rate generator) TXREG (transfer register) RCREG (receiver register) TXSTA (transmit status and control register) RCSTA(receive status and control register) PIR1 (peripheral interrupt request register1) 9
SPBRG register and baud rate Desired baud rate = Fosc/(4x16x(Y+1)) 1)) Fosc/4 instruction cycle The value for SPBGR Y = Fosc/64/BaudRate -1 10
Start bit detection in asynchronous Receiver Example: 8-bit, parity enabled 1 stop 8 cycles 16 cycles Tim Wilmshurst, An Introduction to the Design of Small-Scale Embedded Systems, Palgrave, 2001. 11
TXSTA: Transmit status and control register 12
TXSTA: Receive status and control register 13
PIR1 (interrupt control register 1) 14
Transmit the message YES serial at 9600 baud, 8- bit data, and 1 stop bit. Do this forever. MOVLW B 00100000 ; enable transmit and choose low baud MOVWF TXSTA MOVLW D 15 ; 9600 bps MOVWF SPBRG BCF TRISC, TX ; make TX pin an output BSF RCSTA, SPEN ; enable serial port OVER MOVLW A Y CALL TRANS MOVLW A E CALL TRANS MOVLW A S CALL TRANS MOVLW 0x0 ; null to purge the buffer CALL TRANS 15
Transmit the message YES (cont.) TRANS S1 BTFSS PIR1, TXIF ; wait until the last bit is gone BRA S1 MOVWF TXREG RETURN 16
Receive bytes of data serially and put them on PORTB. Set the baud rate 9600, 8-bit data, and 1 stop bit. MOVLW B 10010000 ; enable receive and serial port MOVWF RCSTA MOVLW D 15 ; 9600 bps MOVWF BSF CLRF SPBRG TRISC, RX ; make RX pin an input TRISB R1 BTFSS PIR1, RCIF ; check for ready BRA R1 MOVFF RCREG, PORTB BRA R1 17
Quadrupling the baud rate Baud rate for BRGH = 0 = Fosc/(4x16x(Y+1)) Baud rate for BRGH = 1 = Fosc/(4x4x(Y+1)) 18
Simplified USART transmit block diagram 19
Programming g serial communication interrupts 20
Read data from PORTD and transmit serially ORG 0000H GOTO MAIN ORG 0008H BTFSC PIR1, TXIF ; Is interrupt due to transmit? BRA TX_ISR RETFIE TX_ ISR ORG 0040H MOVFF PORTD, TXREG ; load new value, clear TXIF RETFIE 21
Read data from PORTD and transmit serially y( (cont.) ORG 00100H MAIN SETF TRISD ; make PORTD input MOVLW 0x20 ; enable transmit and choose low baud MOVWF TXSTA MOVLW D 15 ; 9600 bps MOVWF SPBRG BCF TRISC, TX ; make TX pin an output BSF RCSTA, SPEN ; enable serial port BSF PIE1, TXIE ; enable TX interrupt BSF INTCON, PEIE ; enable peripheral interrupts BSF INTCON, GIE ; enable interrupts globally OVER BRA OVER 22
Read data from PORTD and transmit serially while incoming data from serial port is sent to PORTB ORG 0000H GOTO MAIN ORG 0008H HI_ISRISR BTFSC PIR1, TXIF ;IsitTXinterrupt? interrupt? BRA TX_ISR BTFSC PIR1, RCIF ; Is it RC interrupt? TX_ ISR RC_ISR BRA RETFIE RC_ISR MOVFF PORTD, TXREG ; load new value, clear TXIF GOTO HI_ISR MOVFF RCREG, PORTB ; copy received data to PORTB GOTO HI_ISR 23
Read data from PORTD and transmit serially while incoming data from serial port to PORTB (cont.) ORG 00100H MAIN CLRF TRISB ; make PORTB output SETF TRISD ; make PORTD input MOVLW 0x20 ; enable transmit and choose low baud MOVWF TXSTA MOVLW D 15 ; 9600 bps MOVWF SPBRG BCF TRISC, TX ; make TX pin an output BSF TRISC, RX ; make RC pin an input MOVLW 0x90 ; enable receive and serial port MOVWF RCSTA BSF PIE1, TXIE ; enable TX interrupt BSF PIE1, RCIE ; enable receive interrupt BSF INTCON, PEIE ; enable peripheral interrupts BSF INTCON, GIE ; enable interrupts globally OVER BRA OVER 24
Reference M.A. Mazidi, R.D. Mckinlay, D Causey, PIC Microcontroller and Embedded Systems Using Assembly and C for PIC18, Pearson Education Inc., 2008. 25