Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8545
Скачиваний: 0
340 |
Chapter 14 |
follow standard communications protocols, then pure PIC communications techniques and hardware are often simpler and more effective. In other words, adhering to a communications protocol usually implies an additional cost in software and hardware complexity. Here are two examples: a PIC-based circuit that interfaces with a PC through the RS-232-C port would be a case where compliance with
RS-232-C is required. Another case would be a PIC-based circuit that sends serial data to an onboard LCD display. In this case, the circuit and the software need not comply with any communications standards or protocols. Programmers often refer to techniques that use serial communications without the presence of specialized hardware, such as UART or USART chips, as bit-banging.
In the following sections, we discuss serial and parallel communications at their most essential level. In the general literature, communications concerns often focus on transmission speeds, system performance, and minimum processing time. Typically, PIC applications do not transfer large data files or communicate interactively on the Internet or in networks. In a typical PIC application, communication functions are used to upload stored data to a PC, sometimes called data-logging, or to receive small data sets or commands from a host machine. In this context there are no major concerns regarding super-fast transmission rates or maximum performance.
14.1 Serial Data Transmission
Serial communications take place by transmitting and receiving data in a stream of consecutive electrical pulses that represent data bits and control codes. The Electronic Industries Association (EIA) has sponsored the development of several standards for serial communications, such as RS-232-C, RS-422, RS-423, RS 449, EIA232E, and EIA232F, among others. In this designation the characters RS stand for the words Recommended Standard. The oldest, simplest to implement, and most-used serial communications standard is the RS-232-C voltage level convention. In the following sections we present the essential concepts of the RS-232-C standard. Most of the material also applies to the various updates of the standard. Later in the chapter we briefly discuss the EIA485 Standard.
14.1.1 Asynchronous Serial Transmission
The information in a serial bit stream is contained in a time-dependent waveform, that is, each bit code (data, control, or error) is transmitted for a fixed time period, known as the baud period. The word baud was chosen to honor the French scientist and inventor Jean Maurice Emile Baudot who studied various serial encodings in the late 19th century.
The serial bit streams used in data transmission follow a very simple encoding: one bit is transmitted during each baud period. A binary 1 bit is represented with a negative voltage level and a binary 0 bit by a positive voltage. The line condition during the logic 1 transmission is called a marking state, and the one for a logic 0 a spacing state. The baud rate is equal to the number of bits per second being transmitted or received. Note that the voltage levels that represent a one and a zero bit in RS232 are somewhat counter-intuitive, since one would expect a logic 1 to be represented with a positive voltage, and not a negative one.
342 |
Chapter 14 |
This form of transmitting serial data is called asynchronous because the receiver resynchronizes itself to the transmitter using the start bit of each frame. The lack of synchronization does not refer to the bits within each frame, which must be in fact
“synchronized,” but to the fact that characters need not come at a fixed time interval.
14.1.2 Synchronous Serial Transmission
An alternative approach to asynchronous serial data transmission is one in which the characters are sent in blocks with no framing bits surrounding them. In asynchronous communications, each character is framed by a start and a stop signal so that the receiver can know exactly where the character bits are located. In synchronous communications, the sender and receiver are synchronized with a clock or a signal that is part of the data stream.
In theory, synchronous communications implies that characters are sent out at a constant rate, in step with a clock signal. This scheme assumes that a separate line
(or wire) is used for the clock signal, although, in some variations, the clock signal is contained in the transmitted characters. Alternatively, a clock line can be used to synchronize the moment in time at which the receiver reads the data line. In either case, it is this contained clock or command signal that identifies a synchronous transmission.
Most legacy PC communications systems are asynchronous, although the
EIA232F standard supports both synchronous and asynchronous methods. The most common chip used in PC communications is the UART (Universal Asynchronous Receiver and Transmitter). An alternative chip called the USRT is used for synchronous communications and the USART (Universal Synchronous/Asynchronous Receiver and Transmitter) supports both.
Synchronous communications can be blockor bit-based. The block-based modes are also called character-based. In this mode, characters are grouped in blocks with each block having a starting flag, similar to the start bit used in asynchronous communications. Once the receiver and the transmitter are synchronized, the transmitter inserts two or more control characters known as synchronous idle characters, or SYNs. Then the block is sent and the receiver places the data in a memory storage area for later processing. Bit-oriented methods, on the other hand, are used for the transmission of binary data that is not tied to any particular character set.
14.1.3 PIC Serial Communications
Serial communications are often used in PIC programming, mostly due to the scarcity of available port lines. For example, an application in which a 16F84 PIC needs to read data in parallel from eight DIP switches and display the result, also in parallel, in eight LEDs, requires a total of 16 available port lines. But the 16F84 only has 13 lines, 8 in Port-B and 5 in Port-A; therefore, the application would not be feasible.
One possible solution is to find some way of reading the DIP switches serially; this requires three lines at most. Alternatively, the output data to the LEDs could be
Communications |
343 |
transmitted serially, thus reducing the total lines required from 16 for parallel transmission, to six, or even less for serial transmission.
PIC communications can be designed both asynchronously and synchronously. Asynchronous modes are used when the same or compatible clock signals are available to both receiver and transmitter. For example, two PICs both running at the same clock rate can transmit and receive data using a single communications line, plus a common ground. PIC-to-PIC asynchronous data transmission mode is demonstrated later in this chapter with both circuit and code.
Asynchronous communications can be implemented by incorporating a dedicated IC, such as a UART or USART chip, in the circuit. PCs usually have one of these ICs, or functionally equivalent ones, in their implementation of the serial port. Some PICs include one or more serial circuits, which sometimes include a USART module. For example, the 16F877 PIC has two serial communication modules. One of them is the Master Asynchronous Serial Port, or MSSP. The other one is a USART. Later in this chapter we present serial communications programming examples using the USART module in the 16F877 PIC. Programs using the MSSP module are found in the chapter on EEPROM programming.
When communications take place between a PIC and a device that does not contain a clock, or whose clock runs at a different speed than the PIC’s, then synchronous communications is used. For example, a circuit can be designed using a shift register IC, such as the 74HC164, that performs an 8-bit serial-in, parallel-out function. In the previous example, it is possible to reduce the number of transmission lines by connecting the eight LEDs to the output ports of the 74HC164. But the 74HC164 contains no internal clock that runs at the speed of the 16F84. Thus, communications between the PIC and the shift register IC (74HC164 in this case) requires a clock or command signal transmitted through a separate line; that is, a synchronous serial transmission. In this chapter we present circuits and sample code showing synchronous communications between a PIC and one or more shift register ICs.
14.1.4 The RS-232-C Standard
RS-232-C was developed jointly by the Electronic Industries Association (EIA), the Bell Telephone System, and modem and computer manufacturers. The standard has achieved such widespread acceptance that its name is often used as a synonym for the serial port. EIA232F, published in 1997, is the latest update of RS-232-C. Today, RS-232-C is gradually being replaced by USB for local communications. USB is faster, has lower voltage levels, and uses smaller connectors that are easier to wire. USB has software support in most PC operating systems. On the other hand, USB is a more complex standard, requiring more complex software. Furthermore, serial ports are used to directly control hardware devices, such as relays and lamps, since the RS-232-C control lines can be easily manipulated by software. This is not feasible with USB.
In the following sections we describe the essential terminology and communications principles of RS-232-C.
344 |
Chapter 14 |
Essential Concepts
The RS-232-C convention specifies that, with respect to ground, a voltage more negative than -3 V is interpreted as a 1 bit and a voltage more positive than +3 V as a 0 bit. Serial communications, according to RS-232-C, require that transmitter and receiver agree on a communications protocol. The following terminology refers to the RS-232-C communications protocol:
•Baud period: The rate of transmission measured in bits per second, also called the baud rate. In serial protocols, the transmitter and the receiver clocks must be synchronized to the same baud period.
•Marking state: The time period during which no data is transmitted. During the marking period, the transmitter holds the line at a steady high voltage, indicating logic 0.
•Spacing state: The time period during which data is transmitted. During the spacing period, the transmitter holds the line at a steady low voltage, indicating logic 1.
•Start bit: The transition that indicates that data transmission is about to start. The voltage low state that occurs during the start bit is called the spacing state.
•Character bits: The data stream composed of 5, 6, 7, or 8 bits that encode the character transmitted. The least significant bit is the first one transmitted.
•Parity bit: An optional bit, transmitted following the character bits, used in checking for transmission errors. If even parity is chosen, the transmitter sets or clears the parity bit so as to make the sum of the character’s 1 bits and the parity bit an even number. In odd parity, the sum of 1 bits is an odd number. If parity is not correct, the receiver sets an error flag in a special register.
•Stop bits: One or more logic high bits inserted in the stream following the character bits or the parity bit, if there is one. The stop bit or bits ensure that the receiver has enough time to get ready for the next character.
•DTE (Data Terminal Equipment): The device at the far end of the connection. It is usually a computer or terminal. The DTE uses a male DB-25 connector, and utilizes 22 of the 25 available pins. DB-9 connectors with 9 pins are also used.
•DCE (Data Circuit-terminating Equipment): Refers to the modem or other terminal of the telephone line interface. DCE has a female DB-25 connector, and utilizes the same 22 pins as the DTE for signals and ground. DB-9 connectors are also used.
•Half-duplex: A system that allows serial communications in both directions, but only one direction at a time. Half-duplex communications are reminiscent of radio communications where one user says the word “Over” to indicate the end of transmission. In other words, half-duplex is similar to a one-lane road in which with traffic controllers at each end can direct flow in either direction, but only in one direction at a time.
•Full-duplex: A full-duplex system allows communication in both directions simultaneously. A full-duplex system is reminiscent of a two-lane highway in which traffic can flow in both directions at once.
The Serial Bit Stream
In the RS-232-C protocol, the transmission/reception parameters are selected from a range of standard values. The following are the most common ones:
346 |
Chapter 14 |
name stands for Registered-Jack 45) is used for twisted-pair cables. RJ-45 use in RS-232-C serial interface is regulated by the EIA/TIA-561 standard. A common application of RJ-45 connectors is in Ethernet cables. Figure 14-2 shows the male DB-25,
DB-9, and the female RJ-45 connectors.
1 |
DB-25 |
13 |
14 |
25 |
RJ-45
DB-9
1 5
6 9
8 1
Figure 14-2 DB-25, DB-9, and RJ-45 Connectors
The function assigned to each pin varies in the common connectors. Table 14.1 lists the assignation of the RS-232-C lines in the different hardware. The cable linking DTE and DCE devices is a parallel straight-through cable with no cross-over or self-connects.
Table 14.1
Definition of Common RS-232-C Lines
CONNECTOR |
CODE |
||||
DB-25 |
DB-9 |
RJ-45 |
FUNCTION |
NAME |
DIRECTION |
1 |
4 |
Ground |
G |
||
2 |
3 |
6 |
Transmit data |
TXD |
Output |
3 |
2 |
5 |
Receive data |
RXD |
Input |
4 |
7 |
8 |
Request to send |
RTS |
Output |
5 |
8 |
7 |
Clear to send |
CTS |
Input |
6 |
6 |
Data set ready |
DSR |
Input |
|
7 |
5 |
Chassis ground |
G |
||
8 |
1 |
2 |
Carrier detect |
CD |
|
20 |
4 |
3 |
Data terminal ready DTR |
Output |
|
22 |
9 |
1 |
Ring indicator |
RI |
Input |
The Null Modem
The RS-232-C standards describe the way a computer communicates with a peripheral device, such as a modem. In this case, the DTE and DCE lines serve as a communications control. In this context, DTE means data terminal equipment, such as a computer, and DCE is the abbreviation of data communication equipment, such as modems. Often, communications must take place in an environment that does not include a modem; for example, computers communicating with each other or with other devices such as a PIC-based board. In these cases, the use of the DTE/DCE communication lines in flow control is not well defined.The common RS-232-C control and data signals appear in Table 14.2.