PIC18 Serial Port Programming
PIC18 Serial Port Programming
Objectives
Explain serial communication protocol Describe data transfer rate and bps rate Interface the PIC18 with an RS232 connector Describe the main registers used by serial communication of the PIC18 Program the PIC18 serial port in Assembly
Introduction
Computers transfer data in two ways: Parallel and Serial. Parallel: Eight or more data lines, few feet only, short time Serial: Single data line, long distance The PIC18 has serial communication capability built into it.
The byte of data must be converted to serial bits using a parallel-in-serial-out shift register
In the asynchronous method, each character is placed between start and stop bits (framing)
MSB LSB
RS232 Standard
Input-output voltage are not TTL compatible So, we need MAX232/233 for voltage converter. Commonly known as line drivers
RS232 Pins
Connectors:
Minimally, 3 wires: RxD, TxD, GND
Could have 9-pin or 25-pin
Line driver
(a) Inside MAX232 (b) its Connection to the PIC18
Line driver
(a) Inside MAX233 (b) Its Connection to the PIC18
SPBRG (Serial Port Baud Rate Generator) Register and Baud Rate in the PIC18 The baud rate in SPBRG the PIC18 is Baud Rate (Hex Value) programmable 38400 3 The value loaded 19200 7 into the SPBRG 9600 F decides the baud 4800 20 rate 2400 40 Depend on crystal 1200 81 frequency
*For XTAL = 10MHz only!
TXREG Register
8-bit register used for serial communication in the PIC18 For a byte of data to be transferred via the Tx pin, it must be placed in the TXREG register
RCREG Register
8-bit register used for serial communication in the PIC18 When the bits are received serially via the Rx pin, the PIC18 deframes them by eliminating the START and STOP bit, making a byte out of data received and then placing in the RCREG register
Example
;Write a program for the PIC18 to transfer the letter 'G' serially ;at 9600 baud continuously. Assume XTAL = 10 MHz MOVLW MOVWF MOVLW MOVWF BCF BSF MOVLW BTFSS PIR1, BRA MOVWF BRA B'00100000' TXSTA D'15' SPBRG TRISC, TX RCSTA, SPEN A 'G' TXIF S1 TXREG OVER
OVER S1
Example
;Write a program for the PIC18 to receive data serially and ;put them on PORTB. Set the baud rate at 9600, 8-bit data ;and 1 stop bit MOVLW MOVWF MOVLW MOVWF BSF CLRF BTFSS PIR1, BRA MOVFF BRA B10010000' RCSTA D'15' SPBRG TRISC, RX TRISB RCIF R1 RCREG, PORTB R1
R1
Life is not so short but that there is always time enough for courtesy Quote by Ralph Waldo
Emerson