Файл: 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

456 The Quintessential PIC Microcontroller

SET switch is released will be the new time-out and will be retained indefinitely until another set process.

The first decision to be made is the choice of microcontroller (MCU). In this case we are constrained by the need to use our book’s model device, i.e. one of the mid-range PIC family. As we require non-volatile storage to store the time-out value we are limited at the time of writing to the PIC16F8X and 16F87X lines of devices to avoid the necessity to use an external serial EEPROM. As the display will physically be large for long-distant viewing the bulk of the circuit is not critical so the cheaper PIC16F84 device is our choice. The 18-pin count of this device compared to the alternative 40-pin PIC16F874 will require additional support functions to expand the port pin budget, but should better illustrate the trade- o s of more complex systems.

Based on this decision the final target hardware is shown in Fig. 16.1. The port pin budget is allocated as follows:

U1

+5V

C1

D1

RED

R2

16

OSC1

RB7

13

330R

33p

Y1

12

D2

YELLOW

R3

330R

C2

3.2768MHz

RB6

GREEN

11

D3

R4

330R

15

OSC2

RB5

GO

33p

RB4

10

S2

GND

SET

RB3

9

NOTE: RBPU (bit 7) of OPTION (81h)

S3

STOP

GND

RB2

8

must be set to enable

S4

DIAG

the Port B’s weak pull-ups

RB1

7

S5

PAUSE

RB0/INT

6

S6

3

RA4/TOCKI

BZ1

RA3

2

U2

+5V

RA2

1

+5V

\Reset

9

BUZZER

RA1

18

Clock

8

R1

33K

RA0

17

Data

S1

1

3

R5

U4

GND

4

MCLR

2

1

16

1

RESET

4

2

15

14

PIC16C84-04

5

3

14

12

4MHz PIC

6

4

13

10

10

5

12

4

11

6

11

2

3

+5V

12

7

10

13

5

13

8

9

9

16

74HC164

R-DIL8

6

11

330R

7-SEGMENT

U3

Common-anode

9

8

1

3

R6

U5

2

1

16

1

4

2

15

14

5

3

14

12

6

4

13

10

10

5

12

4

11

6

11

2

3

12

7

10

13

5

13

8

9

9

16

74HC164

R-DIL8

6

11

330R

7-SEGMENT

Common-anode

Fig. 16.1 The annunciator hardware.


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.

458 The Quintessential PIC Microcontroller

ficient illumination and means that the 74HC164 shift registers do not need current bu ering.3

Crystal

A 3.2768 MHz crystal provides the timing for the MCU’s clock oscillator, giving an instruction rate of 819.21 kHz. A typical crystal of this value has a tolerance of ±30 ppm and temperature coe cient of ±50 ppm.

This unusual choice is 216 × 50 so if we use the 8-bit Timer 0 with a prescale value of 1:64 then we can create an interrupt 50 times per second. An alternative low-power configuration would be to use a 32.768 kHz crystal and generate an interrupt every two seconds. However, compared to the current consumption of the optical components, the MCU’s power dissipation is minor.

With the hardware environment designed, we can now concentrate on the software.

MAIN

Reset

Timer 0

SET

PAUSE

SET_TIME

MINUTE++

DIAG

DIAGNOSTIC

Main loop

STOP

OUTPUT

Fig. 16.2 The modular software structure.

Figure 16.2 shows the basic modular structure for our system. Here the distinctive double right/left edged box denotes a subroutine or Interrupt Service Routine (ISR). Three distinct processes can be identified together with two major supporting tasks.

3Alternatively low-current seven-segment displays are available.


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