Файл: The quintessential PIC microcontroller (S. Katzen, 2000).pdf

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

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

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

Добавлен: 15.06.2025

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

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

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

12. One Bit at a Time 307

dp

dp

dp

330R

330R

330R

SRG8 [74HCT164] R

C1/

& 1D

SRG8 [74HCT164] R

C1/

& 1D

SRG8 [74HCT164] R

C1/

& 1D

VDD

VDD

VDD

SCK

RA1

RA0 SDO

Fig. 12.2 Serial interface to a 3-digit 7-segment display.

driver routine of Program 11.7 on page 301 which converts a binary byte to an array of BCD digits in HUNDREDS, TENS and UNITS. These are mapped to 7-segment code and then sent out to each digit 8-bits at a time.

To serialize this process we require to design a subroutine to put each bit of a specified file register DATA_OUT out at SDO while pulsing SCK, beginning with the leftmost bit. A task list for such a subroutine is:

1.Bring SCK low.

2.COUNT = 8.

3.WHILE COUNT > 0 DO:

(a)Shift DATA_OUT left into Carry.

(b)Copy Carry to SDO.

(c) Pulse SCK / \ .

(d) Decrement COUNT.

Program 12.1 shows two subroutines. The first called DISPLAY is closely akin to Program 11.7 in that it calls the subroutines BIN_2_BCD and then sends the 7-segment coded bytes out to the interface registers. In this instance the units byte is sent first as this will eventually be shifted to the far end of the chain; followed by the tens and finally the hundreds byte.

The actual serial transmission is handled by the subroutine SPI_WRITE, which implements our task list. The datum placed by the caller in file register DATA_OUT is shifted left and the state of the Carry bit used to make the Serial Data Out pin RA0 0 or 1. The Serial ClocK pin RA1 is then toggled once / \ to shift the data into the shift register chain. This is repeated eight times to complete the transaction, which takes a maxi-


308 The Quintessential PIC Microcontroller

Program 12.1 Displaying the decimal equivalent of a binary byte using a serial data stream.

SDO

equ

0

SCK

equ

1

DISPLAY

bcf

PORTA,SCK

; Initialize the clock line

movf

BINARY,w

; Get binary byte

call

BIN_2_BCD

; Convert to 3-digit BCD

movf

UNITS,w

; Get Units nybble

call

SVN_SEG

; Convert to 7-segment code

movwf

DATA_OUT

; Copy into the serial register

call

SPI_WRITE

; Shift it out

movf

TENS,w

; Get Tens nybble

call

SVN_SEG

; Convert to 7-segment code

movwf

DATA_OUT

; Copy into the serial register

call

SPI_WRITE

; Shift it out

movf

HUNDREDS,w

; Get Hundreds nybble

call

SVN_SEG

; Convert to 7-segment code

movwf

DATA_OUT

; Copy into the serial register

call

SPI_WRITE

; Shift it out

return

; *****************************************************

; * FUNCTION: Clocks out a byte in series, MSB first

*

; * ENTRY : Datum in DATA_OUT

*

; * EXIT

: DATA_OUT zero

*

;*****************************************************

;Task 1

SPI_WRITE

bcf

PORTA,SCK

; Make sure clock starts at low

; Task 2

movlw

8

; Initialize loop counter to 8

movwf

COUNT

; Tasks

3(a)&(b)

LOOP

bcf

PORTA,SDO

; Zero data bit

rlf

DATA_OUT,f

; Shift datum right into Carry

btfsc

STATUS,C

; Skip if Carry is 0

bsf

PORTA,SDO

; ELSE make data bit 1

; Task 3(c)

bsf

PORTA,SCK

; Pulse clock

bcf

PORTA,SCK

; Task 3(d)

decfsz

COUNT,f

; Decrement count

goto

LOOP

; and repeat until zero

return

mum of 87 cycles to complete, depending slightly on the data pattern. A complete update of the display will take around 120 µs with a processor clock of 8 MHz and excluding the time spent in doing the data conversion.


12. One Bit at a Time 309

Where a long chain of shift registers is being serviced, speed may be improved a little if each register has its own data feed but all clocked with the same SCK pin or sharing the same lines but each with a separate Enable. This latter technique is the method used in Fig. 12.8.

One problem with our shift register technique is that for the period where shifting is in process the data appearing at the port outputs are not valid; for 23 clock pulses in our example. Of course in this situation the response of the eye to microsecond changes in illumination makes this observation spurious. However, this may not always be the case and in such instances the shift register may be bu ered from the parallel outputs using an array of D flip flops or latches, which can be loaded after the shifting process has been completed to give a single update.

A B C D E F G H

G

EN3

C2

2D

RCK

SOUT

SRG8 R C1/

1D

SCLR

SCK

SIN

Fig. 12.3 Logic functional diagram of the 74HCT595 octal shift register with output register.

Rather than employing a separate bu er register, a more e cient solution typically uses the 74HCT595 of Fig. 12.3 with its integral 8-bit parallel-in parallel-out (PIPO) register between the shift register and the outside world. A rising edge / on the RCK (Register ClocK) pin transfers the serialized data to the parallel outputs. The last stage output of the shift register is made available to allow cascading to any length. All RCK pins can be pulsed together to allow the entire chain to simultaneously update.

One example where rippling of data may be undesirable, is were a digital datum is to be converted to its analog equivalent. In Fig. 12.4 the conversion is carried out using a National Semiconductor DAC0800. Essentially the analog voltage is a linear function of the 8-bit digital input


310 The Quintessential PIC Microcontroller

+10V

4K7

[DAC0800]

4K7

LSB

MSB

3

[74LS595]

SRG8 R

2D

EN3 C2

C1/

1D

RA2

LOAD

VDD

RA1

SCK

RA0

SDO

4K7

+

-00000000 = -9.96V

4K7

11111111 = +9.96V

Serial output

to further serial ports

Fig. 12.4 Serially interfacing to a DAC0800 digital to analog converter.

varying from −9.96 V for an input of 00000000b through +9.96 V for 11111111b – see Fig. 14.13 on page 419.

Using a 74HCT595 registered shift register, the digital input does not change until the new datum is in place and the PIC pulses the C2 Register Clock, giving clean changes in the data presented to the DAC and corresponding analog output.

Data can be input serially in a similar manner using parallel-in serialout (PISO) shift registers. The example shown in Fig. 12.5 is a serialized version of the intruder alarm of Fig. 11.10 on page 287 using only three lines to connect to all eight sensor groups; a considerable economy compared to the original 16 channels.

Each sensor group is attached to a 74HCT165 8-bit PISO shift register, with the serial output of the further register feeding the serial input of the next nearest register. Once the data has been loaded in, it may be shifted into the SDI (Serial Data In) parallel input RA1 and assembled bit by bit. In the specific case of the multi-zone intruder alarm, after each eight shifts the assembled byte can be tested for non zero and the appropriate action taken – see SAQ 12.1.

Also shown in Fig. 12.5 is the single output port used to display the active zone. As both input SDI and output SDO serial channels share the same shift clock SCK, then shifting data in will also clock this serial output port. Conversely, sending data to the output port will shift data in from the Zone ports. In this example there is no problem as microsecond fluctuations in the Zone lamps are of no consequence, and the sequence of operations ends with the output port being accessed with the earmarked data. Where this interaction is undesirable, then either the appropriate


12. One Bit at a Time 311

[74HCT165]

G1 [SHIFT]

C2 [LOAD]

1

>1

1

C3/

3D

Zone 7

2D

[74HCT165]

G1 [SHIFT]

C2 [LOAD]

1

>1

1

C3/

3D

Zone 6

2D

74HCT165

Z1Z2Z3Z4Z5Z6Z7

X

Z0

&1D

8

C1/

[74HCT165]

R

DD

[74LS164]

V

G1 [SHIFT]

SRG8

C2 [LOAD]

1

>1

1

C3/

3D

Zone 0

2D

PIC

LOAD

SCK

SDI

SDO

RA4

RA2

RA1

RA0

Fig. 12.5 Serially interfacing to the multi-zone intruder alarm.