Файл: The quintessential PIC microcontroller (S. Katzen, 2000).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 15.06.2025
Просмотров: 5318
Скачиваний: 0
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