The pic16F84 Microcontroller Part 3 Timer
The solution lies in the method of subtraction by 2′s complementing the subtrahend
Download 305.41 Kb.
|
The PIC16F84 Microcontroller Part 3
- Bu sahifa navigatsiya:
- Example 4.3
The solution lies in the method of subtraction by 2′s complementing the subtrahend. The subtract instructions convert the subtrahend to their 2′s complement form and then configure the ALU to add. After the addition there can be two outcomes, depending on the relative magnitude of the minuend and subtrahend.
1. Where the subtrahend is greater than the minuend then the outcome is negative and there is no carry-out. An example of this situation is: 06 – 0A ^ 00000110 + 11110110 = (0) 11111100 or – 4 (no carry). 2. Where the subtrahend is less than the minuend then the outcome is positive and there is a carry-out. An example of this situation is: 0A – 06 ^ 00001010 + 11111010 = (1) 00000100 or + 4 (carry). In both cases the Carry flag acts as an inverted borrow. This is in keeping with the RISC philosophy of the PIC family, to keep the processor ‘lean and mean’. In any case this non inversion means that subtraction can be implemented by adding negative data, eg. addlw -6. This is translated by the assembler to addlw 0FCh, where FCh is of course the 2′s complement of 6. Example 4.3Write a program to increment a packed BCD quantity located in Data memory at File 20h. SolutionTwo Binary-Coded Decimal (BCD) digits may be packed into a single byte to represent numbers up to 99. For example represents BCD 49. Incrementing a number stored in this hybrid decimal-binary form using the normal binary addition rules may give an incorrect result. For example 01001001 + 1 (49 + 1) gives 01001010 (4Ah) after addition, but should give 01010000 (50h). Similarly, 10011001 + 1 (99 + 1) gives 10011010 (9Ah) instead of 00000000 Carry 1 (100h). From these examples it can be seen that whenever any of the BCD decades equals ten after incrementation then it should be zeroed and one added to any higher decade. Based on this increment and add algorithm we can formulate the task list. 1. Increment the packed BCD byte using normal binary arithmetic. 2. IF the lower nybble of the outcome is ten then add six to the outcome. 3. IF the upper nybble of the outcome is ten then add six to it. Program 4.1 Incrementing a packed BCD byte. Download 305.41 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling