1. Simulate the project using MPLAB SIM 2. Use logical and arithmetic instructions in program writing. 3. Record the changing of STATUS Register condition flag
Equipment list: 1. Personal computer (PC) 2. MPLAB software
Theory The flag register in the PIC is called the status register. In PIC18F, only five flags are used to indicate some conditions that result after an instruction is executed. These five flags are C (Carry), DC (Digital Carry), (Zero). X X X N OV Z DC C C Carry DC Digital Carry Z Zero OV Overflow N - Negative X Not implemented, reserved for future use C, the carry flag This flag is set whenever there is a carry out from the D7 bit. This flag bit is affected after an 8-bit addition or subtraction. DC, the digital carry If there is a carry from D3 to D4 during an ADD or SUB operation, this bit is set; otherwise it is cleared. Z, the zero flag The zero flag reflects the result of an arithmetic or logic operation if the result is zero, then Z=1. Therefore, Z=0 if the result is not zero.
Arithmetic Instruction Logic Instruction ADDLW Add Literal and WREG Status Affected: C, DC, Z
ADDWF Add W and F Status Affected: C, DC, Z
SUBLW Subtract W from Literal Status Affected: C, DC, Z
SUBWF Subtract W from F Status Affected: C, DC, Z
ANDLW AND Literal with WREG Status Affected: Z
ANDWF AND W with F Status Affected: Z
IORLW Inclusive OR Literal with W Status Affected: Z
IORWF Inclusive OR W with F Status Affected: Z
XORLW Exclusive OR Literal with W Status Affected: Z
XORWF Exclusive OR W with F Status Affected: Z
Precedure: Creating Project File 1. Create a New Project File. 2. Now open a new file by clicking File>New, and key in the program below: #include<p18F4550.inc> ORG 00H ; Operation 38h +2Fh MOVLW 38H ADDLW 2FH ; Operation 255 - 255 MOVLW D255 ADDLW D255 ; Operation 10000001 AND 11001100 MOVLW B10000001 ANDLW B11001100 END
3. Save this file using File>Save As. Select file type Assembly Source File, and save as lab2.asm 4. Right-click on the folder Source Files (Project window), and click Add Files to add lab2.asm 5. Assemble the program by pressing Project>Build All. (If the error occurs, fix it and repeat this step again)
Simulating the program using MPLAB simulator, MPSIM 6. Select Debugger > Select Tool > MPLAB SIM to simulate the program. 7. Open a Watch window by clicking View>Watch, and select the items as shown in Figure 2.1
Figure 2.1
8. Click Step Into to start the simulation and record the result into Table 2.1
9. Write an assembly program that can perform the following operations: a. 176 +128 b. 63 45 c. 11110011 OR 00000011 d. 11111100 ExOR 00101011 e. 2F AND 90 10. Simulate your code again and record the result into Table 2.2
Result : Operation Instruction WREG STATUS a) 38 h +2F h MOVLW 38H
W=
ADDLW 2FH W= C = DC = Z = b) 255 d -255 d MOVLW D255