Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8527
Скачиваний: 0
Communications |
347 |
Table 14.2
Definition of Common RS-232-C Lines
SIGNAL NAME |
DIRECTION |
PURPOSE |
CONTROL SIGNALS |
||
Request to Send |
DTE -> DCE |
DTE wishes to send |
Clear to Send |
DTE <- DCE |
Response to Request to |
Send |
||
Data Set Ready |
DTE <- DCE |
DCE ready to operate |
Data Terminal Ready |
DTE -> DCE |
DTE ready to operate |
Ring Indicator |
DTE <- DCE |
DTE receiving telephone |
ringing signal |
||
Carrier Detect |
DTE <- DCE |
DTE receiving a carrier |
DATA SIGNALS |
signal |
|
Transmitted Data |
DTE -> DCE |
Data generated by DTE |
Received Data |
DTE <- DCE |
Data generated by DCE |
The term null modem refers to situations in which serial communications take place without the presence of a modem. In this case, the connection between the communicating devices, usually a cable, is wired in such a way so as to allow data transmission without a modem.
In Table 14.1 two pins are used in flow control: RTS (request to send) and CTS (clear to send). In conventional RS232 communication (as is the case when a computer communicates with a modem), the RTS signal is an output and DCE an input. Before a character is sent, the sender sets the RTS line high to ask the DTE’s permission. Until the DTE grants permission, no data is sent. The DTE grants its permission by setting the CTS line high. If the DCE cannot receive new data it keeps the CTS signal low. This interface, which provides a simple mechanism for flow control in a single direction, is called a handshake.
In full duplex transmission the handshake must take place in both directions, that is, both devices must be able to signal their status. The DTR (data terminal ready) and DSR (data set ready) signals can be used for a second level of flow control. Finally, the CD (carrier detect) signal serves as an indication of the state of a modem.
The Null Modem Cable
Implementing handshaking without a modem requires that we take into account that two communicating devices can expect to find certain signals on given lines. For example, a device checks the CTS signal for a high value before sending data. If the CTS signal never goes high, transmission does not take place. When a cable is wired so that two devices can communicate without one of them being a modem, the cable is said to be a null modem.
One simple approach is to completely eliminate handshaking. In this case, cable wiring interconnects the transmit and the receive lines and the ground wire. The remaining pins are left unconnected, as shown in the null modem cable in Figure 14-3 (in the following page).
348 |
Chapter 14 |
DB-9 (female)
5 4 3 2 1
9 8 7 6
WIRING |
|||
DB-9 |
DB-9 |
||
Female |
Male |
||
3 |
TX--------- |
2 |
RX |
2 |
RX--------- |
3 |
TX |
5 |
GND-------- |
5 |
GND |
1 2 3 4 5
6 7 8 9
DB-9 (male)
Figure 14-3 Null Modem with No Handshaking
The three-wire null modem cable can be used to interface devices that do not use modem control signals. However, if one of the devices checks one of the handshake lines, such as RTS/CTS, then the three-wire modem cable fails. To solve this problem, a modem cable can be designed so that the handshake signals are interconnected. For example, DTS to DSR and vice versa. Not knowing which handshake signals are to be used, manufacturers of standard modem cables usually interconnect all handshake lines, as shown in Figure 14-4.
DB-9 (female)
5 4 3 2 1
9 8 7 6
WIRING |
|||
DB-9 |
DB-9 |
||
Female |
Male |
||
2 |
RX--------- |
3 |
TX |
3 |
TX--------- |
3 |
RX |
4 |
DTR-------- |
6 |
DSR |
5 |
GND-------- |
5 |
GND |
6 |
DSR-------- |
4 |
DTR |
7 |
RTS-------- |
8 |
CTS |
8 |
CTS-------- |
7 |
RTS |
1 2 3 4 5
6 7 8 9
DB-9 (male)
Figure 14-4 Null Model With Full Handshaking
Some variations of the full-handshake null modem connect the DTR to the CD line at each end. Pin number 1 (CD) in both male and female connectors is dum- mied-out to pin number 4 (CDR).
Communications |
349 |
A conventional, straight-through serial cable can be converted to null modem by means of a commercial null modem adapter that crosses over the corresponding signal lines. A continuity test is used to determine whether a serial cable is wired as null modem or not. If it is null modem, pin number 2 on one end would show continuity with number 3 pin on the other end.
A circuit tester is used to diagnose serial cables. The tester, which is plugged into the port connector, contains a LED for each of the communications lines. When the corresponding LED lights up the line is active. LED colors indicate positive or negative voltages, with green usually indicating positive and red negative. The light pattern is used to identify different handshakes. Figure 14-5 shows a DB-25 mini tester.
Figure 14-5 DB-25 RS232 Line Tester
14.1.5 The EIA-485 Standard
EIA-485 provides a two-wire, half-duplex serial connection standard, also known as RS-485. This convention provides a multipoint connection with differential signaling. The connection can be made full-duplex by using four wires. In this standard, data is conveyed by voltage differences. One polarity represents logic 1 and the reverse one logic 0. The standard requires that the difference of potential be at least 0.2 volts, but any voltage between +12 and -7 volts allows correct operation.
EIA-485 does not specify a data transmission protocol, making possible the implementation of simple, inexpensive local networks and communications links. Its data transmission speeds can reach 35 Mbits/s at distances of up to 10 m, and 100 kbit/s at distances up to 1200 m. The use of a twisted wire pair and the differential balanced line allows spanning distances of up to 4000 ft.
EIA-485 is often used with common UARTs and USARTs to implement low-speed data communications that require minimal hardware. It is also found in programmable logic controllers that are used with proprietary data communications systems. In factories and other electrically charged environments, the differential feature of EIA-485 makes it resistant to electromagnetic interference from motors and other
Communications |
351 |
In PIC-based systems, parallel communications often refer to the general principle rather than to the specific Centronics implementation. For example, wiring an 8-line toggle switch to the eight pins of the 16F85 Port-B line provides parallel communications between the switch and the PIC.
PIC circuits that use parallel data transfers offer many advantages. In the first place, parallel transmission is fast and the software is simple to develop. The hardware implementation is straightforward and does not require many additional components. Examples are connecting a multiple toggle switch to each of the lines of a PIC input port, or each of the pins of a seven-segment LED to the various pins of a PIC output port. The disadvantages of parallel systems are the distance limitations and the cost in system resources. Furthermore, parallel data transfers do not work well for data transmission over long distances. Many of the circuits and programs covered in previous chapters use parallel data transmission techniques. Since
PIC-based systems rarely communicate with parallel printers or use the Centronics standard for data transfer, no further discussion of the Centronics standard is justifiable in this context.
14.2.1 PIC Parallel Slave Port (PSP)
Some PICs are equipped with an 8-bit Parallel Slave Port module (PSP). At present, the PSP is multiplexed onto Port D and is found in PICs of the mid-range family, such as the 16F877. The PSP is also called the microprocessor port.
The PSP module provides an interface mechanism with one or more microprocessors. The parallel slave port has an operating speed of 200 ns with a clock rate of 20 MHz, as well as several on-chip peripheral functions for implementing real world interfaces.
In PICs equipped with the PSP, the parallel slave port functions are assigned to Port D, with some Port E bits providing control signals. To initialize PSP mode, data direction bits in the TRISE register that correspond to RD, WR, and CS (TRISE<2:0>) are configured as inputs and the control bit PSPMODE (TRISE) is set. When the PSP mode is active, Port D is asynchronously readable and writable through the chip Select (RE2/CS), Read (RE0/RD), and Write (RE1/WR) control inputs.
At this time, not many general-purpose applications for the PSP port have been documented, outside of its use as a multi-microprocessor interface. For this reason we have excluded PSP programming from this context.
14.3 PIC “Free-style” Serial Programming
This section is about PIC serial programming and circuit design that does not follow any specific communications protocol. In this sense, we have used the expression “free-style” as opposed to circuits and programs constrained by the requirements of a standard or convention. Many self-contained PIC circuits that do not interface with standardized components can benefit from not having to follow any specific standard. Later in this chapter, and in other chapters in the book, we present examples of PIC circuits and programs that follow established communications protocols. The titles of
352 |
Chapter 14 |
the corresponding sections refer to the specific standards or protocols; for example, the section titled PIC RS-232-C Serial Programming found in this chapter.
The advantages of so-called “free-style” circuit design and programming are greater in ease in development and the use of fewer hardware components. When designer and programmer are not constrained by the specifications of a standard, the circuit can be implemented with a minimal number of hardware components. By the same token, software is simpler and easier to develop.
The following examples of free-style communications systems are presented in the sections that follow:
1.A PIC to PIC communications circuit and program. Two programs are required: one for the receiver PIC and one for the sender.
2.Serial-to-parallel and parallel-to-serial circuit and program. Circuit uses 74HC164 and
74HC165 ICs.
14.3.1 PIC-to-PIC Serial Communications
Perhaps the most obvious and straightforward mode of PIC serial communications is one that takes place between two PICs. In this case, one PIC acts as a sender, or master, and the other one as a receiver or slave, although it is also possible for sender and receiver to exchange roles. Consider a circuit in which one PIC polls the state of a bank of switches and then sends the result serially to a second PIC that controls a bank of LEDs to be lighted according to the switch settings. The reason for this circuit is that some PICs may not have a sufficient number of ports to monitor eight switches and control eight LEDs.
PIC-to-PIC Serial Communications Circuits
Actually, the system required for one PIC reading data and serially sending the result to another PIC that outputs the data can be visualized as two separate circuits. One circuit is used to read the state of the eight DIP switches and to send the data serially to another PIC circuit that displays the results. Figure 14-7 shows the two PIC-based circuits.
Structurally, the circuits in Figure 14-7 are quite similar to ones described previously in this book. The bottom circuit contains eight DIP switches wired to ports RB0 to RB7. A pushbutton switch is wired to port RA2 and a LED to port RA3. The serial output is through port RA1. The circuit at the top of Figure 14-7 has eight LEDs wired to ports RB0 to RB7. There is a pushbutton on port RA2 and a LED on port RA3. Input into the circuit is through port RA0. In the remainder of this description we refer to the bottom circuit as the sender circuit and PIC and the one on the top as the receiver circuit and PIC.
The pushbuttons are necessary so that sender and receiver are synchronized. In operation, the receiver circuit is first activated by pressing the switch labeled “receive ready.” The LED on the top circuit lights to indicate the ready state. The sender circuit has a LED labeled “ready” that indicates its state. The user presses the switch labeled “send ready” in the sender circuit. At that time, the program in the sender reads the state of the DIP switches and sends the data out, one bit at a time, through the line labeled “serial out” in the diagram. The receiver reads the eight bits in its “serial in” line and lights the LEDs accordingly.