Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 8615

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Communications

363

The following code fragment lists a procedure to interface a 16F84 PIC with a 74HC165 parallel-to-serial shift register:

;============================================================

; constant definitions from wiring diagram

;============================================================

#define clk65LN 1

;|

— 74HC165 lines

#define loadLN 2

;|

.

.

.

;============================================================

;74HC165 procedure to read parallel data and send

;

serially to PIC

;============================================================

;OPERATION:

;1. Eight DIP switches are connected to the input

;ports of an 74HC165 IC. Its output line Hout,

;and its control lines CLK and load are connected

;to the PIC’s Port-B lines 0, 1, and 2

;respectively.

;2. Procedure sets a counter (bitCount) for 8

;iterations and clears a data holding register

;(dataReg).

;3. Port-B bits are read into w. Only the lsb of

;Port-B is relevant. Value is stored in a working

;register and the meaningful bit is rotated into

;the carry flag, then the carry flag bit is

;then shifted into the data register.

;4. The iteration counter is decremented. If this

;is the last iteration the routine ends. Otherwise

;the bitwise read-and-write operation is repeated.

in165:

clrf

dataReg

; Clear data register

movlw

0x08

; Initialize counter

movwf

bitCount

bcf

PORTB,loadLN

; Reset shift register

bsf

PORTB,loadLN

nextBit:

movf

PORTB,w

; Read Port-B (only LOB is

; meaningful in this routine)

movwf

workReg

; Store value in local

;register

rrf

workReg,f

; Rotate LOB bit into carry

; flag

rlf

dataReg,f

; Carry flag into dataReg

decfsz

bitCount,f

; Decrement bit counter

goto

shiftBits

; Continue if not zero


364

Chapter 14

Return

; done

shiftBits:

bsf

PORTB,clk65LN

; Pulse clock

bcf

PORTB,clk65LN

goto

nextBit

; Continue

The procedure in165 is in the program Serial6465 listed at the end of this chapter.

74HC164 Serial-to-Parallel Shift Register

The circuit in Figure 14-9 also uses a 74HC164 serial-to-parallel shift register for output to the eight LEDs. Figure 14-11 shows the pin-out of the 74HC164 IC.

input A

+5V

1

14

input B

2

13

Q7

Q0

3

12

Q6

Q1

4

74HC164

11

Q5

Q2

5

10

Q4

Q3

6

9

reset/clear

GND

7

8

clock

Figure 14-11 74HC164 Pin Out

Serial input into the 164 is through the input A line (pin number 1). Parallel output is through the lines labeled Q0 to Q7. The reset/clear line (on pin 9) and the clock line (on pin 8) provide the control functions. The operations are as follows:

1.A local data storage register holds the 8-bit value that serves as data input. A local counter is initialized for 8 data bits.

2.The 164 shift register is cleared by pulsing the reset/clear line.

3.The first/next bit of the data operand is placed on the input line.

4.Bit is shifted-in by pulsing the 164 clock line.

5.Bit counter is decremented. If it goes to zero the routine ends.

6.Otherwise, the bits in the source operand are shifted and execution continues at step number 3.

The following code fragment lists a procedure to interface a 16F84 PIC with a 74HC164 serial-to-parallel shift register:

;=====================================================

; constant definitions from wiring diagram

;=====================================================

#define clockLN 1

;|


Communications

365

#define clearLN 2

;|

— 74HC164 lines

#define dataLN 0

;|

...

;============================================================

;74HC164 procedure to send serial data ;============================================================

;ON ENTRY:

;local variable dataReg holds 8-bit value to be

;transmitted through port labeled serialLN

;OPERATION:

;1. A local counter (bitCount) is initialized to

;8 bits

;2. Code assumes that the first bit is zero by

;setting the data line low. Then the high-order

;bit in the data register (dataReg) is tested.

;If set, the data line is changed to high.

;3. Bits are shifted in by pulsing the 74HC164

;clock line (CLK).

;4. Data bits are then shifted left and the bit

;counter is tested. If all 8 bits have been sent

;the procedure returns.

out164:

; Clear 74HC164 shift register

bcf

PORTA,clearLN

; 74HC164 CLR clear low

bsf

PORTA,clearLN

; then high again

; Init counter

movlw

0x08

; Initialize bit counter

movwf

bitCount

sendBit:

bcf

PORTA,dataLN

; Set data line low (assume)

;Using this assumption is possible because the bit is not

;shifted in until the clock line is pulsed.

btfsc

dataReg,highBit

;

test number bit 7

bsf

PORTA,dataLN

;

Change assumption

if set

;=========================

;pulse clock line ;=========================

;Bits are shifted in by pulsing the 74HC164 CLK line

bsf

PORTA,clockLN

; CLK high

bcf

PORTA,clockLN

; CLK low

;=========================

; Rotate data bits left

;=========================

rlf

dataReg,f

; Shift left data bits

decfsz

bitCount,f

; Decrement bit counter

goto

sendBit

; Repeat if not 8 bits

;=========================

;

end of transmission


366

Chapter 14

;=========================

return

It is important to note that serial communications that use shift register ICs are described as synchronous. Synchronous serial transmission requires that the sender and receiver use the same clock signal or that the sender provide signal or pulse so as to indicate to the receiver when to read the next data element from the line. In the circuits discussed in this section the shift/load, reset/clear, and clock lines provide this synchronous interface between the PIC and the shift register IC.

The program named Serial6465, in the book’s on line software, is a demonstration of PIC-to-shift register interfacing.

14.4 PIC Protocol-based Serial Programming

In the preceding sections we discussed circuits and developed software using PIC serial communications that did not conform to any particular protocol or standard. This style is adequate for stand-alone applications and circuits. On the other hand,

PIC-based circuits sometimes communicate with systems that conform to a specific communications standard, for example, with a PC through its RS-232-C serial port. In this case, the PIC software and hardware must conform with the protocol, at least to an operational minimum that ensures satisfactory interfacing with the protocol-based system.

In the context of protocol-based programming, two situations are possible: either the PIC in use supports the communications standard or protocol or it does not. In the case of the smaller PICs, such as the 16F84, the software emulates communications protocols since hardware provides no support. The more complex PICs, on the other hand, often contain hardware modules that provide a functionality equivalent to that required by the various standards. In this sense, mid-range and high-range PICs often include hardware support for one or more communication standards and conventions. For instance, the 16F87X PIC family includes an MSSP (Master Synchronous Serial Port) module and a USART (Universal Synchronous/asynchronous Receiver and Transmitter) module.

In the sections that follow we develop circuits and programs for cases in which the on-board PIC does not contain hardware support for the standard and for cases in which it does. Examples with PICs that do not provide hardware support for serial communications use the 16F84. Examples with PICs that provide hardware serial communications support use the 16F877, which contains an MSSP and a USART module. The 16F877 circuits and applications in the present chapter use the processor’s USART module. The 16F877 MSSP module is demonstrated in the chapter on EEPROM programming.

14.4.1 RS-232-C Communications on the 16F84

The UART (Universal Asynchronous Receiver/Transmitter) controller is a serial communications IC found in computers and other data communication devices. In the PC, the UART was originally National Semiconductor INS8250. With the introductions of the PC AT, IBM changed its serial IC to the NC16450, an improved 8250. Later PCs

Communications

367

adopted the NS16550A UART as their serial communications controllers. Other vendors, including Intel and Western Digital, furnish clones of the NS16550A and other UARTs.

The UART-based serial port implementation and circuitry in the PC is compliant with RS/EIA232. For a PIC-based circuit to communicate with a PC’s serial port it must either implement in hardware or emulate in software the RS232 signals and protocol. One possibility is to include a UART or UART-like IC in the circuit. But this option is not simple to implement since RS-232-C requires voltage levels that are not TTL-compatible.

For PIC-based systems without a UART module, a viable approach is to emulate UART functions in software, at least those required for interfacing with the PC hardware. This is quite feasible due to the availability of dedicated ICs that provide

RS-232-C-compatible signals and voltage levels in systems in which a ±12 volt source is not available. These chips, sometimes called RS-232-C Drivers/Receivers or Transceivers, are especially useful in interfacing UART and USART-based systems with PIC-based hardware.

The RS-232-C Transceiver IC

RS-232-C interface ICs are available from several vendors, although the ones from Dallas Semiconductors’ Maxim line are probably the most popular. These chips, sometimes called RS-232-C driver/receivers, have in common the use of so-called charge-pump DC/DC converters that generate, from the +5 volt TTL power source, the polarities and voltage levels required by RS-232-C.

One of the most popular implementations of the RS-232-C transceiver used in

PIC-based systems is the MAX232 and its upgrade, the MAX202. One improvement in the MAX202 is to provide some degree of human-body electrostatic discharge protection (ESD), a desirable feature in experimenter boards. Other versions are the MAX233 and MAX203, which do not require external capacitors. Other RS-232-C transceiver ICs with various additional features, such as automatic shutdown, are available. Figure 14-12 is a pin-out of the MAX232 and 203 ICs.

C1+

1

16

+5V

V+ 2

15

GND

C1- 3

MAX202

14

D1out (RS-232)

C2+

4

MAX232

13

R1in (RS-232)

C2- 5

12

R1out (TTL)

V- 6

11

D1in (TTL)

(RS-232) D2out 7

10

D2in (TTL)

(RS-232) R2in 8

9

R2out (TTL)

Figure 14-12 MAX202 and MAX232 Transceiver Pin Out