Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Embedded Systems
Muhammad Ali Mazidi, Rolin McKinlay and Danny Causey
10-1
10-2
Objective
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
10-3
Outlines
Programming timers 0 and 1
Counter Programming
10-4
Introduction
Computers transfer data in two ways:
10-5
10-6
Transfers a single
byte at a time
The PIC uCs
Transfers a
block of data at
a time
10-7
Half-and Full-Duplex
Transmission
The
The PIC
PICuCs
uCs
10-8
LSB
10-9
second)
Another widely used terminology for bps is
baud rate
For Asynchronous serial data
communication, the baud rate is generally
limited to 100,000bps
10-10
RS232 Standard
Standard for serial comm (COM port)
1: -3V to -25V;
0: +3V to +25V
Reason: for long distance wired line
Input-output voltage are not TTL
compatible
So, we need MAX232/233 for voltage
converter. Commonly known as line drivers
10-11
RS232 Pins
Connectors:
Minimally, 3 wires: RxD, TxD, GND
Could have 9-pin or 25-pin
The
The PIC
PICuCs
uCs
DB-25
DB-9
25-Pin Connector
9-Pin Connector
10-12
Data out
Pin
Pin
Pin
Pin
Pin
Pin
Pin
Pin
Pin
DB-9
9-Pin Connector
The
The PIC
PICuCs
uCs
10-13
Line driver
(a) Inside MAX232
The
The PIC
PICuCs
uCs
10-14
10-15
Line driver
(a) Inside MAX232
The
The PIC
PICuCs
uCs
10-16
Line driver
PIC18
The
The PIC
PICuCs
uCs
10-17
Synchronous
Asynchronous
Rx Port
6 registers
SPBRG
TXREG
RCREG
TXSTA
RCSTA
PIR1
Tx Port
10-18
is programmable
loaded into the
SPBRG decides
the baud rate
Depend on crystal
frequency
BR =
F Fosc
4*16*(X+1)(
Baud
Rate
SPBRG
(Hex Value)
38400
19200
9600
4800
2400
1200
3
7
F
20
40
81
10-19
10-20
TXREG Register
8-bit register used for serial
10-21
RCREG Register
8-bit register used for serial
10-22
The
The PIC
PICuCs
uCs
10-23
The
The PIC
PICuCs
uCs
10-24
The
The PIC
PICuCs
uCs
10-25
The
The PIC
PICuCs
uCs
10-26
The
The PIC
PICuCs
uCs
10-27
10-28
Example 10.2
;Write a program for the PIC18 to transfer the letter 'G' serially
;at 9600 baud continuously. Assume XTAL = 10 MHz
OVER
S1
The
The PIC
PICuCs
uCs
MOVLW
MOVWF
MOVLW
MOVWF
BCF
BSF
MOVLW
BTFSS PIR1,
BRA
MOVWF
BRA
B'00100000'
TXSTA
D'15'; 9600 bps
SPBRG
TRISC, TX
RCSTA, SPEN
A'G'
TXIF
S1
TXREG
OVER
10-29
10-30
10-31
Example 10.4
;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
R1
The
The PIC
PICuCs
uCs
MOVLW
MOVWF
MOVLW
MOVWF
BSF
CLRF
BTFSS PIR1,
BRA
MOVFF
BRA
B'10010000'
RCSTA
D'15'
SPBRG
TRISC, RX
TRISB
RCIF
R1
RCREG, PORTB
R1
10-32
TXSTA.BRGH bit
Normally used low
Can be set high
Quadruples rate when set high
10-35
10-36
10-37