Professional Documents
Culture Documents
Chapter 14
M68HCS12 Serial I/O
Objectives
14.1 Introduction
The M68HCS12 can contain a variety of serial interfaces. Among these are the following:
• The Asynchronous Serial Communications Interface – SCI. This is a universal
asynchronous receiver-transmitter (UART) designed for serial communications
to a terminal or other asynchronous serial devices such as personal computers.
• The Synchronous Serial Peripheral Interface – SPI. This is a high-speed,
synchronous serial interface used between an M68HCS12 and a serial peripheral
such as a Motorola MC68HC68A1 Serial 10-bit A/D converter or between two
M68HCS12s
• The Motorola Scalable Controller Area Network, or MSCAN. This module is
serial communications controller implementing the CAN 2.0 A/B protocol
widely used in automotive applications.
Figure 14-1(a) shows the asynchronous Serial Communications Interface – SCI. It is
frequently used to communicate with other computers, terminals and modems. In the
personal computer world, this interface is called the COM port. Usually an RS-232-C
interface standard is adopted with voltages levels that require a CMOS-to-RS-232-C
translation chip. The RS-232-C standard also defines handshaking signals such as
Request to Send – RTS and Clear to Send – CTS. These signals have not been
implemented in the SCI but Port S, bits 2 and 3 may be used for this purpose. Chapter 17
shows how to interface the SCI to RS-232-C devices
Figure 14-1(b) shows the Controller Area Network – MSCAN. This interface is
widely used in the automotive industry and is a two-wire, carrier-sense, multiple access
bus. A CAN transceiver is required to create a bus structure supporting multiple CAN
devices. Chapter 15 shows how to use the MSCAN interface.
Figure 14-1(c) is the synchronous Serial Peripheral Interface – SPI. This interface
allows a much higher data transfer between peripheral SPI devices and the
microcontroller than does the SCI. Section 14.3 shows how to use the SPI and Chapter 17
gives interfacing examples.
Chapter 14 – M68HCS12 Serial I/O 14-2
'PORT S'
Serial Received Data
RXD PS0 RS232
SCI CMOS to
(a) Serial Transmitted Data Serial
TXD PS1 RS232
Device
PS2
PS3
'PORT M'
RXCAN PM0 CAN CAN CAN
(b) CAN Transceiver Transceiver Device
TXCAN PM1
Master In/Slave Out
MISO PM2
SPI Master Out/Slave In
(c) MOSI PM3 SPI
Shift Clock Device
SCK PM4
Slave Select
SS* PM5
Figure 14-1 M68HCS12 (a) asynchronous, (b) CAN and (c) synchronous serial interfaces.
SCI Data
Two data registers, SCIDRH and SCIDRL, shown in Figure 14-2,
Serial data are read from and contain the 8- or 9-bit serial data received and to be transmitted.
written to the SCIDR register. The SCIDRL register is two separate registers occupying the same
memory address. Data to be transmitted serially are written to and
serial data received are read from these registers.
SCIDRH and SCIDRL form a 9-bit data register when sending and receiving 9-bit
data. The ninth transmit bit (T8) does not have to be changed each time new serial data
are sent. The same value will be transmitted until T8 is changed. If 9-bit data are to be
used, SCIDRH bit should be written before SCIDRL to ensure the correct data are
transferred to the SCI transmit data register when SCIDRL is written. If an 8-bit data
format is being used, only SCIDRL needs to be read or written.
SCIDRL contains the 8-bit serial transmitted and received data. Reading from
SCIDRL reads the last received data and writing to it sends (transmits) the serial data.
Writing to SCIDRL does not overwrite or destroy the contents of the data that has been
received.
'SCIDRL' (READ)
Serial Received
R7 R0
Date
Receiver
R7 R0 RxD
'SCIDRH' Shift Register
R8 T8
Transmitter
T7 T0 TxD
Shift Register
Serial Transmitted
T7 T0
Data
'SCIDRL' (WRITE)
SCI Initialization
Receiver and Transmitter Enable
As with any programmable device, the SCI must be initialized before use and there are a
variety of registers and bits to be programmed. First, the transmitter and receiver are
enabled by setting TE and RE in the SCICR2 - SCI Control Register 2. SCICR2 contains
bits to enable various interrupts, the SCI wake-up control, and a break generation bit. We
will discuss these in a later section.
TCIE
Transmission Complete Interrupt Enable
0 = Transmission complete (TC) flag interrupt requests disabled (default).
1 = Transmission complete (TC) flag interrupt requests enabled.
RIE
Receiver Full Interrupt Enable
0 = Receive data register full (RDRF) flag interrupt requests disabled (default).
1 = Receive data register full (RDRF) flag interrupt requests enabled.
ILIE
Idle Line Interrupt Enable
0 = Idle line (IDLE) flag interrupt requests disabled (default).
1 = Idle line (IDLE) flag interrupt requests enabled.
Chapter 14 – M68HCS12 Serial I/O 14-5
TE
Transmitter Enable
0 = SCI transmitter disabled (default).
1 = Transmitter enabled.
RE
Receiver Enable
0 = SCI receiver disabled (default).
1 = Receiver enabled.
RWU
Receiver Wakeup
0 = SCI normal operation (default)
1 = Enables the wakeup function and disables further receiver interrupt requests.
SBK
Send Break
0 = No break characters (default).
1 = Transmit break characters.
SCIWAI
SCI Stop in Wait Mode
0 = SCI enabled in wait mode (default).
1 = SCI disabled in wait mode.
RSRC
Receiver Source
0 = Receiver input internally connected to the transmitter output (default).
1 = Receiver input externally connected to the transmitter output.
M
Data Format Select
0 = One start, eight data, one stop bit (default).
1 = One start, eight data, plus ninth data, one stop bit.
WAKE
Wakeup Condition
0 = Idle line wakeup (default).
1 = Address line wakeup.
ILT
Idle Line Type
0 = Idle character bit count starts after start bit (default).
1 = Idle character bit count starts after stop bit.
An idle character contains all ones with no start or stop bit. ILT determines when the bit-count starts for
detecting an idle line. The M bit determines the number of bits in an idle character.
PE
Parity Enable
0 = Parity is disabled (default).
1 = Parity is enabled.
When parity is enabled, PT determines the type of parity. The parity bit is inserted in the data word in
the most significant bit position.
PT
Parity Type
0 = Even parity (default).
1 = Odd parity.
8- or 9-Bit Data
The number of data bits to be sent and received is controlled by the M bit in SCICR1. M
= 0 for one start, eight data and one stop bit; M = 1 for one start, nine data, one stop bit.
Idle Line
An idle line is one in which the receive line is in a mark 1 condition (logic high) for more
than one character time. Idle line detect may be used in half-duplex, or single-wire
systems in which the line needs to be "turned around" when the remote transmitter is
done transmitting. The idle condition is detected when the RxD input remains in the mark
(1) condition for a full character time. In long idle detect mode, the idle detect logic does
not start counting logic 1-bit times until after a stop bit. In short idle detect mode, the idle
detect logic starts counting logic ones after a start bit so the stop bit time and any
contiguous one s at the end of a serial character can cause idle to be detected earlier than
expected.
Parity
The M68HCS12 can generate a parity bit in hardware. Parity generation for transmitting
and checking for receiving is enabled by setting the PE bit. The type of parity, even or
odd, is selected by the PT bit. The chosen parity bit is inserted in the most significant bit
position.
TXDIR
Transmit Data Direction in Single-Wire Mode
0 = TxD pin is used as an input (default).
1 = TxD pin is used as an output.
RAF
Receiver Active Flag
0 = No reception in progress (default).
1 = Reception in progress.
This bit is set when the receiver detects a start bit and cleared when an idle character is detected.
1
Mark and space are terms used in asynchronous serial communications to denote a logic one (mark) and
zero (space).
Chapter 14 – M68HCS12 Serial I/O 14-8
Bus Clock
SCI Baud Rate =
(16 ∗ SCIBR [12 : 0])
TC
Transmit Complete Flag
0 = Transmission in progress.
1 = No transmission in progress (default).
This bit is different than the TDRE bit. It shows when the last character has been completely sent from
the output shift register. The flag is reset (cleared) by reading the SCISR1 register with TC set and
then writing the next byte to the SCIDRL.
RDRF
Receive Data Register Full Flag
0 = Data not available in the SCI data register (default).
1 = Received data is available.
The flag is reset (cleared) by reading the SCISR1 register with RDRF set and then reading SCIDRL
IDLE
Idle Line flag
0 = The receive line is either active now or has never been active since IDLE was last reset
(default).
1 = The receive line has become idle.
The idle flag is cleared by reading SCISR1 (while IDLE = 1) and then reading SCIDRL. After the
IDLE flag has been reset, it will not be set again until the receive line becomes active and then idle
again. When the receiver wakeup bit (RWU) is set, an idle line does not set the IDLE flag.
OR
Receiver Overrun Flag
0 = No overrun error (default).
1 = An overrun has occurred.
Overrun occurs if a new character has been received before the old data have been read by the program.
The new data are lost and the old data preserved. The flag is cleared by reading SCISR1 (with OR =
1) and then reading the SCIDRL.
NF
Chapter 14 – M68HCS12 Serial I/O 14-10
Noise Flag
0 = No noise was detected during the last character (default).
1 = Noise was detected.
The hardware takes three samples of the received signal near the middle of each data bit and the stop
bit. Seven samples are taken during the start bit. If the samples in each bit do not agree, the noise
flag is set. The flag may be reset by reading SCISR1 (with NF=1) followed by a read of SCIDRL.
FE
Framing Error Flag
0 = No framing error occurred (default).
1 = Framing error occurred.
A framing error occurs if the receiver detects a space during the stop bit time instead of a mark. This
kind of error can occur if the receiver misses the start bit or if the sending and receiving data rates
are not equal. It is possible to have bad framing or mismatched Baud rates but not get a framing
error indication if a mark is detected when a stop bit was expected. The flag is reset by reading
SCISR1 (with FE = 1) and then reading the SCIDRL.
PF
Parity Error Flag
0 = Parity on the last received data is correct (default).
1 = Parity incorrect.
PE is reset by reading SCISR1 with PF set and then reading SCIDRL.
SCI Interrupts
The SCI interrupts are enabled by setting bits in the SCICR2 SCI Control Register 2. The
interrupts are serviced by an interrupt service routine whose vector is shown in Table
14-3. There are five potential sources of interrupts shown in Table 14-3 with one
interrupt vector. When an SCI system interrupt occurs, the service routine must test the
SCISR1 status register to find out which condition caused the interrupt. As in the case of
all other interrupting sources, the flag that caused the interrupt must be cleared in the
interrupt service routine. This is done by reading the SCISR1 register and then writing
the next data byte to the SCIDRL.
SCI Wakeup
The SCI features a sleep and wake up mode. This may be used in multi-receiver
applications in which one M68HCS12 is broadcasting data to many serial receivers in a
network. Software in each receiver puts it to sleep (by setting the RWU bit in the SCICR2
register) until the programmed wake-up sequence is received. At the start of a broadcast,
each receiver automatically wakes up and software in all receivers decodes for whom the
message is intended. Only the addressed station stays awake to receive the message. Each
of the others are put back to sleep until the start of the next broadcast. Receivers that are
asleep do not respond to received data. However, only the SCI receiver is asleep and the
CPU can continue to operate and do other chores. The CPU can wake up the SCI by
resetting the RWU bit to zero although the automatic hardware mechanism normally
wakes up sleeping receivers. The wake up mode and receiver wake up enable are
controlled by bits in SCICR1 and SCICR2.
When the program puts a receiver to sleep by writing a one to the RWU bit, all
receiver interrupts are disabled until the receiver is awakened by one of two wake up
methods. If WAKE = 0, a full character of idle line (a mark) wakes up the receiver. If
WAKE = 1, any byte with a one in the most significant bit wakes it up.
Metrowerks HC12-Assembler
(c) COPYRIGHT METROWERKS 1987-2003