Serial Communication Interface SCI Ellenor Brown Howard Liles
Serial Communication Interface (SCI) Ellenor Brown Howard Liles Algan Samur 1
Presentation Outline Ø Ø Ø Types of data transmission • • Howard Liles Parallel Serial Communication • • Synchronous Asynchronous Baud and Bit Rates Asynchronous Serial Transmission • • • Ellenor Brown Start Bit Data Bit Stop Bit Parity Bit Noise Algan Samur SCI Registers Show two examples of how data words are transmitted 2
Data Transmission Power supply circuit Electronic devices communicating with each other Desktop computer Lights Turbine generators Printer Hard drive 3
Types of Data Transmission • Serial Data Transmission One bit at a time Receiver (Monitor) • Parallel Data Transmission One word (N bits) at a time Receiver (printer) = bits Word Transmitter (CPU) Transmitter (microprocessor) 4
Parallel vs. Serial • Parallel requires more transfer lines • Bits have to be synchronized • Fast, but expensive Examples: • Printer cables • Serial requires less transfer lines • Transfers one bit at a time • Slow comparatively, but less expensive Examples: USB, Firewire, ethernet 5
Serial Communication Synchronous Serial Communication • Transmitter and Receiver have synchronized clocks • Data must be sent constantly in order for them to stay synchronized • Any data not sent on a regular clock cycle is considered noise • Transmission parameters are set up before sending data • 30% faster than asynchronous transmission for large continuous blocks of data • Clock rate determines data transfer rate Transmitter Receiver Asynchronous Communication Transmitter and receiver do not have synchronized clocks and act independently Simpler and less expensive than synchronous Start, Stop and Parity “caution” bits are sent with each word of data Transmitter Word Receiver 6
Comparisons SYNCHRONOUS How It Works Clock Advantages Lower overhead Greater throughput Faster Disadvantages More complex hardware More expensive ASYNCHRONOUS Start and Stop bits Simple Cheap hardware Large overhead Slower 7 Andrew Byrley
Baud and Bit Rates • Baud Rate (Bd) is the rate at which Symbols are transferred. A symbol is a given signaling event ‘Symbols/second’ • Number of bits per Symbol is Hardware Specific (our hardware uses 1 baud/bit) Conversion factor: • 1 bit = 1 Symbol = 1 baud Bit rate (bps) - the rate at which bits are transmitted or “bits/second” Conversion Equation: Bit rate (bps) = baud rate × number of bits per baud 8
Baud and Bit Rates Cont. ’d • Some bits are data and some are not! – Remember the start, stop, and parity bits which are also known as “overhead bits” • • • Data throughput can be determined by Characters per second (cps) Cps = actual rate of data being sent 1 Standard Character = 1 Bit Characters per second (cps)=Bit rate*(character bits/total bits) *Remember CPS is not same as bytes/second. CPS does not include overhead! 9
Baud and Bit Rates Example 1: You have an asynchronous serial connection. Assuming 2 bits per symbol, 9600 bd line speed, 8 bit data format with no parity, 1 start bit and 1 stop bit, calculate throughput in cps. Bit rate (bps) = baud rate × number of bits per baud bps= 9600 x 2=19200 (cps)=Bit rate*(character bits/total bits) cps=19200*(8/10)= 15360 10
Presentation Outline Ø Ø Ø Types of data transmission • • Howard Liles Parallel Serial Communication • • Synchronous Asynchronous Baud and Bit Rates Asynchronous Serial Transmission • • • Ellenor Brown Start Bit Data Bit Stop Bit Parity Bit Noise Algan Samur SCI Registers Show two examples of how data words are transmitted 11
Asynchronous Serial Communication • Transmitter and Receiver are independent • Transmitter sends ‘Start’, ‘Parity’ and ‘Stop’ bits with each word of data • Data received between a Stop bit and the next Start bit is ignored Stop Data Receiver Transmitter Parity Start 12
Data Frame Format • • Start bit – Indicates beginning of data Data bit – Data being transmitted Parity bit – Integrity check Stop bit – Indicates end of data word • Data frame size: 10 or 11 bits Stop Data Receiver Transmitter Parity Start 13
Start Bit • One bit • Indicates the beginning of word • Opposite polarity from idle bit state – Idle state for HCS 12 is 1’s – Start bit = 0 14
Data Bits • Actual data being transmitted plus a parity bit • Most common mode: – 8 -bit transmission – Used for ASCII character transmission (7 -bit ASCII + 1 -bit parity = 8 -bit) • Less common mode: – 9 -bit transmission – Can be used to send a full byte of data + parity bit • HCS 12 sends least significant bit (LSB) first 15
Parity Bit • Used to determine if an error occurred during data transmission • Error Detection – Transmitter calculates proper parity bit – Receiver calculates parity bit based on data it received – Receiver compares its parity bit to the one it received 16 Evan Johnson
Parity Bit • 2 types of Parity functionality • Even Parity – Parity bit is set to 1 if there is an odd number of 1’s in data bits # of 1’s becomes even • Odd Parity – Parity bit is set to 1 if there is an even number of 1 s in data bits # of 1’s becomes odd) • Even/Odd Parity is set by user on HCS 12 17 Evan Johnson
Stop Bit • • 1 or 2 bits Used due to asynchronous nature Directly after the parity bit Stop bit is the same as the polarity of the data-line’s idle state – Idle state for HCS 12 = all 1’s – Stop bits = 1 18
Asynchronous Data Transmission • Example 1: LSB – Hex# 4 A 16 is to be sent with one start bit, even parity, 8 -bit data length and one stop bit – 4 A 16 = 0100 10102 Start Bit 0 Data Bit 1 Data Bit 2 Data Bit 3 Data Bit 4 Data Bit 5 Data Bit 6 Data Bit 7 0 1 0 Parity Bit 1 Stop Bit 1 19
Asynchronous Data Transmission • Example 2: – Hex# B 416 is to be sent with one start bit, even parity, 8 -bit data length and one stop bit – B 416 = 1011 01002 Start Bit 0 Data Bit 0 0 Data Bit 1 0 Data Bit 2 Data Bit 3 1 0 Data Bit 4 1 Data Bit 5 1 Data Bit 6 Data Bit 7 0 1 Parity Bit 0 Stop Bit 1 20
Asynchronous Data Transmission • Example 3: – Hex# B 416 is to be sent with one start bit, odd parity, 8 bit data length and one stop bit – B 416 = 1011 01002 Start Bit 0 Data Bit 0 0 Data Bit 1 Data Bit 2 Data Bit 3 Data Bit 4 Data Bit 5 Data Bit 6 Data Bit 7 Parity Bit 0 1 1 Stop Bit 1 21
Error sources • Frame shift – Detected when a logic 0 is accepted as the stop bit • Overrun – Software fails to read the SCI data register before the shift register receives the next frame • Noise – SCI detects noise on the receiver input 22
Framing Error • Occurs when stop bit is not where receiver expects it to be • Detected when a logic 0 is accepted as the stop bit • Ex: “ 4” bit is skipped and stop bit is one bit before it should be 1 2 3 4 5 6 7 23
Overrun SOFTWARE REGISTER RECEIVER • Software fails to read the SCI data register before it receives the next frame • Data in the shift register is lost • Data already in the SCI data registers is not affected TRANSMITTER 24
Noise Detection • SCI detects noise on the receiver input 25
Presentation Outline Ø Ø Ø Types of data transmission • • Howard Liles Parallel Serial Communication • • Synchronous Asynchronous Baud and Bit Rates Asynchronous Serial Transmission • • • Ellenor Brown Start Bit Data Bit Stop Bit Parity Bit Noise SCI Registers Coding example Algan Samur 26
SCI Baud Rate Registers SCIBDH & SCHBDL - $00 C 8 -$00 C 9 • • 13 -Bits register determines SCI Baud rate generator is disabled until TE or RE bit is set for the first time after reset. Baud rate generator is turned off when this register contains $0000 Note: Writing to SCIBDH has no effect w/out writing to SCIBDL 27
Implementation Specific Features (S 12 SCIV 2) q 13 -bit baud rate selection q 8 - or 9 -bit data format q Separately enabled transmitter and receiver q Programmable transmitter output parity q Interrupt driven operation with 8 flags q 8 registers used to control SCI ($00 C 8$00 CF) q Uses Port S pins 0 & 1 for RXD and TXD respectively 28
SCI Control Register 1 SCICR 1 - $00 CA • • • M (data format mode) - 0: 8 data bits, - 1: 9 data bits. - Both use 1 start bit and 1 stop bit PE (parity enable) – 0: Off, 1: On PT (parity type) – 0: Even, 1: Odd 29
SCI Control Register 2 SCICR 2 - $00 CB • TIE (transmit interrupt enable) – 0: disables interrupts for transmit data register empty, 1: enables • TCIE (transmit complete interrupt enable) – 0: disables interrupts for transmit complete, 1: enables • RIE (receiver interrupt enable) – 0: disables interrupts for receiver full and overrun , 1: enables • ILIE (idle line interrupt enable) – 0: disables interrupts for idle line, 1: enables • TE (transmit enable) – 0: disable transmitter, 1: enable • RE (receiver enable) – 0: disable receiver, 1: enable 30
SCI Status Register 1 SCISR 1 - $00 CC • • • Read only register Can be used to provide input to the microcontroller for generation of SCI interrupts TDRE (transmit data register empty) – 0: No byte transferred, 1: byte successfully transferred to transmit shift register TC (transmit complete flag) – 0: transmission in progress, 1: no transmission in progress RDRF (receive data register full) – 0: no data in data register, 1: data in data register IDLE (idle flag) – 0: receiver input is active, 1: receiver input has become idle 31
SCI Status Register 1 SCISR 1 - $00 CC • OR (overrun) – 0: no overrun, 1: overrun (overrun happens when new data is received before old data is read) • NF (noise flag) – 0: disable, 1: enable • FE (framing error flag) – 0: disable, 1: enable • PF (parity error) – 0: No parity error, 1: parity error 32
SCI Status Register 2 SCISR 2 - $00 CD • BK 13 (break transmit character length) – 0: 10 or 11 bit, 1: 13 or 14 bit • TXDIR (transmitter pin direction) – 0: TXD pin used as input, 1: TXD pin used as output. (used only in single wire mode) RAF (receiver active flag) – 0: no reception in progress, 1: reception in progress • 33
SCI Data Registers SCIDRH &SCIDRL - $00 CE - $00 CF • SCIRDL contains incoming bytes of data from serial port R 8 – bit 8 of received 9 -bit data • T 8 – bit 8 of transmitted 9 -bit data • 34
SCI is easy q SCI module makes it easy to send/receive data q SCI module encodes data into standard NRZ format! q Hardest part is setting up baud rate q Can use either polling or interrupt based logic to drive SCI q SCIDRH/SCIDRL are like two registers in one. • Read this register to receive data • Write to this register to send data 35
Example • First, calculate baud rate. Assume 8 MHz bus and desired baud rate is 9600 • SCI module runs at bus speed • Desired value for SCIBR is 52 • You will have some error margin – Exact solution is 52. 0833 – Actual baud rate is 9615. 3 (0. 160% error) 36
Example q Write SCIBR ($0034) to SCIBDH/SCIBDL q For 8 -bit, no parity, no interrupts, default values will work q Simply enable transmit and receive in SCICR 2 q Read from SCIDRL to receive 8 -bit data q Write data to SCIDRL to send 8 -bit data 37
Code Example 38
Code Example 39
#include <hidef. h> /* common defines and macros */ #include <mc 9 s 12 c 32. h> /* derivative information */ #pragma LINK_INFO DERIVATIVE "mc 9 s 12 c 32" MISC = 0 x 03; PEAR = 0 x 0 C; MODE = 0 x. E 2; void SCI_init(void){ int BR = 0 x 0034; SCIBDH = BR>>8; //stores high Byte SCIBDL = BR; //stores low Byte SCICR 2 = 0 x 0 C; //sets TE and RE to 1 } //Call function to setup SCI_init(); //Main loop while(1) { char SCI_get. Byte(void){ while (!(SCISR 1_RDRF)) ; //waits FOREVER until receive register is full return SCIDRL; } SCI_send. Byte(data); } /* loop forever */ /* please make sure that you never leave this function */ char data = SCI_get. Byte(); } void SCI_send. Byte(char data){ while (!(SCISR 1_TDRE)) ; //waits FOREVER until transmit register is empty SCIDRL = data; //return void; } void main(void) { //variable declarations must go at beginning /* put your own code here */ Enable. Interrupts; //required code as per instructions 40
Thank You! Any Questions? 41
- Slides: 41