Lecture 5 Serial communication Prof Tania Hanekom

jazzydoeSoftware and s/w Development

Oct 30, 2013 (3 years and 10 months ago)

138 views

Electrical, Electronic & Computer Engineering


EMK310

Lecture 5


Serial communication


Prof Tania Hanekom



References:



Projects.pdf


PIC18F45K20 datasheet


PICmicro® 18C MCU Family Reference Manual



Huang, Chapters 9
-

11


Electrical, Electronic & Computer Engineering


This lecture


Serial communication


RS232


USART


I
2
C


EEPROM

Electrical, Electronic & Computer Engineering


RS232

References


Study all these documents, the information contained
in them will be examinable


http://www.taltech.com/TALtech_web/resources/intro
-
sc.html


http://en.wikipedia.org/wiki/Rs232


(available on EMK310 website)


PIC18F45K20 datasheet, section 18


PIC18 reference manual, section 21


Huang, Chapter 9

Electrical, Electronic & Computer Engineering


Introduction to RS232


RS232 is an asynchronous or synchronous
serial communications protocol


Asynchronous implemented on IBM PCs:


no
synchronising clock signal


it has to synchronise itself to the incoming data


'START' and 'STOP' pulses


signal itself
:

+12V to
-
12V


MAX232 chip


chip specially designed to interface between 5V logic
levels and the +12V/
-
12V of RS232

Electrical, Electronic & Computer Engineering


RS232 protocol


V
arious data types and speeds used for
RS232


most common type known as 8N1


8 signifies '8 Data Bits‘


N signifies 'No Parity‘


1 signifies '1 Stop Bit‘


total data sent consists of

1 start bit, 8 data bits, and 1
stop bit
= total

of 10 bits.


Electrical, Electronic & Computer Engineering


RS232 baud rate


Intended for baud rates less than 20000
BAUD


19200 BAUD rate (prac 2)


19200 bits per second/10 = 1920 words/s


52 us per bit


This value is crucial to successful RS232
communication


doesn't have to be exact as the stop pulse allows
resynchronisation after each data byte, but it must be
accurate enough to maintain reading in the correct bit
throughout each byte

Electrical, Electronic & Computer Engineering


RS232 connector


RS232 pin assignments (DB9 PC signal set)


Pin 1


Data carrier detect


Pin 2


Received data


Pin 3


Transmit data


Pin 4


Data terminal ready


Pin 5


Signal ground


Pin 6


Data set ready


Pin 7


Request to send


Pin 8


Clear to send


Pin 9


Ring indicator


Electrical, Electronic & Computer Engineering


RS232 on PIC (1)


PIC18F45K20


EUSART module or serial communications interface


Full
-
duplex asynchronous system (can receive and transmit
simultaneously)


Pin RC6/TX/CK = transmit line


Pin RC7/RX/DT = receive line


Electrical, Electronic & Computer Engineering


RS232 on PIC (2)


Asynchronous transmission setup: datasheet p. 238


Asynchronous reception setup: datasheet p. 242


Baud rate setup


Equation on p. 247


Use high speed baud rate generator (bit BRGH in TXSTA =
1) for better accuracy



Electrical, Electronic & Computer Engineering


RS232 on PIC (2)


Example of setup of USART on PIC16F887 for
asynchronous communication



MOVLW d‘207'


; 19200 baud @ 64
Mhz

Fosc

+0.16% err






; table 18
-
5 of datasheet


MOVWF SPBRG


; load
baudrate

register


BSF

TXSTA,BRGH

; High
BAUDrate


BCF BAUDCON,BRG16; BRG operates in 8 bit mode


movlw

b'11000000‘

; Setup port C for serial port.



movwf

TRISC


; TRISC<7>=1 and TRISC<6>=1.


BCF

TXSTA,SYNC

; enable asynchronous transmission


BSF

RCSTA,SPEN


;
Enable serial port.


BSF

TXSTA,TXEN ; enable transmission


BSF

PIE1,RCIE ; enable receive interrupt


BSF

RCSTA, CREN

; Enable continuous reception


Electrical, Electronic & Computer Engineering


RS232 on PIC (3)


To read a byte


Read RCREG register


To write a byte


Move byte into TXREG register

Electrical, Electronic & Computer Engineering


You will need to design & build a …

… USB
-
to
-
RS232 converter because most of the the PCs
in the labs do not have serial ports anymore.



http://www.lammertbies.nl/comm/info/RS
-
232
-
usb.html
provides
some technical info on USB
-
to
-
RS232 converters.


Voltage levels


Protocols:


USB fixed


RS232 open



Refer to Application notes on USB
-
to
-
serial converters.

Electrical, Electronic & Computer Engineering


USB
-
to
-
serial bridge


Install Microchip Application Libraries (RS232 to
USB).


"Hack" the relevant files to decrease the buffer sizes


library written for PIC18F14K50 which has more
memory.


Instructions on EMK310 website.

Electrical, Electronic & Computer Engineering


I
2
C bus

References


Study all these documents, the information contained
in them will be examinable


http://en.wikipedia.org/wiki/I2C (on website)


EEPROM datasheet (study guide/ website)


PIC18F45K20 datasheet Section 17


PIC18 reference manual Sections 19 & 20


Huang, Chapters 10 & 11

Electrical, Electronic & Computer Engineering


MSSP on PIC


MSSP (Master Synchronous Serial Port) module


interface used to communicate with other peripheral
or
μ
C (microcontroller) devices.


serial EEPROM
, shift registers, display drivers, A/D
converters, etc.


operate in one of two modes


Serial Peripheral Interface (SPI™)


Inter
-
Integrated Circuit (I
2
C™)

Electrical, Electronic & Computer Engineering


I
2
C


I²C uses two bidirectional open
-
drain lines, serial
data (SDA) and serial clock (SCL), pulled up with
resistors.


7
-
bit address space with 16 reserved addresses


maximum of 112 nodes can communicate on the same bus


Most common bus modes


10 kbit/s low
-
speed mode


100 kbit/s standard mode


400 kbit/s fast mode


3.4 Mbit/s High Speed mode


other extended features


e.g 10
-
bit

addressing

Electrical, Electronic & Computer Engineering


Pin description 24C02C EEPROM


Vss = Ground


SDA = Serial Data


SCL = Serial Clock


VCC = +4.5V to 5.5V


A0, A1, A2 = Chip Selects


(address lines)


WP = Hardware Write
-
Protect


Electrical, Electronic & Computer Engineering


SDA Serial Data


Bidirectional pin transfer
addresses and data into

and
data out

of the device.


Open drain terminal, therefore the SDA bus requires
a pull
-
up resistor to VCC (typical 10 kΩ for 100 kHz, 2
kΩ for 400 kHz).

Electrical, Electronic & Computer Engineering


SCL Serial Clock


Synchronize the data transfer from and to the device


Master device controls clock


i.e. PIC in
Prac

2


Electrical, Electronic & Computer Engineering


A0, A1, A2


Hardwire A0, A1 and A2 to appropriate voltage levels
e.g. A0 = 0V, A1 = 0V, A2 = 0V


The levels on these inputs are compared with the
corresponding bits in the slave address.


The chip is selected if the compare is true.

Electrical, Electronic & Computer Engineering


Bus protocol (1)


Bus not Busy


Data transfer initiated only when bus is not busy.


Both data and clock lines remain high.


Start Data Transfer


A
high
-
to
-
low
transition of the SDA line while the clock
(SCL) is
high

determines a Start condition. All commands
must be preceded by a Start condition.


Stop Data Transfer


A
low
-
to
-
high
transition of the SDA line while the clock
(SCL) is
high

determines a Stop condition. All operations
must be ended with a Stop condition.


Data Valid


The state of the data line represents valid data when, after a
Start condition, the
data line is stable for the duration
of the high period of the clock signal
.


The data on the line must be
changed during the low
period of the clock

signal. There is one bit of data per
clock pulse.

Electrical, Electronic & Computer Engineering


Bus protocol (2)


Timing diagram

Electrical, Electronic & Computer Engineering


Acknowledge


Each receiving device, when addressed, is required to
generate an acknowledge after the reception of each
byte.


The master device must generate an extra clock pulse which
is associated with this Acknowledge bit.


The device that acknowledges has to pull down the
SDA line during the Acknowledge clock pulse in such
a way that the SDA line is stable low during the high
period of the acknowledge related clock pulse.


Electrical, Electronic & Computer Engineering


Interface with PIC

Three steps to read/write memory

1.
Send device address (Control word)

2.
Send data address


If data are read out sequentially, this may be omitted

3.
Send/receive data byte


Timing important


EEPROM
(e.g., Atmel
AT24C01) operates @

400
kHz max


Include appropriate delays

Electrical, Electronic & Computer Engineering


Byte write (1)


Control word


1010 for read/write
operations


A0,A1,A2 = chip select


R/W = read/write

Electrical, Electronic & Computer Engineering


Byte write (2)

Electrical, Electronic & Computer Engineering


Practical 2


You must HARD CODE the I
2
C protocol for Practical 2
because:


it will provide you with experience in coding applications
where synchronization and timing are important.


it will provide you with general coding experience.


it will provide you with a good opportunity to debug a
lengthy piece of code.


For an additional 2 bonus marks, you may also
implement the practical using the PIC’s onboard
MSSP.

Electrical, Electronic & Computer Engineering


Examples


Application notes on Microchip website


AN554: I
2
C Bus Master Implementation


AN774: Asynchronous Communications with the
PICmicro
TM

USART