Файл: The quintessential PIC microcontroller (S. Katzen, 2000).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 15.06.2025
Просмотров: 5293
Скачиваний: 0
15. To Have and to Hold 453
tions of labels is much more reliable. However, in our case we would need to add nn to the address the assembler selects for the label TABLE. Unfortunately Program store addresses are 13-bits wide and PIC arithmetic is only 8-bit. Microchip compatible assemblers have the directives high and low to separate the upper and lower bytes parts of a label; eg. movlw low TABLE. Using these directives modify the subroutine SQUARE if the directive org 300h is removed.
15.3 Microchip-compatible assemblers have the directive da (DAta) which can be used to store strings of character codes in Program memory. For example:
MESSAGE da "Hello world/n",0
which places the codes characters in quotes coded in 7-bit ASCII code packed two at a time in each 14-bit word followed by all zeros. The \n escape character means New Line – ASCII code 0Ah.
Assuming that this is done in a PIC16F87X device, write a subroutine called PDATA (Print DATA) to fetch each character from Program memory and transmit to a terminal using the subroutine PUTCHAR of Program 12.11 on page 340.
15.4 A certain hotel security system is to use a PIC-based reprogrammable smart card for electronic guest room locks. On registration the card is to be charged up with the following details:
1.A 4-digit room number, eg. 1311.
2.Start data, eg. 13072000.
3.End date, eg. 15072000.
Assume that the PIC has an integral EEPROM Data module and communicates with the receptionist’s terminal via a serial input subroutine, such as described in Program 12.11 on page 340. Data is coded in ASCII in the order outlined, preceded with the character STX, terminated by ETX and delimited by SP – see Table 1.1 on page 5. Design a routine to interpret the data and store them in EEPROM.
CHAPTER 16
A Case Study
Up to this point our microcontroller material has been presented piecemeal. To complete our study we are going to put much of what we have learnt to good use and design both the hardware and software of an actual widget (gadget). This is not an easy task to do in a single short chapter. However, very little new material needs to be presented at this point, rather a process of coalescence.
We begin with our specification. Students invariably talk too long during their oral presentations. It is proposed that a dedicated embedded microcontroller-based system be designed to act as a time monitor. This monitor should default to a time-out of 10 minutes, but will have the provision to vary the allotted time from 1 to 99 minutes.
Once triggered, the monitor should perform the following sequence of operations:
1.When the GO switch is closed, a green lamp will illuminate and a
dual seven-segment display will show a count-down from the timeout value to at one-minute intervals.
2.After a further minute, an amber lamp only will illuminate, the count of will be displayed and a buzzer will sound for nominally one second.
3.After a further minute, a red lamp only will illuminate together with a display of . The buzzer will sound for two seconds.
4.Finally, after another minute the display will show , the red lamp will continue to be illuminated and the buzzer will sound continuously until the STOP switch is pressed. This will halt the timer and turn o all displays, lamps and buzzer. Indeed, closing the STOP switch at any time during the sequence above will cause the system to permanently halt. The system may be restarted from the time-out value by resetting the processor.
5.At any time the sequence can be frozen by toggling the PAUSE switch. When toggled again, the sequence will continue on from where it left o .
6.In order to alter the time-out from the default value of , the SET switch must be closed when the system is reset. The display will then show and will count down slowly. The value showing when the
16. A Case Study 457
Switches
The five switches S2…S6 implementing the functions GO, SET, STOP, DIAG, PAUSE are read from Port B at RB[4:0]. By using this port’s internal pull-up resistors (see Fig 11.7 on page 280) no external resistors are required.
S1 with R1 provides a Manual reset in order to restart the count. This MCLR signal also provides a Reset signal feed for external circuitry.
All six switches can be conveniently implemented as momentary contact keyboard switches.
Lamps
Three suitably colored 10 mm (0.4") high-brightness LEDs D3…D1 driven from RB[7:5] provide the light signals. 330Ω series resistors limit the current to nominally 10 mA.
Buzzer
The buzzer should be a miniature solid-state device. A typical piezoelectric implementation will operate over a wide d.c. voltage range of typically 3–16 V and require little more than 1 mA at 5 V.1
The buzzer is driven via RA2.
Numerical display
Two 7-segment displays give the required 2-digit read-out, facilitating the maximum specified period of 99 minutes. As only four port pins remain, a serial interface is implemented. This is similar to that shown in Fig. 12.2 on page 307 but each SIPO shift register has a separate data feed, with RA0 being used for the ten’s digit and RA3 for the units digit. Both digits can therefore be simultaneously updated with eight shifts.
The common-anode seven-segment display pinning shown in the diagram is that of the 16-pin Dual In Line (DIL) footprint with both left and right decimal points – lhdp and rhdp. Only the latter is used here (to indicate that the system has paused) in conjunction with the 8-bit 74HC164 shift register. Alternative 16and 14-pinouts are commonly available and even dual-digit packages. However, even the 16-pin footprint pinout is not standardized.
Smaller-sized displays, typically below 0.8 /20 mm, use a single LED for each bar, with a conducting voltage drop of around 2 V.2 The DIL 330 Ω series resistors R5 and R6 limit the current to around 10 mA. The common anodes are connected directly back to the normal +5 V power supply to avoid current surges a ecting the logic circuits, and should be decoupled by small tantalum capacitors. Although the displays are normally rated for 20 mA, restricting the current to this value gives suf-
1If you want to put paid to any possibility of the speaker continuing, a piezo-electric sound bomb producing 110 dB at 1 m distance needs a 12 V d.c. supply at 200 mA.
2Larger displays, e.g. 2.24 /56 mm, have typically two or four LEDs in series. In the latter case a separate 12 V supply would be needed and current bu ering.
16. A Case Study 459
Timebase task
All processes are time related. Timekeeping is implemented in hardware by generating an interrupt 50 times each second. By keeping a Ji y count, seconds and minute tasks are updated and are used to sequence the appropriate process.
By monitoring the PAUSE switch this decrementing time chain can be by-passed, hence freezing the countdown for as long as necessary.
Display task
All processes need to output the state of the count or status information to the two 7-segment displays. As this involves parallel to serial conversion and shifting, the task is better gathered into one module.
Main process
The Main process is a loop displaying the Minute count until it reaches zero, with a premature break if the STOP switch is closed.
Set-time process
If the SET switch is closed when the PIC is reset then the SET_TIME subroutine quickly decrements the display count until the switch is released. This displayed value is then written into Data EEPROM and is used by all subsequent Main processes as the starting value for the Minute count.
Diagnostic process
If the DIAG switch is closed on reset, the system enters a diagnostic subroutine. The essentially exercises each peripheral device in a manner calculated to ease hardware fault finding.
All processes are dependent on the Timebase task to pass basic realtime clock information back. As shown in Program 16.1 this is interrupt driven and is based on the Timer 0:Prescaler dividing down the 3.2763 MHz crystal-driven oscillator to give overflow every 501 s. As can be seen in Program 16.3, the Timer 0 interrupt is enabled and thus the PIC will enter ISR whenever the timer overflows – every 256 outputs from the Prescaler. Remembering that the crystal oscillator runs at 14 of the crystal frequency, a prescale ratio of 1:64 will give a timebase rate of 50
per second – 3.2763×106 = 50.
4×64×256
The task list for this function is:
1.IF PAUSE switch open THEN
(a)Decrement the time chain by one Ji y.
(b)IF new second THEN flag it.
2.ELSE
(a)Toggle the Pause flag.
(b)IF set THEN tell the world that the system is paused.
(c)ELSE display time to indicate normal running.
(d)Wait until PAUSE switch is released.
3.Return from interrupt.
460 The Quintessential PIC Microcontroller
Program 16.1 The timebase software. (continued next page).
; *************************************************************
; * |
The ISR to decrement the real-time clock |
* |
|
; |
* |
Adding a 20ms Jiffy on each entry |
* |
; |
* |
Sets NEW_SEC to a non-zero value each Minute update |
* |
;*************************************************************
;First save context in usual way
ISR |
movwf |
_work |
; |
Put |
away W |
swapf |
STATUS,w |
; |
and |
the Status register |
|
movwf |
_status |
;*************************************************************
;The core code
btfss |
INTCON,T0IF |
; Was it a Timer0 time-out? |
goto |
ISR_EXIT |
; IF no THEN false alarm |
btfsc |
Pause,0 |
; Check the Pause flag |
goto |
ISR_EXIT |
; IF closed THEN don’t increment |
bcf |
INTCON,T0IF |
; Clear interrupt flag |
incf |
JIFFY,f |
; Record one more 1/50 second |
movlw |
d’50’ |
; Has Jiffy count reached 50? |
subwf |
JIFFY,w |
|
btfss |
STATUS,Z |
|
goto |
ISR_EXIT |
; IF not THEN finished |
clrf |
JIFFY |
; ELSE zero Jiffy count |
movf |
SECOND,f |
; Test for Seconds count = 00? |
btfsc |
STATUS,Z |
|
goto |
NEW_MIN |
; IF it is THEN a NEW_SEC minute |
decf |
SECOND,f |
; ELSE decrement Seconds count and |
incf |
NEW_SEC,f |
; tell background prog new second |
goto |
ISR_EXIT |
; and exit |
NEW_MIN movlw |
d’59’ |
; Reset Seconds to 59 seconds |
movwf |
SECOND |
|
movf |
MINUTE,f |
; Test for Minutes count = 00? |
btfsc |
STATUS,Z |
|
goto |
ISR_EXIT |
; IF it is THEN no more decrement |
decf |
MINUTE,f |
; ELSE decrement Minutes |
; ************************************************************
ISR_EXIT btfss |
PORTB,PAUSE |
; Check the PAUSE |
switch |
call |
FREEZE |
; IF closed THEN update Pause flag |
|
swapf |
_status,w |
; Untwist the original Status reg |
|
movwf |
STATUS |
||
swapf |
_work,f |
; Get the original |
W reg back |
swapf |
_work,w |
; leaving STATUS unchanged |
|
retfie |
; and return from |
interrupt |
|