Serial Communication z RS232 standard serial line y

  • Slides: 25
Download presentation
Serial Communication z RS-232 (standard serial line) y Point-to-point, full-duplex y Synchronous or asynchronous

Serial Communication z RS-232 (standard serial line) y Point-to-point, full-duplex y Synchronous or asynchronous y Flow control y Variable baud (bit) rates y Cheap connections (low-quality and few wires) CSE 477 Serial Communication 1

Serial data format z Variations: parity bit; 1, 1. 5, or 2 stop bits

Serial data format z Variations: parity bit; 1, 1. 5, or 2 stop bits start bit CSE 477 8 data bits Serial Communication parity bit stop bit 2

RS-232 wires z z Tx. D – transmit data Tx. C – transmit clock

RS-232 wires z z Tx. D – transmit data Tx. C – transmit clock RTS – request to send: Handshake CTS – clear to send : Handshake z z Rx. D – receive data Rx. C – receive clock DSR – data set ready: Handshake DTR – data terminal ready: Handshake all wires active low "0" = -12 v, "1" = 12 v special driver chips that generate ± 12 v from 5 v z Ground CSE 477 Serial Communication 3

Transfer modes z Synchronous y clock signal wire is used by both receiver and

Transfer modes z Synchronous y clock signal wire is used by both receiver and sender to sample data z Asynchronous y no clock signal in common y data must be oversampled (16 x is typical) to find bit boundaries z Flow control y handshaking signals to control rate of transfer CLK CSE 477 Serial Communication 4

Typical connections z Terminal Rx. D Tx. D Rx. C Tx. C z Asynchronous

Typical connections z Terminal Rx. D Tx. D Rx. C Tx. C z Asynchronous modem terminal baud rate generator Synchronous modem Rx. D Tx. D DSR DTR CTS RTS async modem Rx. C Tx. C baud rate generator CSE 477 phone line interface Rx. D Tx. D DSR DTR CTS Rx. C Tx. C phone line Serial Communication phone line interface sync modem phone line 5

8051 Serial Interface z Tx. D: Port 3, pin 1 y Transmit data shifted

8051 Serial Interface z Tx. D: Port 3, pin 1 y Transmit data shifted out z Rx. D: Port 3, pin 0 y Receive data shifted in z Full duplex: both operate in parallel z We will use Mode 1 only y asynchronous y 10 bit transfer: 1 start, 8 data, 1 stop y Look at documentation for other modes z Clock for serial shift provided by timer 1 y i. e. programmable baud rate y takes away a timer from other uses CSE 477 Serial Communication 6

Serial Port Control Register (SCON) z Configures the serial interface CSE 477 Serial Communication

Serial Port Control Register (SCON) z Configures the serial interface CSE 477 Serial Communication 7

Baud Rate Generator z Use timer 1 overflow to generate serial data clock y

Baud Rate Generator z Use timer 1 overflow to generate serial data clock y serial clock is 16 x oversampled, i. e. baud rate x 16 y SMOD bit (PCON register) x 0: divides baud rate by 2 z Typical timer 1 setup y auto-reload timer y reload value determines overflow clock rate z Baud rate calculation y Clocks between overflows = y Overflow frequency = y Baud rate (assuming SMOD = 1) clocks y Baud rate = y Max Baud rate = y TH 1 value for 9600 baud = CSE 477 Serial Communication 8

8051 Serial Interface Transmitter CSE 477 Serial Communication 9

8051 Serial Interface Transmitter CSE 477 Serial Communication 9

Sending Serial Data z Transmission is initiated by a write to SBUF y start,

Sending Serial Data z Transmission is initiated by a write to SBUF y start, data and stop bits shifted out automatically y TI (transmit interrupt) set when stop bit goes xindicates that interface is ready for next character x. TI can be polled, or used to interrupt xmust reset it in the software CSE 477 Serial Communication 10

8051 Serial Receiver Interface CSE 477 Serial Communication 11

8051 Serial Receiver Interface CSE 477 Serial Communication 11

Receiving Serial Data z Reception is initiated by a 1 -0 transition - a

Receiving Serial Data z Reception is initiated by a 1 -0 transition - a start bit y data is sampled and shifted in automatically y on the stop bit, the 8 data bits are loaded into SBUF xsame address, but different register and sending SBUF y RI (receive interrupt) set when SBUF is loaded xindicates a character is ready • next character can start entering before SBUF is read • must read SBUF before next character arrives x. RI can be polled, or used to interrupt xmust be reset in the software CSE 477 Serial Communication 12

Serial Interface Interrupts z RI and TI share the same interrupt y Interrupt #4

Serial Interface Interrupts z RI and TI share the same interrupt y Interrupt #4 z Interrupt routine must look at RI and TI to see which caused the interrupt z Routine must reset RI or TI before returning y If both RI and TI are on, another interrupt will happen right away y Which bit do you check first? CSE 477 Serial Communication 13

Baud Rate Generator z Use timer 1 overflow to generate serial data clock y

Baud Rate Generator z Use timer 1 overflow to generate serial data clock y serial clock is 16 x oversampled, i. e. baud rate x 16 y SMOD bit (PCON register) x 0: divides baud rate by 2 z Typical timer 1 setup y auto-reload timer y reload value determines overflow clock rate z Baud rate calculation y Clocks between overflows = 12 x (256 -TH 1) clocks y Overflow frequency = Fclk/Clocks-between-overflows y Baud rate (assuming SMOD = 1) x 1/16 x overflow-frequency y Baud rate = 24 MHz / (16 x 12 x (256 -TH 1)) y Max Baud rate = 125 KHz y TH 1 value for 9600 baud = 13 CSE 477 Serial Communication 14

getchar() / putchar() z c = getchar() y returns the character in the buffer,

getchar() / putchar() z c = getchar() y returns the character in the buffer, if there is one y returns NULL otherwise y could check for error (character overrun) z r = putchar(c) y sends the character to the serial port, if it is not busy y returns c for normal operation, NULL if port was busy z Simple operation, no need for interrupts while ((c = getchar) == NULL) { }; while (putchar(c) == NULL) { }; z Polling doesn’t allow us to do anything else z If we are busy, we might miss a character CSE 477 Serial Communication 15

getchar() / putchar() (Part 2) z We’ll add a 1 -character buffer for both

getchar() / putchar() (Part 2) z We’ll add a 1 -character buffer for both input and output z getchar() y interrupt when a new character arrives y if the buffer is empty, place character in buffer y otherwise, set error flag (new function to check for errors) y getchar() now looks at the buffer for a character y otherwise the same as before z putchar() y interrupt when a character has been sent y if the buffer has a character, send it to the serial port y putchar() now puts the character into the buffer y otherwise the same as before y what if the buffer is empty when interrupt occurs? xnew character to buffer will not be sent z Complication: one interrupt routine for both input and output CSE 477 Serial Communication 16

getchar() / putchar() (Part 2) CSE 477 Serial Communication 17

getchar() / putchar() (Part 2) CSE 477 Serial Communication 17

getchar() / putchar() (Part 3) z The 1 -character buffer gives us some time

getchar() / putchar() (Part 3) z The 1 -character buffer gives us some time to read/write y but not a lot z Extend the 1 -character buffers to 32 characters buffers y now we can go away for a long time and not miss incoming characters y we can write out lots of characters and not wait for them all to go z Each buffer now becomes a queue y standard circular queue x 33 character vector (why 33? ) xhead, tail pointers y initialize on startup z getchar() y interrupt routine writes characters to buffer, getchar() reads z putchar() y putchar() writes characters to buffer, getchar() reads CSE 477 Serial Communication 18

getchar() / putchar() (Part 3) CSE 477 Serial Communication 19

getchar() / putchar() (Part 3) CSE 477 Serial Communication 19

Inter-Integrated Circuit Bus (I 2 C) z z Modular connections on a printed circuit

Inter-Integrated Circuit Bus (I 2 C) z z Modular connections on a printed circuit board Multi-point connections (needs addressing) Synchronous transfer (but adapts to slowest device) Similar to Controller Area Network (CAN) protocol used in automotive applications +5 v SCL SDA device 1 CSE 477 device 2 Serial Communication device n 20

Serial data format z z SDA going low while SCL high signals start of

Serial data format z z SDA going low while SCL high signals start of data SDA going high while SCL high signals end of data SDA can change when SCL low SCL high (after start and before end) signals that a data bit can be read SDA SCL START CSE 477 STOP Serial Communication 21

Byte transfer z Byte followed by a 1 bit acknowledge from receiver z Open-collector

Byte transfer z Byte followed by a 1 bit acknowledge from receiver z Open-collector wires y sender allows SDA to rise y receiver pulls low to acknowledge after 8 bits SDA 1 2 3 4 5 6 7 8 ack SCL z Multi-byte transfers y first byte contains address of receiver y all devices check address to determine if following data is for them y second byte usually contains address of sender CSE 477 Serial Communication 22

Clock synchronization z Synchronous data transfer with variable speed devices y go as fast

Clock synchronization z Synchronous data transfer with variable speed devices y go as fast as the slowest device involved in transfer z Each device looks at the SCL line as an input as well as driving it y if clock stays low even when being driven high then another device needs more time, so wait for it to finish before continuing y rising clock edges are synchronized clk 1 clk 2 SCL CSE 477 Serial Communication 23

Arbitration z Devices can start transmitting at any time y wait until lines are

Arbitration z Devices can start transmitting at any time y wait until lines are both high for some minimum time y multiple devices may start together - clocks will be synchronized z All senders will think they are sending data y possibly slowed down by receiver (or another sender) y each sender keeps watching SDA - if ever different (driving high, but its really low) then there is another driver y sender that detects difference gets off the bus and aborts message z Device priority given to devices with early 0 s in their address CSE 477 Serial Communication 24

Inter-Integrated Circuit Bus (I 2 C) z Supports data transfers from 0 to 400

Inter-Integrated Circuit Bus (I 2 C) z Supports data transfers from 0 to 400 KHz z Philips (and others) provide many devices y microcontrollers with built-in interface y A/D and D/A converters y parallel I/O ports y memory modules y LCD drivers y real-time clock/calendars y DTMF decoders y frequency synthesizers y video/audio processors CSE 477 Serial Communication 25