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

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

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

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

Добавлен: 15.06.2025

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

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

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

THE ESSENCE OF THE PIC MICROCONTROLLER 131

Program 5.9 Binary to 2-digit BCD conversion.

STATUS

equ

3

; Status

register

is File 3

NB

equ

0

; Carry/Not Borrow

flag is bit0

BINARY

equ

20h

; Binary

byte is in File 20h

TENS

equ

21h

;

The

quotient is

put here

UNITS

equ

22h

;

The

remainder is

put here

; First divide by ten BIN_2_BCD clrf TENS

movf BINARY,w

LOOP

incf

TENS,f

addlw

-d’10’

btfsc

STATUS,NB

goto

LOOP

decf

TENS,f

addlw

d’10’

movwf

UNITS

..... ......

;Zero the loop count

;Get binary byte into W

;Record one ten subtracted

;Subtract decimal ten

;IF a borrow (NB==0) THEN exit loop

;ELSE do another subtract/count

;Compensate for one inc too many

;Add ten to residue

;which gives the remainder

;Next routine

This gives the task list:

1.Clear AVERAGE.

2.Point to Temp[0] (i = 0).

3.DO

(a)Add Temp[i] to the 2-byte grand total.

(b)Increment i.

(c)Repeat WHILE i < 24.

4.Divide by 24.

5.End.

Program 5.10 directly implements the task list, summing each datum byte by adding to the double-byte location File 48:9h, which has been cleared before entry to the loop. Division is accomplished by repetitively subtracting 24 from the final total. This is similar to the ÷10 routine of Program 5.9 but this time the single-byte constant is taken o the doublebyte dividend. The number of successful subtracts is the quotient, which in this case is the truncated average. Of course it would be more accurate to round up if the remainder was more than half of the divisor.

Example 5.5

Write a routine to multiply a byte in File 22h by ten. The 2-byte prod-

uct is to be located at File 23:4h giving OVERFLOW F 21h MULTIPLICAND F 22h ×

10 = PRODUCT_H F 23h PRODUCT_L File 24h where File 21h is used to extend the

single-byte multiplicand to a 16-bit double-byte datum.

Solution


132 The Quintessential PIC Microcontroller

The task list implemented in Program 5.11 splits the ×10 task into a ×8 + ×2 operation. Thus:

1.Multiply multiplicand by eight (shift left three times).

2.Multiply multiplicand by two (shift left once).

3.Add the two 16-bit partial products.

4.End.

Program 5.10 Average daily temperature.

INDF

equ

0

; INDirect File register

STATUS

equ

3

; Status register is File 3

FSR

equ

4

; File Status Register

TEMP_0

equ

30h

; Array starts @ File 30h

SUM

equ

48h

; Grand total to be in File 48:9h

AVERAGE

equ

4Ah

; Average byte is to be here

Z

equ

2

; Zero flag is bit2 of STATUS

C

equ

0

; Carry flag is bit0

NB

equ

0

; The alternative name is Not Borrow

; Task1: Clear grand total

AV_DAILY

clrf

SUM

; MSbyte sum zeroed

clrf

SUM+1

; LSbyte sum zeroed

; Task2: Point to Temp[0]

movlw

TEMP_0

; Put address of first temp byte

movwf

FSR

; in the pointer register

;Task3: DO

;Task3A: Add Temp[i] to the double-byte grand sum

LOOP1

movf

INDF,w

; Get Temp[i]

addwf

SUM+1,f

; Add LSB sum to it and put away

btfsc

STATUS,C

; IF no carry, don’t increment MSB

incf

SUM,f

; ELSE pass carry on

; Task3B: Increment i

NEXT

incf

FSR,f

; i++

; Task3C: REPEAT WHILE i <

24

movf

FSR,w

; Get pointer address

sublw

TEMP_0+18h

; Take away end address (Temp[24])

btfss

STATUS,Z

; IF equal THEN end

goto

LOOP1

; ELSE repeat

; Task4: Divide

by 24 to give the average

clrf

AVERAGE

; Zero the average

LOOP2

movlw

d’24’

; Put the constant 24 in W

incf

AVERAGE,f

; Record one subtract 24

subwf

SUM+1,f

; Take away 24 from the sum LSB

btfsc

STATUS,NB

; IF borrow out, goto high byte

goto

LOOP2

; ELSE do next subtract

movlw

1

; Subtract one from high byte

subwf

SUM,f

btfsc

STATUS,NB

; IF a borrow (NB==0) THEN exit loop

goto

LOOP2

; ELSE do another subtract/count

decf

AVERAGE,f

; Compensate for one inc too many

.....

......

; Next routine


THE ESSENCE OF THE PIC MICROCONTROLLER 133

Program 5.11 multiplication by ten.

STATUS

equ

3

; Status register is File 3

OVERFLOW

equ

21h

; Overflow to the multiplicand

MULTIPLICAND

equ

22h

; Multiplicand byte

PRODUCT_H

equ

23h

; High byte of product

PRODUCT_L

equ

24h

; Low byte of product

C

equ

0

; Carry flag is bit0

; Task1:

Multiply multiplicand

by eight

MUL_10

movf

MULTIPLICAND,w

; Get Xcand byte

movwf

PRODUCT_L

; giving the lower product byte

clrf

PRODUCT_H

; extended to 16 bits

bcf

STATUS,C

; Clear Carry

rlf

PRODUCT_L,f

; Now shift word left

rlf

PRODUCT_H,f

; three times

rlf

PRODUCT_L,f

rlf

PRODUCT_H,f

rlf

PRODUCT_L,f

rlf

PRODUCT_H,f

; Note Carry out here is zero

; Task2: Multiply multiplicand

by two

clrf

OVERFLOW

; Extend Xcand to 16 bits

rlf

MULTIPLICAND,f

rlf

OVERFLOW,f

; Task3: Add X8

and X2

movf

MULTIPLICAND,w

; Get LSB of X2

addwf

PRODUCT_L,f

; Add to LSB of X8

btfsc

STATUS,C

; Skip if no carry

incf

OVERFLOW,f

; ELSE add one onto MSByte

movf

OVERFLOW,w

; Get MSB of X2

addwf

PRODUCT_H,f

; Add to MSB of X8

.....

......

; Next routine

The coding copies the 1-byte multiplicand into the lower byte of the product to be. Clearing the upper byte extends the datum to 16 bits. Clearing the Carry flag and then shifting left three times gives the ×8 subproduct. As the upper byte is initially clear then the Carry flag is always zero after each double-byte shift, as it is going into the second ×2 shift routine. This is done on the extended multiplicand memory space and the resulting subproduct added to first datum. This addition is simplified as the shifted upper byte of the ×2 subproduct is small and so any carry from the lower byte addition is accounted for by simply incrementing the upper byte of this datum before the upper bytes are added. There will be no carry out from this latter addition.


134 The Quintessential PIC Microcontroller

Self-assessment questions

5.1 Can you deduce what function the following code fragment performs on the data byte in the Working register?

addwf FILE,w subwf FILE,w

5.2How could you extend Example 5.3 to give an outcome as packed BCD in File 21h? Hint: Consider making use of the swapf instruction.

5.3Develop Example 5.3 to give a 3-digit BCD outcome; removing the restriction that the original binary byte should be limited to decimal 99. The outcome is to be in File 21:2:3h.

5.4Extend Example 5.1 to decrement a quad-precision 32-bit word located at File 26:7:8:9h, most significant byte first.

5.5Example 5.4 evaluated the average of an array of hourly temperature samples by summing all bytes and then subtracting 24 until the residue dropped below zero. Write an extension to this program to round the average to the nearest integer; that is if the remainder is more than 12 then round up.

5.6Write a routine to multiply a byte in File 22h by 13. The 2-byte

product is to be located at File 23:4h. The memory map for this

is OVERFLOW F 21h MULTIPLICAND F 22h × 13 = PROD_H F 23h PROD_L F 24h

where File 21h is used to extend the single-byte multiplicand to a 16bit double byte datum. Note that this will require three shift and add processes.

5.7 A simple digital low-pass filter can be implemented using the algorithm:

Array[i] = S4n + Sn2−1 + Sn4−2

where Sn is the nth sample from an eight-bit analog to digital converter located at Port B.

Write a routine assuming that the three byte memory locations to store Sn, Sn−1 and Sn−2 are located at File 20:1:2h respectively. The outcome Array[i] is to be located at File 48h.

5.8 A certain television show has eight contestants which are evenly divided into Team A and Team B. Each member has a switch, giving logic 1 when pressed, which may all be read simultaneously by the

THE ESSENCE OF THE PIC MICROCONTROLLER 135

microcontroller at Port B. Team A switches appear on the lower four bits of the port.

Write a routine that will:

Decide when a response to the question has been made – any switch closed.

Determine the team identity that has responded, by clearing File 20h for Team A and setting it to any non-zero value to signify Team B.

Ascertain which team member pressed his or her switch by putting the member number 0–3 in File 21h.