ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 3127
Скачиваний: 0
2.5 Exercises |
25 |
Solution
Bits 7 and 6 of PORTD of HC11 are not implemented, and always read 0. By writing $38 = 00111000b to DDRD, bits 5, 4, 3 of PORTD are configured as output lines. Only these three bits are affected by subsequent write operations to PORTD. Read operations from an output port return the last value written to the port.
For this reason, the content of A after the final read from PORTD in the above example is 00110xxxb. The least significant three bits are input lines, and their status is determined by the logic levels on the external pins of the MCU.
SX 2.2
Write a code fragment to configure the upper nibble of PORTC of AT90S8535 as output, and the lower nibble as input, with the internal pull-up resistors enabled, then read the values of PINC0-3 and write them to PORTC4-7.
Solution
LDI |
R16,$F0 |
;configure port |
OUT |
DDRC,R16 |
|
LDI |
R16,$0F |
;enable pull-ups |
OUT |
PORTC,R16 |
|
IN |
R16,PINC ;read input lines |
|
SWAP |
R16 ;swap nibbles |
|
SBR |
R16,$0F |
;keep pull-ups active |
OUT |
PORTC,R16 ;write to port |
|
SX 2.3
Write a code fragment that configures the line P2.0 of a 8051 as input, then reads the status of this line, and writes the value read to P2.7.
Solution
SETB |
P2.0 |
;P2.0 |
configured as |
input |
MOV |
C,P2.0 |
;read |
input line to |
carry |
MOV |
P2.7,C |
;write carry to the |
output line |
|
3
Using the Asynchronous Serial Interface
3.1 In this Chapter
This chapter is an introduction to serial communication. It contains the description of the asynchronous serial communication interface of HC11, AVR, and 8051, as well as an overview of the RS232 and RS422/485 interfaces, and the principles of creating simple microcontroller networks.
3.2 Synchronous vs. Asynchronous Communication
The main distinctive feature of a serial communication system is that data is handled in series, i. e. bit by bit. The simplest serial communication device is the shift register. Consider the example in Fig. 3.1, where two shift registers are connected in such a way that the content of the first, called the transmitter, is transferred to the second, called the receiver.
Note that, in this case, the shift clock CLK, and the control signals SH/LD\ and RSTR must be generated at the transmitter level, at precise moments of time (see Fig. 3.2.) and transmitted along with data on the communication line. Such a communication system, where the transmission clock is sent to the communication line, is called synchronous communication.
RSTR
CLK
IC1
GND
D0
D1
D2
D3
D4
D5
D6
D7
SH/LD\
Serial data
|
9 |
||||||||||||||
|
QH |
CLK INH SH\LD/ |
|||||||||||||
|
SER A B C D E F G H |
||||||||||||||
|
10 |
11 |
12 |
13 |
14 |
3 |
4 |
5 |
6 |
2 |
15 |
1 |
|||
|
GND |
||||||||||||||
74HC165
|
1 |
15 |
VCC |
||||||||
|
2 |
3 |
|||||||||
|
STR D CLK |
OE |
|||||||||
|
IC2 |
Q1 Q2 Q3 Q4 Q5 Q6 Q7 |
Q8 |
||||||||
|
4 |
5 |
6 |
7 |
14 |
13 |
12 |
11 |
|||
4094N
RD7
RD6
RD5
RD4
RD3
RD2
RD1
RD0
Fig. 3.1. Example of synchronous serial communication circuit
28 3 Using the Asynchronous Serial Interface
SH/LD\
CLK
RSTR
Fig. 3.2. Waveforms of the control signals for the circuit presented in Fig. 3.1
The problem becomes more complicated when it is not possible to send the serial clock over the communication line. In this situation, the receiver must generate its own clock, RxCLK, to shift data into the Rx shift register.
This type of serial communication, where the serial clock is not transmitted on the communication line, is called asynchronous communication. The generic block diagram of an asynchronous communication system is shown in Fig. 3.3.
To make this possible, the first requirement is that the transmitter and the receiver clock have exactly the same frequency. A limited number of possible frequencies have been standardized for asynchronous communication. These are: 110, 300, 600, 1200, 2400, 4800, 9600, 19 200, 57 600, 115 200 Hz.
Since the frequency of the transmission clock is directly related to the communication speed, so that with each clock pulse a bit of information is transmitted, the communication speed is measured in bits per second or baud. The period of the transmission clock is called the bit time Tb.
The second requirement is to mark somehow the beginning of the transmission of a sequence of bits. For this purpose, a special synchronization bit, called the start bit, has been inserted. This has the polarity opposed to the idle line status and its duration equals one TxCLK period.
The moment when the transmission ends is known, because the number of bits in each packet is known. In most cases, data is sent in 8-bit packets. To make sure the communication line returns to its idle status after each data packet is transmitted, an
Data |
Data |
|||||||
Serial |
||||||||
Tx Shift register |
Rx Shift register |
|||||||
line |
||||||||
RxCLK |
||||||||
TxCLK |
||||||||
Transmiter |
Receiver |
|||||||
Fig. 3.3. Block diagram of an asynchronous communication system
Start |
1 |
0 |
0 |
0 |
1 |
1 |
0 |
0 |
Stop |
||||||||||||||||||||||
Tb/2 |
Tb |
Tb |
Tb |
Tb |
Tb |
Tb |
Tb |
Tb |
Tb |
||||||||||||||||||||||
Fig. 3.4. Sampling data line in an asynchronous serial communication
3.3 Error Detection in Asynchronous Communication |
29 |
additional stop bit is transmitted. This always has the status of the idle line. Figure 3.4. shows how the data line is sampled at the receiver.
The falling edge of the data line, corresponding to the start bit, starts the reception process.
The data line is sampled after half of the bit time interval to check for a valid start bit, and then at intervals equal to Tb. The values of the data line at these moments are shifted into the receiver data shift register Rx.
Important notes. The first bit transmitted in an asynchronous serial communication is the least significant bit (LSB).
The idle line status is HIGH.
The start bit always has the opposite polarity of the idle line, i. e. it is always 0. The stop bit always has the polarity of the idle line, i. e. it is always 1.
3.3 Error Detection in Asynchronous Communication
One serious problem when handling asynchronous serial communication is the vulnerability to electromagnetic interference. Several means have been provided to detect communication errors, and the status register of an asynchronous serial interface normally contains special status bits to indicate these errors.
If, for instance, when sampling the data line at the moment T0 + Tb/2 (refer to Fig. 3.4), a value of 1 is obtained, that means the falling edge detected at the moment T0 was not a valid start bit, but a spike due to electromagnetic noise. This type of error is called noise error.
Similarly, if the data line status at the moment T1 = T0 + Tb/2 + 9 × T b is not HIGH, this means that the expected stop bit is invalid, indicating that the byte received is in error. This type of error is called framing error.
Obviously, these two control methods are insufficient to detect all possible errors. Another method to verify the integrity of data is parity control. For this purpose, a special bit, called the parity bit, is transmitted just before the stop bit. This is either the most significant bit of each byte, or an additional ninth bit attached to each byte.
The values of the parity bits are automatically set so that the total number of 1s contained in the byte, plus the parity bit, is always an ODD or EVEN number, at the user’s choice. Both the transmitter and receiver must calculate the parity according to the same rule (ODD or EVEN). Upon reception of each byte, the parity is calculated, and, if the parity does not match the rule, the error is reported.
Parity control still cannot detect all errors, but, at the hardware level, the methods described above are all that can be done for error detection. For better error detection, software techniques must be used. Basically, software detection of communication errors relies on the following principles:
30 3 Using the Asynchronous Serial Interface
•The communication is based on data packets, having a determined structure.
•Each data packet contains a special field reserved for a sophisticated checksum. The transmitter computes the checksum for each packet and inserts it into the reserved field of the packet.
•The receiver recalculates the checksum of the packet and compares it with the value calculated by the transmitter and sent along with the packet. If the two values don’t match the packet is rejected, and the transmitter is requested to repeat the transmission of the packet.
This method is called Cyclic Redundancy Check Control (CRC). The algorithms used to compute the checksums are so complex that the probability that a packet with errors still has a correct checksum is extremely low.
3.4 The General Structure
of the Asynchronous Serial Communication Interface
The general block diagram of an asynchronous serial interface is presented in Fig. 3.5. This circuit is called a Universal Asynchronous Receiver Transmitter (UART). There are numerous stand-alone integrated circuits with this function, but most microcontrollers include a simplified version of UART, with the generic name Serial Communication Interface (SCI). This chapter contains details on the implementation of the SCI of HC11, AVR and 8051 microcontrollers. Even though there are differences in what concerns the names of the registers associated with the interface, or the names and particular functions of the control and status bits, the general structure of the interface is basically the same in all microcontrollers.
CLK |
BAUD rate generator |
TxD |
Tx shift register |
Rx shift register |
RxD |
|||||||||||||||||||||||
Control logic |
||||||||||||||||||||||||||
Control |
Tx Data |
Rx Data |
Status |
|||||||||||||||||||||||
Internal bus
Fig. 3.5. General block diagram of the asynchronous serial communication interface
3.5 The Serial Communication Interface of 68HC11F1
SCDR – SCI Data Register
The transmitter’s and receiver’s data registers have the same address and the same name: Serial Communication Data Register (SCDR). Physically, they are distinct registers, but the write operations to SCDR are directed to the transmitter’s data
3.5 The Serial Communication Interface of 68HC11F1 |
31 |
register, while the read operations from SCDR return the content of the receiver’s data register.
BAUD – Baud Rate Generator Control Register
To select the communication speed, a special register, called BAUD, has been provided. This controls how the system clock E is divided in the baud rate generator block, before it is applied to the control logic that actually generates the clock for the serial shift registers. The system clock is first applied to a programmable counter, called a prescaler. The prescaler output is then applied to a second programmable counter. After the two division stages the frequency of the resulting clock is 16 times the actual baud rate.
The BAUD register has the following structure:
BAUD |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
TCLR |
– |
SCP1 |
SCP0 |
RCKB |
SCR2 |
SCR1 |
SCR0 |
|
RESET |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
•TCLR – Clear Baud Rate Counters – and RCKB – SCI Baud Rate Clock Check, are only used in special test operating mode.
•SCP[1:0] – SCI Baud Rate Prescaller Selects. These two bits control the prescaler division rate, as defined in Table 3.1.
•SCR[2:0] – SCI Baud Rate Selects. These bits control the second stage programmable counter of the baud generator, as shown in Table 3.2
Table 3.1. HC11 prescaler control bits
SCP1 |
SCP0 |
Prescaller divide internal clock by: |
0 |
0 |
1 |
0 |
1 |
3 |
1 |
0 |
4 |
1 |
1 |
13 |
Table 3.2. HC11 baud rate select bits
SCR2 |
SCR1 |
SCR0 |
Prescaller output is divided by: |
0 |
0 |
0 |
1 |
0 |
0 |
1 |
2 |
0 |
1 |
0 |
4 |
0 |
1 |
1 |
8 |
1 |
0 |
0 |
16 |
1 |
0 |
1 |
32 |
1 |
1 |
0 |
64 |
1 |
1 |
1 |
128 |