The pic16F84 Microcontroller Part 3 Timer


Program 4.1 gives an efficient implementation of this task list


Download 305.41 Kb.
bet5/7
Sana15.06.2023
Hajmi305.41 Kb.
#1477874
1   2   3   4   5   6   7
Bog'liq
The PIC16F84 Microcontroller Part 3

Program 4.1 gives an efficient implementation of this task list. After incrementing using normal binary rules, six is added to the previous outcome and the DC flag checked for activity. This flag will only be set when the original nybble was ten (0Ah + 6 = 10h). In this case the add six operation is allowed to stand as the necessary correction otherwise it is cancelled by subtraction. The upper nybble (BCD digit) is checked and corrected in the same manner, but this time it is the full Carry flag that is tested. If this is set, then the addition of 60h is allowed to stand, otherwise it is subtracted. This Carry flag could be used to set a hundreds digit if desired, to show overflow from 99 to 100.
An alternative approach would be to subtract nine before incrementation and if the Z flag is set then leave the digit at zero and increment the higher digit; otherwise add ten. Repeat for the upper digit.

Example 4.4


Write a routine that will add two packed BCD numbers at File 20h and File 21 h. The outcome is to be in File 22h for the hundred’s digit and File 23h for the tens and unit digits.

Solution


As in the case of Example 4.3 the binary addition of numbers that are already in BCD form may need correction afterwards. Again, a correction will be needed where the digit outcome is greater than nine. The situation is more complex in this case as the sum of two digits can be anywhere between zero and 19 (9 + 9 + 1 (carry-in) = 19). The illegal range 10 to 19 may be determined by applying the criterion:
• Check for the range Ah to Fh (10 to 15) — for example, 3 + 9 = Ch (3 + 9 = 12).
• Otherwise check if there was a carry out to the next decade — for example, 9 + 9 = 1 2h (9 + 9 = 18).
In both cases the digit may be corrected by adding six to ‘jump over’ the six illegal BCD states.
Based on this course a possible task list is:
1. Add the two packed BCD bytes using normal binary arithmetic.
2. IF the addition results in a full Carry THEN Hundreds = 1
3. IF the addition results in a half Digit Carry OR IF the unit BCD nybble is greater than nine THEN add six to the outcome.
4. IF the last correction (if any) resulted in a full Carry OR IF the tens BCD nybble is greater than nine THEN add six to the upper nybble of the outcome.

Download 305.41 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling