07 chapter 10 - serial communication
TRANSCRIPT
-
8/2/2019 07 Chapter 10 - Serial Communication
1/36
Chapter10Sections1,2,9,10
Dr.IyadJafar
Starting
with
Serial
-
8/2/2019 07 Chapter 10 - Serial Communication
2/36
Outline
Introduction
Synchronous
Serial
Communication AsynchronousSerialCommunication
PhysicalLimitations
OverviewofPIC16Series
The16F87xAUSART
Summary
2
-
8/2/2019 07 Chapter 10 - Serial Communication
3/36
Introduction
Microcontrollersneedtomovedatatoandfrom
externaldevices
Ingeneral,
two
approaches
Parallel Datawordbitsaretransferredatthesametime
Awire
is
dedicated
for
each
bit
Simpleandfastbutexpensive
Shortdistances
Serial Bitsaretransferredoneafteranotheroverthesamelink/wire
Requirescomplexhardwaretotransmitandreceive
Slow
Shortandlongdistances3
-
8/2/2019 07 Chapter 10 - Serial Communication
4/36
Introduction
Twomemoriesofthesamesize.However,one
usesparalleltransferwhiletheotherusesserial
4
-
8/2/2019 07 Chapter 10 - Serial Communication
5/36
SerialCommunication Bitsaretransferredoneafteranotheronthesame
wire!!!
Challenges Howtodistinguishthestartandendofthebit?
Howtodeterminethestartandendofaword?
Twoapproaches
Synchronousserialcommunication
Aseparateclocksignalissentinparallelwiththedata
Eachclockcyclerepresentsonebitduration
Asynchronousserialcommunication
Noclocksignal!
Timingis
derived
from
the
data
itself
5
-
8/2/2019 07 Chapter 10 - Serial Communication
6/36
SerialCommunication
6
Synchronous
Asynchronous
-
8/2/2019 07 Chapter 10 - Serial Communication
7/36
Data
inside
the
memory
and
microprocessor
is
formatted
in
parallel.Howtotransmititserially?
Shiftregisters
SerialCommunication
7
-
8/2/2019 07 Chapter 10 - Serial Communication
8/36
SynchronousSerialCommunication
8
General Serial Link
Synchronous link implemented using a microcontroller
-
8/2/2019 07 Chapter 10 - Serial Communication
9/36
SynchronousSerialCommunication
9
Advantages Simple hardware Efficient
High speed
Disadvantages Extra line for the clock
The bandwidth needed for the clock is twice the data bandwidth Data and clock may lose synchronization over long distance
-
8/2/2019 07 Chapter 10 - Serial Communication
10/36
AsynchronousSerialCommunication Noclocksignal!
Thetransmitter
and
receiver
should
operate
a
clockatthesameratethatisintegermultipleof
thedatarate(usually16)
Tosynchronizetheclocksofthetransmitterand
receiver,data
is
framed
with
astart
and
stop
bits
10
-
8/2/2019 07 Chapter 10 - Serial Communication
11/36
AsynchronousSerialCommunication Framing
11
-
8/2/2019 07 Chapter 10 - Serial Communication
12/36
AsynchronousSerialCommunication
Synchronization
12
-
8/2/2019 07 Chapter 10 - Serial Communication
13/36
PhysicalLimitations TimeConstanteffect
13
-
8/2/2019 07 Chapter 10 - Serial Communication
14/36
PhysicalLimitations TransmissionLineEffects
Characteristicimpedanceandreflections
Linesshould
be
terminated
properly
14
-
8/2/2019 07 Chapter 10 - Serial Communication
15/36
PhysicalLimitations ElectromagneticInterference
15
-
8/2/2019 07 Chapter 10 - Serial Communication
16/36
PhysicalLimitations GroundDifferentials
16
http://upload.wikimedia.org/wikipedia/en/1/1c/Differential_Signaling.png -
8/2/2019 07 Chapter 10 - Serial Communication
17/36
OverviewofthePIC16Series
Wehavealreadyseen
thePIC16F84A
Othermembersinthe
serieshavemore
features:
AdditionalI/Oports
MoreHWtimers
A/Dconverters
LCDDrivers
USARTs
SynchronousSerial
Comparators
.17
-
8/2/2019 07 Chapter 10 - Serial Communication
18/36
18
OverviewofthePIC16Series
-
8/2/2019 07 Chapter 10 - Serial Communication
19/36
19
OverviewofthePIC16SeriesInterruptLogicfor16F874A/16F877A
-
8/2/2019 07 Chapter 10 - Serial Communication
20/36
20
OverviewofthePIC16SeriesDevice Pins Features
16F873A
16F876A
28 3parallelports,
3counter/timers,
2capture/compare/PWM,
2serial,510bitADC,
2comparators
16F874A
16F877A
40 5 parallelports,
3counter/timers,
2
capture/compare/PWM,2serial,8 10bitADC,
2comparators
-
8/2/2019 07 Chapter 10 - Serial Communication
21/36
The16F87xAUSART The
16F87XA
family
has
aUniversal
Synchronous
AsynchronousReceiverTransmitter(USART)
Configurable
Halfduplex
synchronous
master
or
slave
Fullduplexasynchronoustransmitterandreceiver
TheUSARTsharespinswithPORTC
pin7being
the
receive
line
pin6beingthetransmitline
Operationinvolves thefollowingregisters
TXSTA (0x98) TXREG(0x19) RCSTA (0x18)
RCREG(0x1A) SPBRG(0x99) PIE1 (0x8C)
PIR1
(0x0C)
INTCON
(0x0B,
0x8B,0x10B,0x18B)TRISC (0x87)21
-
8/2/2019 07 Chapter 10 - Serial Communication
22/36
AsynchronousUSARTTransmitterBlockDiagramThe16F87xAUSART
22
-
8/2/2019 07 Chapter 10 - Serial Communication
23/36
The16F87xAUSART AsynchronousUSARTTransmitter OperationNotes
Datais
transmitted
LSB first
on
RC6 pin
TheshiftregisterTSR isbufferedbytheTXREG(19H)andisnot
accessibleasamemorylocation
TransmissioniscontrolledbytheTXEN bitwhichenablesthe
clocktostartthetransmission
Toenableserialtransmission onRC6,bitSPEN inRCSTA register
hastobeset
Totransmit
data,
it
must
be
loaded
in
the
TXREG.
It
is
transferred
toTSR immediatelyifnotransmissionorafterthestopbit from
previoustransmissionissentout
Transmissionstatusisprovidedbytwobits:
TXIFflaginPRR1registerindicatesthestatusofTXREG.Itis
setwhendataistransferredtoTSR.Itisclearedonwritingto
TXREG
TRMTflag
in
TXSTA
it
is
set
when
the
shift
register
is
empty
ParitybitcanbesentoutbyusingTXD9 bitand TX9inTXSTA23
-
8/2/2019 07 Chapter 10 - Serial Communication
24/36
The16F87xAUSARTTXSTA(98H)
24
-
8/2/2019 07 Chapter 10 - Serial Communication
25/36
The16F87xAUSART StepsforUsingtheasynchronoustransmitter
1. ClearTRISCbittoconfigureRC6asoutput
2. SettheSPBRG(0x99)registerandBRGH(TXSTA)bittochoose
theappropriatebaudrate
3. EnableasynchronousserialportbyclearingtheSYNC(TXSTA)
bitandsettingtheSPENbit(RCTSA)
4. Ifinterrupts
are
desired,
set
the
TXIE
(PIE1),
GIE
(INTCON),
andPEIE (INTCON) bits
5. If9bittransmissionisdesired,settheTX9(TXSTA)bit
6. EnabletransmissionbysettingtheTXEN(TXSTA),whichwill
setthe
TXIF
(PIR1)
bit
7. If9bittransmissionisselected,thentheninthbitshouldbe
loadedinTX9D(TXSTA)
8. Load
data
in
TXREG
(0x19)
to
start
the
transmission25
-
8/2/2019 07 Chapter 10 - Serial Communication
26/36
The16F87xAUSART Timingofasynchronoustransmission
Registersinvolvedinasynchronoustransmission
26
-
8/2/2019 07 Chapter 10 - Serial Communication
27/36
AsynchronousReceiver
The16F87xAUSART
27
Th 16F87 A USART
-
8/2/2019 07 Chapter 10 - Serial Communication
28/36
The16F87xAUSART AsynchronousUSARTReceiver OperationNotes
Datais
received
LSB first
on
RC7 pin
ReceptionisenabledbytheCREN bit
AttheheartoftheblockistheRSR register.Onceastopbitis
detected,dataistransferredtoRCREG register,ifitisempty,and
theRCIF
flag
is
set.
On
receive
interrupt
can
be
enabled
by
RCIE bit
TheRCREG isFIFO doublebufferedregister
can beusedtoreceivebyteswhilereceptioncontinuesinRSR
It
can
be
read
twice
to
read
the
received
two
bytes
IfastopbitisdetectedinRSRandtheRCREGisstillfull,anoverrunerror
occursandisindicatedinOERRbit(ThewordisRSRislost)
IfOERRbitisset,shiftingstopsinRSRandtransferstotheRCREGisinhibited!
Tocleartheframingerror,cleartheCRENbit.
IfthestopbitisreceivedasclearinRSR aframingerroroccursand
isindicatedbytheFERR bit.
The9th bitofdataRCD9 andFERR arealsodoublebuffered.Itis
essentialto
read
the
RCSTA register
before
the
RCREG to
avoid
losingthecorrespondingvaluesofRCD9 andFERR28
Th 16F87 A USART
-
8/2/2019 07 Chapter 10 - Serial Communication
29/36
The16F87xAUSARTRCSTA(18H)
29
-
8/2/2019 07 Chapter 10 - Serial Communication
30/36
The16F87xAUSART StepsforUsingtheasynchronousreceiver
1. SettheSPBEG(0x99)registerandBRGH(TXSTA)bittochoose
theappropriatebaudrate
2. Enable
asynchronous
serial
port
by
clearing
the
SYNC
(TXSTA)
bitandsettingtheSPENbit(RCTSA)
3. Ifinterruptsaredesired,settheRCIE(PIE1),GIE(INTCON),
andPEIE(INTCON)bits
4. If9bit
reception
is
desired,
set
the
RX9
(RCSTA)
bit
5. EnablethereceptionbysettingbitCREN(RCSTA)
6. TheRCIF(PIR1)willbesetwhenreceptionofonewordis
completeandaninterruptwillbegeneratedifRCIEisset
7. ReadtheRCSTA(0x18)togetthe9th bitanddetermineifany
erroroccurred(OERR,FERR)
8. Readthe8bitreceiveddatabyreadingRCREG(0x1A)
9. Ifany
error
occurred,
clear
the
error
by
clearing
the
CREN
30
-
8/2/2019 07 Chapter 10 - Serial Communication
31/36
The16F87xAUSART Timingofasynchronousreception
Registersinvolvedinasynchronousreception
31
-
8/2/2019 07 Chapter 10 - Serial Communication
32/36
SYNC BRGH=0 BRGH=1
0
(asynchronous)
1
(synchronous)
The16F87xAUSART TheBAUDRateGenerator
TheBAUDrateforUSARTiscontrolledbythevaluein
theSPREG(99H),theSYNCand theBRGH bitsinthe
TXSTA(19H)
32
64( 1)
oscF
SPBRG +
4( 1)
oscF
SPBRG +
16( 1)
oscF
SPBRG +
-
8/2/2019 07 Chapter 10 - Serial Communication
33/36
Example
Aprogramtotransmit3bytesstoredinlocations0x40,0x41,and0x42seriallywithnoparityatarateof9.6Kbps.AssumePIC16F877A withoscillatorfrequency of20MHz
Requirements1.setuptheserialportfortransmission
2.choosetheappropriatevalueofSPBRG and
BRGHto
produce
the
required
rate
33
-
8/2/2019 07 Chapter 10 - Serial Communication
34/36
-
8/2/2019 07 Chapter 10 - Serial Communication
35/36
Example
35
TX movf INDF, W ; read byte to transmit
movwf TXREG ; store in the transmission register
incf FSR, F ; increment FSR to point to next address
WAIT btfss PIR1, TXIF ; check if the TXREG is empty
goto WAIT
movf FSR,W
sublw 0x43btfss STATUS, Z ; check if all values were transmitted
goto TX
DONE goto DONEend
-
8/2/2019 07 Chapter 10 - Serial Communication
36/36
Summary
Serialcommunicationtransmitsbitsoneafteranother
intwomodes:synchronousandasynchronous
Stableand
accurate
clocking
plays
an
important
role
in
serialcommunication
Itischeapertouseserialcommunicationoverlong
distances Somemembersofthe16seriesareequippedwith
synchronousandasynchronouscommunicationports
Theseports
can
be
configured
to
operated
in
different
modesandrates
36