Es Chapter10 (1)

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 37

PIC Microcontroller and

Embedded Systems
Muhammad Ali Mazidi, Rolin McKinlay and Danny Causey

Eng. Husam Alzaq


The Islamic Uni. Of Gaza

The PIC uCs 10-1


Chapter 10: PIC18
Serial Port
Programming in
Assembly.

PIC Microcontroller
and Embedded
Systems
Muhammad Ali
Mazidi, Rolin McKinlay
and Danny Causey,
February 2007.

The PIC uCs 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

The PIC uCs 10-3


Outlines
 Programming timers 0 and 1
 Counter Programming

The PIC uCs 10-4


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 PIC uCs 10-5


Basics of Serial
Communication
 The byte of data must be converted to
serial bits using a parallel-in-serial-out
shift register

The PIC uCs Serial versus Parallel Data Transfer 10-6


Basics of Serial Communication
(cont’d)
 The receiving end must be a serial-in-
parallel-out shift register and pack them
into a byte.
 Two methods of serial data
communication: Asynchronous and
Synchronous

Transfers a
Transfers a block of data
single byte at a at a time
time
The PIC uCs 10-7
Half-and Full-Duplex
Transmission

The
The PIC
PICuCs
uCs 10-8
Start and Stop Bits
 In the asynchronous method, each
character is placed between start and
stop bits (framing)

MSB LSB

Framing ASCII ‘A’ (41H)

The PIC uCs 10-9


Data Transfer Rate
 Rate of data transfer: bps (bits per
second)
 Another widely used terminology for bps
is baud rate
 For Asynchronous serial data
communication, the baud rate is
generally limited to 100,000bps

The PIC uCs 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

The PIC uCs 10-11


RS232 Pins
Connectors:
Minimally, 3 wires: RxD, TxD, GND
Could have 9-pin or 25-pin

DB-25 DB-9
25-Pin Connector 9-Pin Connector

The
The PIC
PICuCs
uCs 10-12
RS232 Pins (cont’d)

IBM PC DB-9 Signals

Data in Data out Pin 1 – Data Carrier Detect (DCD)


Pin 2 – Received Data (RxD)
Pin 3 – Transmitted Data (TxD)
Pin 4 – Data Terminal Ready (DTR)
Pin 5 – Signal Ground (GND)
Pin 6 – Data Set Ready (/DSR)
Pin 7 – Request to Send (/RTS)
Pin 8 – Clear to Send (/CTS)
Pin 9 – Ring Indicator (RI)

DB-9
9-Pin Connector

The
The PIC
PICuCs
uCs 10-13
PIC18 Connection to RS232

Line driver

(a) Inside MAX232 (b) its Connection to the PIC18

The
The PIC
PICuCs
uCs 10-14
Figure 10-6. Null Modem
Connection
 Null modem is a communication method to
connect two DTEs (computer, terminal,
printer etc.) directly using a RS-232 serial
cable.
 With a null modem connection the transmit
and receive lines are crosslinked.
 Depending on the purpose, sometimes also
one or more handshake lines are
crosslinked.

The PIC uCs 10-15


PIC18 Connection to RS232

Line driver

(a) Inside MAX232 (b) its Connection to the PIC18

The
The PIC
PICuCs
uCs 10-16
PIC18 Connection to RS232
(Cont’d)

Line driver

(a) Inside MAX233 (b) Its Connection to the PIC18

The
The PIC
PICuCs
uCs 10-17
Section10.3: PIC18 Serial Port
Programming in Assembly
 USART has both
 Synchronous
 Asynchronous

 6 registers
 SPBRG
 TXREG
Rx Port
 RCREG
 TXSTA
 RCSTA
 PIR1

The PIC uCs Tx Port 10-18


SPBRG Register and Baud
Rate in the PIC18
 The baud rate in
is programmable Baud SPBRG
Rate (Hex Value)
 loaded into the
SPBRG decides 38400 3
the baud rate 19200 7
 Depend on crystal 9600 F
frequency 4800 20
 BR = F Fosc 2400 40
4*16*(X+1)( 1200 81
*For XTAL = 10MHz only!
The PIC uCs 10-19
Baud rate Formula

If Fosc = 10MHz
X = (156250/Desired Baud Rate) - 1
Example:

Desired baud rate = 1200, Clock


Frequency = 10MHz

X = (156250/1200) – 1
X = 129.21 = 129 = 81H
The
The PIC
PICuCs
uCs 10-20
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 first.
 The moment a byte is written into
TXREG, it is fetched into a non-
accessible register TSR
MOVFF PORTB, TXREG
 The frame contains 10 bits
The PIC uCs 10-21
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 it in the RCREG register
MOVFF RCREG, PORTB

The PIC uCs 10-22


TXSTA (Transmit Status and
Control Register)

The
The PIC
PICuCs
uCs 10-23
TXSTA (Transmit Status and
Control Register) (Cont’d)

The
The PIC
PICuCs
uCs 10-24
RCSTA (Receive Status and
Control Register)

The
The PIC
PICuCs
uCs 10-25
RCSTA (Receive Status and
Control Register) (Cont’d)

The
The PIC
PICuCs
uCs 10-26
PIR1 (Peripheral Interrupt
Request Register 1)

The
The PIC
PICuCs
uCs 10-27
Programming the PIC18 to
Transfer Data Serially
1. TXSTA register = 20H: Indicating
asynchronous mode with 8-bit data frame,
low baud rate and transmit enabled
2. Set Tx pin an output (RC6)
3. Loaded SPBRG for baud rate
4. Enabled the serial port (SPEN = 1 in RCSTA)
5. The character byte to transmit must be
written into TXREG
6. Keep Monitor TXIF bit
7. To transmit next character, go to step 5

The PIC uCs 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

MOVLW B'00100000'
MOVWF TXSTA
MOVLW D'15'; 9600 bps
MOVWF SPBRG
BCF TRISC, TX
BSF RCSTA, SPEN
OVER MOVLW A'G'
S1 BTFSS PIR1, TXIF
BRA S1
MOVWF TXREG
BRA OVER
The
The PIC
PICuCs
uCs 10-29
TXSTA: Transmit Status and
Control Register

The PIC uCs 10-30


Programming the PIC18 to
Receive Data Serially
1. RCSTA register = 90H: To enable the
continuous receive in addition to the 8-
bit data size option
2. The TXSTA register = 00H: To choose
the low baud rate option
3. Loaded SPBRG for baud rate
4. Set Rx pin an input
5. Keep Monitor RCIF bit
6. Move RCREG into a safe place
7. To receive next character, go to step 5
The PIC uCs 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

MOVLW B'10010000'
MOVWF RCSTA
MOVLW D'15'
MOVWF SPBRG
BSF TRISC, RX
CLRF TRISB
R1 BTFSS PIR1, RCIF
BRA R1
MOVFF RCREG, PORTB
BRA R1
The
The PIC
PICuCs
uCs 10-32
Increasing the Baud Rate
 Faster Crystal
 May not be able to change crystal

 TXSTA.BRGH bit
 Normally used low
 Can be set high
 Quadruples rate when set high

The PIC uCs


Baud Rate Error
Calculation
 ??? Errors in the baud rate? Yep!
 Caused by using integer division in rate generator

Calculated baud rate  Desired Baud Rate )


Error 
Desired Baud Rate
where
FOSC
Calculated Baud Rate 
64 ( SPBRG  1)

The PIC uCs


Transmit and Receive
 Please see program 10-1: Page 412

The PIC uCs 10-35


Figure 10-12. Simplified
USART Transmit Block
Diagram

The PIC uCs 10-36


Chapter 10: Summary

Next: the final exam

The PIC uCs 10-37

You might also like