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

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

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

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

Добавлен: 15.06.2025

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

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

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

11. One Byte at a Time 285

0 RBIF INTCON

Read

TRIS

buffer

Data

flip

flop

RB7

TRIS

Capture

D

latch

Data bus

1D

C1

h

Read from

0B/8B

Port B

Change

File

D

latch

1D

0)

C1

(R/W

S

RB6:4

from/write to

Port B

Fig. 11.9 The Port B change feature.

of the input pin in the normal way. However, at the same time the Change latch becomes transparent. When the reading action is over, the Change latch freezes and captures the pin state as it is at the time of reading. The outputs of both the Capture and Change latch are Exclusive-ORed together. As we have seen in page 14, an XOR gate detects di erences between its two inputs. As the Capture latch is now transparent, any subsequent change at the pin input will cause the output of the associated XOR gate to go to logic 1. Each of the four Port B cells RB7:4 has a Change feature and the four XOR gates are ORed to give a composite signal which sets the RBIF (Register B Interrupt Flag) in the INTCON of Fig. 7.4 on page 178. If the RBIE (Register B Interrupt Enable) bit is set then this is a convenient way of awaking a PIC slumbering in its Sleep state. If the GIE (General Interrupt Enable) bit is set as well, a change in the top nybble of Port B will cause an interrupt as well. Each XOR gate is ANDed with the TRIS line so that only bits that are programmed as an input can contribute to the Change signal.

Care must be taken in using this facility. For example, using the lower (non-change) part of Port B (e.g. bclr PORTB,0) can a ect the Change facility by forcing the latches to resample. Also a change may occur at the instant the port is being read and may be missed, although later A parts (e.g. PIC16C74A versus PIC16C74) altered the sampling logic to remove


286 The Quintessential PIC Microcontroller

this latter problem. Neither of these foibles are factors if a keypad is used to awaken a sleeping PIC.

Once the PIC has responded to the Change interrupt the Change signal setting RBIF should be removed by reading Port B, which equates the state of the two D latches. Only then should RBIF be cleared. Failure to do this initial read will result in this interrupt flag being immediately set again.

As an example, using the keypad to awaken the PIC with the assumption that GIE is zero (no interrupt) should be implemented as:

movf

PORTB,w

; Read Port B to cancel any difference

bcf

INTCON,RBIF

; Clear the Change Interrupt Flag

bsf

INTCON,RBIE

; Enable the Change Interrupt Enable

sleep

; Go to sleep

; zzzzz

call

DELAY

; On wakening let things settle

movwf

PORTB,w

; before cancelling any difference

bcf

INTCON,RBIF

; Clear the Change Interrupt Flag

bcf

INTCON,RBIE

; Disable Change Interrupt facility

Most PIC devices have relatively few I/O port lines – see Table 11.1. Even the larger footprint devices, such as the PIC16C74 with 33 peripheral pins, may not have enough parallel I/O resources for some projects, especially as several other peripheral devices may need to use the shared I/O pin budget.

As an example, consider a multi-purpose intruder alarm which can monitor up to eight zones– for example, floors in a multi-story building. Each zone can have up to eight movement sensors. A display of eight lamps back at base is to be used to indicate in which zone the intruder is located.

Based on this specification, a budget of 72 (64 input and 8 output) parallel I/O pins will be required. Rather than using one PIC device per zone reporting back to a main controller7 it has been decided to expand the I/O capabilities of a single PIC16F84 device.

One expansion architecture is shown in Fig. 11.10. Here Port B is used to implement an external data bus which connects to the eight zone 3- state bu ers and one indicator flip flop array. Each zone’s set of sensors are interfaced to this local bus via an octal 3-state bu er. One of eight bu ers can be enabled using a 3 to 8-line decoder addressed from Port A. For example, if RA2:RA1:RA0 were 111b and RA3 = 0 then Zone 7’s bu ers are enabled and its eight sensors can be read in at Port B.

To activate the one output lamp array, RA3 should be logic 1 and Port B set to output. Data can then be clocked into the flip flop array by pulsing RA0 low then high to give a rising edge.

The number of output ports may be expanded in this architecture to eight by using a second 3 to 8-line decoder to select the port enabled

7An implementation that is perfectly feasible and cost e ective; see SAQ 11.1.


X/Y

[74HCT138]

RA0

1

7

RA1

2

6

RA2

4

5

+5V

4

3

&

2

1

Fig

RA3

EN

0

.11 .

Zone 0

Zone 7

A 10

[74HCT244] x 8

-multi

zone

RB7

RB6

intruder

RB5

RB4

RB3

11

RB2

alarm

One .

RB1

RB0

.

Time a at Byte

[74HCT377]

1C2

G1

2D

Z7 Z6Z5Z4Z3Z2Z1Z0

287


288 The Quintessential PIC Microcontroller

when RA3 = 1. However, up to two extra output ports could be added by simply substituting RA0 by RA1 and RA2 to enable these two additional flip flop arrays. For example, one port could show which sensor(s) within the zone was active and RA4 used to sound a buzzer if any zone was active.

Program 11.3 Interacting with the intruder hardware.

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

; *

FUNCTION:

Reads Zone N

and

activates

lamp N

*

; *

ENTRY

:

N is in file

register

ZONE, 00000nnn

*

;

*

EXIT

:

Lamp

N active if

Zone

N is

non zero

*

;

*

EXIT

:

ZONE

zero and TEMP unchanged

*

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

ZONE_N

bsf

STATUS,RP0

; Change to Bank

1

movlw

0FFh

; Set Port B to input

movwf

TRISB

clrf

TRISA

; Set Port A to output

bcf

STATUS,RP0

; Change to Bank

0

movf

ZONE,w

; Get N, used

movwf

PORTA

; to select Zone

N’s buffers

nop

; Delay to allow

long lines

nop

; to settle

movf

PORTB,w

; Now read data on Port B

btfsc

STATUS,Z

; IF not zero THEN an intruder!

goto

LAMP_OFF

; otherwise all clear

; Intruder found, now activate lamp

bsf

STATUS,RP0

; Change to Bank

1

clrf

TRISB

; Port B now set

to output

bcf

STATUS,RP0

; back to Bank 0

; Convert binary number to unary equivalent to

activate lamp

movlw

0FFh

; All ones

movwf

TEMP

; into TEMP

bcf

STATUS,C

; Zero Carry bit

incf

ZONE,f

; Map zone range

to 1 -- 8

Z_LOOP

rlf

TEMP,f

; Shift pattern <--

bsf

STATUS,C

; Set Carry bit

decfsz

ZONE,f

; Decrement Zone

number

goto

Z_LOOP

; and repeat N times

; TEMP holds

the unary lamp activation pattern

movf

TEMP,w

; Get it

LAMP_OUT

bsf

PORTA,3

; Enable output port

movwf

PORTB

; Lamp data

bsf

PORTA,0

; Clock it in by

pulsing RA0

bcf

PORTA,0

return

; All done

; Go here if

no

intruder found and turn off all lamps

LAMP_OFF

bsf

STATUS,RP0

; Change to Bank

1

clrf

TRISB

; Port B now set

to output

bcf

STATUS,RP0

; back to Bank 0

movlw

0FFh

; All ones turns

lamps off

goto

LAMP_OUT


11. One Byte at a Time 289

To show how this hardware interacts with the software consider the subroutine in Program 11.3 below that reads Zone N and if non-zero then lights lamp N; where N is an integer 0–7 in a file register called ZONE on entry. We assume that an active sensor gives logic 1 and a lamp illuminates on a logic 0.

Checking Zone N is simply a matter of setting Port B up as an input port and sending the Zone N binary pattern to Port A. The 3 to 8-line decoder is enabled whenever RA3 is low, so no processing of the Zone binary code is needed. Due to the long connection lengths, a short delay is introduced to allow data to settle. For a real system, a delay of several hundreds of milliseconds and a digital smoothing routine, such as the debounce routine of Program 11.2, would be needed for reliable data acquisition, assuming that the zone bu ers were geographically distant.

Activating the eight lamps is a little more tricky. In either case, Port B must be configured as an output. The lamps are then actuated by sending the appropriate pattern to Port B, bringing RA3 high and then pulse RA0. This is implemented in Program 11.3 in routine LAMP_OUT. The lamp datum is simply all logic 1s where no intruder has been detected, that is where the sensor data has been read as all zeros.

When an intruder has been detected, then lamp N alone must be lit; for instance, 10111111b for Zone 6. To do this, the binary zone code in ZONE must be converted to the appropriate unary (one of n) code. For example, Zone 2 00000010b maps to 11111011b, Zone 3 00000011b maps to 11110111 etc.

In the program the unary code is built up in file register TEMP, which is initially set to 11111111b. By clearing Carry before entering the loop at Z_LOOP but setting it to 1 within the loop, a single zero can be shifted left using the Rotate Left File instruction rlf TEMP,f. This gives the se-

quence 11111111 ← 11111110 ← 11111101 ← 11111011 . . . 01111111. As this shift progresses, the ZONE datum (mapped to the range 1–8 so that at least one shift is implemented) is decremented and the loop exited when this reaches zero. Thus the position of the lone 0 (the initial C = 0) represents the original zone number. This unary code is then sent out to the lamp port at LAMP_OUT to activate the one-of-n indicator.

Examples

Example 11.1

A 2N3055 NPN bipolar transistor is to be used to activate the field coils of a small stepper motor. Taking into account the minimum gain of the transistor over the range +85 → −40◦C, it has been calculated that the base current must be at least 10 mA. The transistor is to be controlled from a port pin and its base-emitter voltage can be assumed to be no more than 0.7 V and VDD is 5 V. What is the maximum value of the base

290 The Quintessential PIC Microcontroller

resistor RB and given this value, what will be the worst-case maximum base current?

Solution

For currents of this magnitude we can assume that the pin voltage will be less than 5 V. The data sheet specifies a minimum voltage of 4.3 V (a drop of 0.7 V) for a IOH of −3 mA but for currents greater than this we must resort to graphical techniques.

Figure 11.11 shows the graphical relationship of output source current IOH for a high output voltage state VOH. The grey area is bounded by the minimum situation, which is at +85◦C and maximum condition at −40◦C.

This voltage VOH is also a function of the transistor input base resistor circuit according to the equation VOH = 0.7 + IOH × RB. This straight line relationship (called a load line) is shown on the graph from (0,0.7) drawn to intersect the minimum locus at a current of −10 mA. This crossover is the only point that satisfies both current-voltage relationships. The

VOH

IOH

RB

5 V

0.7 V

4 V

3 V

2 V

1 V

Min @ +85 C

Max @ -40oC

0

-10

-20

-30

-40

-50

IOH(mA)

Source (negative)

Fig. 11.11 Source current against voltage.