390 likes | 528 Views
Serial Communication Interface (SCI). Presented by: Lakmal Kaviratna Thomas Herrmann Nicolaas Ostendorf 13 November 2012. Lakmal Kaviratna. Data Transmission Types. Transmitting Data b 1 b 2 b 3 b 4 b 5 b 6 b 7 b 8. -- Bit i. i. Transmitting Data b 1 b 2 b 3 b 4 b 5 b 6 b 7 b 8. 1.
E N D
Serial Communication Interface(SCI) Presented by: LakmalKaviratna Thomas Herrmann NicolaasOstendorf 13 November 2012
LakmalKaviratna Data Transmission Types Transmitting Data b1b2b3b4b5b6b7b8 -- Bit i i Transmitting Data b1b2b3b4b5b6b7b8 1 2 3 4 Serial 1 2 5 3 4 6 7 8 5 Parallel 6 7 Received Data b1b2b3b4b5b6b7b8 8 Received Data b1b2b3b4b5b6b7b8
LakmalKaviratna Parallel vs Serial Data Transmission
LakmalKaviratna Types of 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 -- Bit of Data Receiver Transmitter Δt
LakmalKaviratna Types of Serial Communication • 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 -- Start Bit -- Parity Bit -- Stop Bit -- Bit of Data Transmitter Transmitter
LakmalKaviratna Synchronous vs Asynchronous Serial Communication
LakmalKaviratna Baud and Bit Rates • Symbol – a pulse representing an integer number of bits • Baud (Bd) – Rate which Symbols are transferred. (Symbols/second) • The number of bits per Symbol is Hardware Specific • Bit rate (bps) – Rate which bits are transmitted. (bits/second) • Data Throughput (dtp) – Rate which bits of DATA are transmitted. (Characters/second) • Remember: in asynchronous transfer, not all bits are data (start/stop/parity bits are also present)
LakmalKaviratna Example Baud and Bit Rates • Example: 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 the throughput in cps.
Thomas Herrmann Asynchronous Serial Communication • Transmitter and Receiver operate independently of each other • Transmitter must send a Start, Parity, and Stop bit with each package or ‘word’ of Data • Any signal after a Stop bit and before a Start bit is ignored
Thomas Herrmann Data Signal Format • Idle State – signal between data • Start Bit – signifies start of data transmission • Data Packet – data being transmitted • Parity Bit – fidelity check of data for receiver • Stop Bit – signifies end of data transmission • Data – 10-11 bits
Thomas Herrmann Idle State • Idle signal is a uniform stream of bits at the idle polarity state • For the HCS12, the idle state is 1
Thomas Herrmann Start Bit • A single bit is used to indicate the start of the transmission of a single word or packet • The polarity of the start bit is opposite the polarity of the idle bits • For the HCS12, the start bit is 0
Thomas Herrmann Data Bits • Actual data that is being transmitted • 8-bit mode transmission, typical • 7 bits of data and 1 parity bit (Used for ASCII) • 9-bit mode transmission, less common • 8 bits of data and 1 parity bit (Full byte transmission) • The HCS12 requires the least significant bit first
Thomas Herrmann Parity Bit • Last bit in data packet used to determine the fidelity of the received signal and check for errors in a transmission • Transmitter calculates parity bit based on data sent • Receiver estimated parity bit with the data received • Receiver compares received parity and estimate parity
Thomas Herrmann Parity Bit • Two types of parity functionality • Even Parity • The parity bit is set to 1 if the total number of 1s in the data bits is odd, total number of 1s becomes even • Odd Parity • The parity bit is set to 1 if the total number of 1s in the data bits is even, total number of 1s becomes odd • Even/Odd Parity for the HCS12 set by User
Thomas Herrmann Stop Bit • Signifies end of packet – necessary for asynchronous transmission • Can be 1-2 bits • Stop bit polarity is the same as the idle state polarity • For the HCS12, stop bit is 1
Thomas Herrmann Example 1 Construct the asynchronous signal for the transmission of 7516 for the HCS12 with8-bit mode, even parity, and one stop bit 7516 = 0111 01012 10
Thomas Herrmann Example 2 Construct the asynchronous signal for the transmission of 5A16 for the HCS12 with8-bit mode, even parity, and one stop bit 5A16 = 0101 10102 10
Thomas Herrmann Example 3 Construct the asynchronous signal for the transmission of B916 for the HCS12 with 9-bit mode, odd parity, and one stop bit B916 = 1011 10012 10
Thomas Herrmann Types of Error • Receiver Overrun • Noise Error • Frame Error • Parity Error
Thomas Herrmann Receiver Overrun • Software fails to process 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
Thomas Herrmann Noise Error • Signal noise can cause the receiver to incorrectly read a start bit or false data • This can be compensated by sampling at a higher frequency that the incoming signal • Multiple reading can be taken of each transmitted bit • These readings are averaged to determine the intended transmission
Thomas Herrmann Frame 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: The fourthbit is skipped and stop bit is one bit before it should be 1 2 3 4 5 6 7
Parity Error • The transmitter determines and send the correct parity bit • The receiver determines what the parity bit should be based on the signal received • If the transmitter and receiver parity bits do not match, then there is a known error • This does not account for an even number of errors in the data bits
NicolaasOstendorf SCI Distinctive Features (S12SCIV2) • Full duplex operation • 13-bit baud rate selection • Programmable 8-bit or 9-bit data format • Separately enabled transmitter and receiver • Programmable transmitter output parity • Two receiver wake up methods • Interrupt driven operation with 8 flags • 8 registers used to control SCI ($00C8 - $00CF) • Uses Port S, pins 0 & 1 for RXD and TXD respectively • SCI is summarized on pages 383-393 in MC9S12C Family Reference Manual
NicolaasOstendorf SCI Baud Rate Registers:SCIBDH & SCHBDL - $00C8-$00C9 • 13-bit register determines SCI baud rate • Baud rate generator is disabled until TE or RE bit is set for the first time after reset • Writing to SCIBDH has no effect without writing to SCIBDL
NicolaasOstendorf SCI Control Register 1:SCICR1 - $00CA • Data Format Bit (M) – 0: 8-bit, 1: 9-bit • Both 8-bit and 9-bit data formats have 1 start bit and 1 stop bit • Parity Enable Bit (PE) – 0: Disabled, 1: Enabled • Parity Type Bit (PT) – 0: Even, 1: Odd
NicolaasOstendorf SCI Control Register 2:SCICR2 - $00CB • Transmit Interrupt Enable (TIE) – 0: Disables interrupts for transmit data register empty flag, 1: Enables • Transmit Complete Interrupt Enable (TCIE) – 0: Disables interrupts for transmit complete flag, 1: Enables • Receiver Interrupt Enable (RIE) – 0: Disables interrupts for receiver full or overrun flags, 1: Enables • Idle Line Interrupt Enable (ILIE) – 0: Disables interrupts for idle line flag, 1: Enables • Transmit Enable (TE) – 0: Disables transmitter, 1: Enables • Receiver Enable (RE) – 0: Disables receiver, 1: Enables
NicolaasOstendorf SCI Status Register 1:SCISR1 - $00CC • Read only register • Transmit Data Register Empty (TDRE) – 0: No byte transferred to transmit shift register, 1: Byte successfully transferred • Transmit Complete (TC) – 0: Transmission in progress, 1: No transmission in progress • Receive Data Register Full (RDRF) – 0: No data in SCI data register, 1: Data in SCI data register
NicolaasOstendorf SCI Status Register 1:SCISR1 - $00CC • Overrun (OR) – 0: No overrun, 1: Overrun • Noise Flag (NF) – 0: No noise, 1: Noise • Framing Error (FE) – 0: No framing error, 1: Framing Error • Parity Error (PE) – 0: No parity error, 1: Parity error
NicolaasOstendorf SCI Status Register 2:SCISR2 - $00CD • Break Transmit Character Length (BK13) – 0: 10 or 11 bits long, 1: 13 or 14 bits long • Transmitter Pin Direction (TXDIR) – 0: TXD pin used as input, 1: TXD used as output (used only in single wire mode) • Receiver Active Flag (RAF) – 0: No reception in progress, 1: Reception in progress
NicolaasOstendorf SCI Data Registers:SCIRDH & SCIRDL - $00CE-$00CF • SCIRDL contains incoming bytes of data from serial port • R8 – Bit 8 of received data in 9-bit format • T8 – Bit 8 of transmitted data in 9-bit format
NicolaasOstendorf Example • First calculate baud rate: • Assume 8MHz bus and desired baud rate is 9600 • Desired value for SCIBR is 52 • You will have some error margin • Exact solution: 52.0833 • Actual baud rate: 9615.3 (0.160% error)
NicolaasOstendorf Example • Write SCIBR ($34) to SCI Baud Rate Registers (SCIBDH/SCIBDL) • Default values work for 8-bit, no parity, and no interrupts • Enable transmit and receive in SCICR2 • Read from SCIDRL to receive 8-bit data • Write data to SCIDRL to send 8-bit data
NicolaasOstendorf Code Example: Help set up SCI in CodeWarrior #include <hidef.h> /* common defines and macros */ #include <mc9s12c32.h> /* derivative information */ #pragma LINK_INFO DERIVATIVE "mc9s12c32" void SCI_init(void){ int BR = 0x34; SCIBDH = (unsigned char)(BR>>8); //stores high Byte SCIBDL = (unsigned char)(BR); //stores low Byte SCICR2 = 0x0C; //sets TE and RE to 1 } unsigned char SCI_getByte(void){ while (!(SCISR1_RDRF)) ;//waits until receive register is full return SCIDRL; } void SCI_sendByte(unsigned char data){ while (!(SCISR1_TDRE)) ;//waits until transmit register is empty SCIDRL = data; //return void; } void main(void) { //variable declarations must go at beginning EnableInterrupts; MISC = 0x03; PEAR = 0x0C; MODE = 0xE2; //Call function to setup SCI SCI_init(); //Main loop for(;;) { SCI_sendByte(SCI_getByte()); } /* wait */ /* please make sure that you never leave this function */ }
NicolaasOstendorf Code Example #include <hidef.h> /* common defines and macros */ #include <mc9s12c32.h> /* derivative information */ #pragma LINK_INFO DERIVATIVE "mc9s12c32" void SCI_init(void){ int BR = 0x34; SCIBDH = (unsigned char)(BR>>8); //stores high Byte SCIBDL = (unsigned char)(BR); //stores low Byte SCICR2 = 0x0C; //sets TE and RE to 1 } unsigned char SCI_getByte(void){ while (!(SCISR1_RDRF)) ;//waits until receive register is full return SCIDRL; } void SCI_sendByte(unsigned char data){ while (!(SCISR1_TDRE)) ;//waits until transmit register is empty SCIDRL = data; //return void; }
NicolaasOstendorf Code Example void main(void) { //variable declarations must go at beginning EnableInterrupts; MISC = 0x03; PEAR = 0x0C; MODE = 0xE2; //Call function to setup SCI SCI_init(); //Main loop for(;;) { SCI_sendByte(SCI_getByte()); } /* wait */ /* please make sure that you never leave this function */ }
References • MC9S12C Family Reference Manual • Past Student Lectures • http://en.wikipedia.org/wiki/Symbol_rate • http://en.wikipedia.org/wiki/Baud