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

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

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

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

Добавлен: 15.06.2025

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

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

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

16. A Case Study 467

LEDs and buzzer

The static output devices are tested in conjunction with the switch test listed above. Of course the failure of a LED to light or buzzer to sound may be due to either the input or output device circuit. Determining which is easily accomplished by using a voltmeter or logic probe. Also all LEDs are illuminated during the Set-time process.

Display

Each of the display devices is exercised by lighting one segment moving on once each second in an endless loop. This is implemented by generating a walking unary pattern 11111110 → 11111101 → · · · 01111111 sent out to the output subroutine SPI_WRITE once each time the file register NEW_SEC is non zero. NEW_SEC is incremented in the Timer 0 interrupt-handling routine each time the Seconds count is incremented and cleared in the Diagnostic procedure code. This acts as a ratchet giving only one new display each second.

The Set-time process is entered when the SETT switch is closed whenever the processor comes out of reset. Its function is to allow the operator to change the contents of the EEPROM Data module location 00h to any value up to 99. This location holds the initial count-down value used by the Main process to determine the length of the procedure.

The strategy behind the coding shown in Program 16.5 is to initialize the Second count to 99 and let it decrement at a 1-second rate as

Program 16.5 The Set-time process.

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

; *

FUNCTION:

Slowly

counts down

from 99-00. When Set switch

*

; *

FUNCTION:

released EEPROM is

Written with NEW_SEC time-out*

; *

RESOURCE:

Subroutines

DISPLAY, EE_PUT, ISR; Var TIME_OUT

*

;

*

ENTRY

:

Set switch is closed

*

;

*

EXIT

:

EEPROM

Data

address 00 is updated

*

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

SET_TIME

movlw

d’99’

; Start count at 99 seconds

movwf

SECOND

; All LEDs on

movlw

b’00000000’

movwf

PORTB

SET_LOOP

movf

SECOND,w

; Get Second count

call

OUTPUT

; Display it and clear NEW_SEC

btfsc

PORTB,SETT

; Check; does the user want to stop?

goto

UPDATE

; IF yes THEN update EEPROM and exit

movf

SECOND,w

; Get displayed count

movwf

TIME_OUT

; Make a temporary copy

S_LOOP

movf

NEW_SEC,f

; Check NEW_SEC status

btfsc

STATUS,Z

; IF non zero THEN skip

goto

S_LOOP

; ELSE try again

goto

SET_LOOP

; Repeat display

UPDATE

movf

TIME_OUT,w

; Get the value

movwf

EEDATA

; Set up EEPROM

clrf

EEADR

; Program EEPROM

call

EE_PUT

return

; and return to main program


468 The Quintessential PIC Microcontroller

determined by the foreground ISR. The value of SECOND is sent to the Display subroutine each time the ISR sets the flag file register NEW_SEC to a non zero value, that is once per second. DISPLAY clears NEW_SEC so the net e ect to update the display only once each second. Each second the SETT switch is checked and when open the state of the Seconds count is transferred to the EEPROM Data module at UPDATE using the EE_PUT subroutine of Program 15.2 on page 436.

The Complete background system flow chart is shown in Fig. 16.3. This shows in outline the decision flow taken after a reset and in detail

Reset

MAIN

Initialize

yes

SET?

Set time-out

no

yes

Diagnostic

DIAG?

no

Green lamp on

Buzzer off

Set initial

value of count

DISPLAY

Output

MINUTE count

no

yes

Amber lamp on

MINUTE = 2?

Buzz 1 second

no

yes

Red lamp on

MINUTE = 1?

Buzz 2 seconds

no

no

yes

Red lamp

STOP?

MINUTE = 0?

& Buzzer on

no

no

yes

yes

no

yes

Next

Everything off

minute?

STOP?

Sleep

Fig. 16.3 The Main process.


16. A Case Study 469

the Main process. Although this looks rather complex, it may be broken down into five phases with corresponding coding shown in Program 16.6.

Preamble

On reset if neither SETT or DIAG switches are closed the Main procedure code is entered at MAIN_PROC. This reads the initial value of the countdown period from EEPROM location 00h and initializes the count chain. The green lamp is illuminated and other lamps and buzzer are turned o .

Countdown

The Countdown phase continually displays the Minute count – updated behind the scenes by the ISR. The green lamp remains illuminated as long as this display does not drop below . This phase is complete whenever the count drops below 3 minutes or else the STOP switch is closed. In the latter case all displays are blanked and the PIC is put into its Sleep state.

In all situations except where the STOP command is issued the Minute count is displayed at 1-minute intervals. The routine at REPEAT checks the Second count and if zero the loop is repeated – that is once per minute. The simpler alternative of continually refreshing the 7-segment readouts gives an inferior display as the data being serially shifted at frequent rate may partially illuminate segments which are nominally o . In addition, repeating the loop each minute eases the task of sounding the buzzer once only when the Minute count drops to two and one.

Two minutes to go

When the display is the amber lamp is illuminated This is timed using the NEW_SEC variable. Again the loop can be prematurely exited if the STOP switch has been closed.

One minute to go

When the display is the loop diverts to illuminate the red lamp. The buzzer is sounded for two seconds; implemented in code as two 1-second buzzes.

Timed out

When the Minute count reaches zero, not only is displayed but also the buzzer sounds continually. This cacophony can only be silenced by pressing the Stop switch – or by resetting and starting again. As in previous situations when the Stop switch is closed, all displays are blanked out and the PIC is placed in its Sleep state.

Once the source code has been assembled and where possible simulated (see Fig. 8.7 on page 222) it can then be burnt into the PIC’s Program store. In the first instance only the diagnostic software and associated tasks need be programmed in order to check the target hardware. The precise details will depend somewhat on the PIC programmer being used and its associated software.

470 The Quintessential PIC Microcontroller

Program 16.6 The Main process. (continued next page).

movlw

b’11000000’

; Green LED on

movwf

PORTB

bsf

PORTA,BUZ

; Buzzer off

; Get start value from EEPROM

clrf

EEADR

; EEPROM address zero

call

EE_GET

; Get the start value

movwf

MINUTE

movlw

d’59’

; Initial value for seconds

movwf

SECOND

; is 59

clrf

JIFFY

DISPLAY movf

MINUTE,w

; Get Minute count

call

OUTPUT

; Output to display

;The 2-minutes-to-go phase ***********************************

;At a count of two sound the buzzer for one second and turn on

;the amber lamp

TWO

movf

MINUTE,w

; Minute count = 2?

addlw

-2

btfss

STATUS,Z

goto

ONE

; IF not THEN try for one minute

movlw

b’10100000’

; Amber LED on

movwf

PORTB

bcf

PORTA,BUZ

; Buzzer on

TWO_LOOP movf

NEW_SEC,f

; Check NEW_SEC status

btfsc STATUS,Z

; IF non zero THEN skip

goto

TWO_LOOP

; ELSE try again

bsf

PORTA,BUZ

; Turn off buzzer after one second

goto

REPEAT

; repeat display

;The 1-minute-to-go phase ************************************

;At a count of one sound the buzzer for two second and turn on

;the red lamp

ONE

movf

MINUTE,w

; Minute count = 1?

addlw

-1

btfss

STATUS,Z

goto

ZERO

; IF not THEN try for zero minutes

movlw

b’01100000’

; Red LED on

movwf

PORTB

bcf

PORTA,BUZ

; Buzzer on

ONE_LOOP

movf

NEW_SEC,f

; Check NEW_SEC status

btfsc STATUS,Z

; IF non zero THEN skip

goto

ONE_LOOP

; ELSE try again

clrf

NEW_SEC

; Again clear NEW_SEC flag

UN_LOOP

movf

NEW_SEC,f

; Again check NEW_SEC status

btfsc STATUS,Z

; IF non zero THEN skip

goto

UN_LOOP

; ELSE try again

bsf

PORTA,BUZ

; Turn off buzzer after two seconds

goto

REPEAT

; Repeat display


16. A Case Study 471

Program 16.6 (continued.) The Main process

;The Timed-Out phase *****************************************

;When the Minute count reaches zero, sound the buzzer

;until the Stop switch is closed

ZERO

movf

MINUTE,f

btfss

STATUS,Z

goto

REPEAT

bcf

PORTA,BUZ

ZERO_LOOP

btfsc

PORTB,STOP

goto

ZERO_LOOP

FINI

movlw

b’11100000’

movwf

PORTB

bsf

PORTA,BUZ

movlw

b’11111111’

movwf

DATA_OUT_L

movwf

DATA_OUT_H

call

SPI_WRITE

sleep

REPEAT

btfss

PORTB,STOP

goto

FINI

movf

SECOND,f

btfss

STATUS,Z

goto

REPEAT

clrf

NEW_SEC

R_LOOP

movf

NEW_SEC,f

btfsc

STATUS,Z

goto

R_LOOP

goto

DISPLAY

;Minute count = 0?

;IF not THEN repeat after minute

;Buzzer on

;Check the Stop switch

;and continue until closed

;Turn lamps off

;and buzzer

;Code for blank

;Blank both displays

;and await another reset

;Check the Stop switch

;IF closed THEN freeze

;Wait until Second count is again zero

;i.e. for the next minute

;IF not THEN wait again

;ELSE wait one more second

;Check NEW_SEC status

;IF non zero THEN skip

;ELSE try again

;Repeat display

The screen shot shown in Fig. 16.4 shows the situation where the Microchip Picstart Plus development programmer is used in conjunction with the MPLAB IDE. Communication with the host computer is via a RS-232 serial port and contact is made from the Picstart Plus menu. The right-hand window allows the operator to set up the Configuration Bits (fuses), shown in the left-hand window. Once this is set up, the operator can Blank out, Read from, Program or Verify the contents of the EPROM or EEPROM Program store is the same as that produced by the last assembly process. This process can only be carried out where the Code Protect has not been turned on. Once this is the case, it is irreversible and neither Program or Verify tasks can be carried out.

The middle window shows the status of the Program or Verify process. As shown here, it is announcing that it has completed the task up to 03FFh and is reporting success. The complete process takes less than a


472 The Quintessential PIC Microcontroller

Fig. 16.4 Programming the PIC from MPLAB.

minute for the PIC16F84 and the 252 program words that this case study software generates.5

With F series PICs, the programming process may be repeated without any preparation of the Program store up to 100 times without any deterioration of the Flash EEPROM. C series PICS,6 such as the PIC16C74, have EPROM Program stores. Where the target has a quartz window it must be erased using a suitable UV-based EPROM eraser for approximately 20 minutes before programming, unless code is being added to previously unprogrammed memory. Although quartz windowed devices are necessary for development purposes, they are relatively expensive. Thus cheaper windowless version are used for production purposes, and are called One-Time Programmable (OTP) since they cannot subsequently be erased. Part numbers which are windowed are usually identified by JW postfix; for example, PIC16C74B-20/JW is a 20 MHz ceramic windowed PIC16C74B part and and the PIC16C74B-4/P is a 4 MHz OTP version in a 40-pin plastic DIL package. Ensure that you obtain the correct device!

The hardware and software circuits have been presented here as a simple illustrative case study to integrate many of the techniques described in the body of the text. If you decide to build your own version,files, C coding, PCB, comparison with a Motorola 68000 MPU version and other ideas

5With 772 instructions left unused, the PIC18F83 with a Program store of 512 words could be used as the target process with a small reduction in cost.

6The exception being the obsolete PIC16C83/4 which also has an EEPROM Program store.

16. A Case Study 473

Fig. 16.5 The Microchip PICSTART Plus programmer.

for experimentation, which you are welcome to contribute are given on the associated Web site detailed in the Introduction. Good luck!

474 The Quintessential PIC Microcontroller