Sei sulla pagina 1di 5

Embedded Systems Laboratory Experiment 3 Program Writing

Eng. Karam AbuGhalieh Princess Sumaya University for Technology 2012 0

Experiment 3

Program Writing
Objectives
This experiment aims to get you familiar with: 1. Using Instruction in programs. 2. Problem solving. 3. More simulation techniques.

Introduction
Now after you got familiar with PIC instructions, we will use these instructions to build useful and meaningful programs. Such programs aim to solve certain problem involving conditions and subroutines usage. We will also get in more simulation details.

Requirements
MPLAB, Instructions set and you are very encouraged to review them

Pre-lab
Write a program that checks the value of PORTA, if it is greater than 16 write 240 on PORTB, if it is smaller than 16 write 15 to PORTB and if it equal 16 write 255 to PORTB.

Procedure
Part 1: Check if a value is greater or smaller than 10
include "p16F84A.inc" cblock 0x25 testNum Result endc org 0x00 Main movf testNum, W sublw .10 ;10d - testNum btfss STATUS, C

goto Greater ;C = 0, that's B = 1, then testNum > 10 goto Smaller ;C = 1, that's B = 0, then testNum < 10 Greater movlw A'G' movwf Result goto Finish Smaller movlw A'S' movwf Result Finish nop end

1. Start a new MPLAB, create new project, and add the above code. 2. Build the project 3. Select Debugger Select Tool MPLAB SIM 4. Add the necessary variables and the working register to the watch window. 5. Enter values into testNum, simulate the program step by step. 6. Edit the code for the case where testNum= 10. 7. Check and analyze Status register for the three cases, testNum>10, testNum=10 and testNum<10. 8. Edit the code so it repeats the whole program forever. Part 2: Counting the number of ones in a byte
include "p16f84a.inc" cblock 0x20 testNum endc cblock 0x30 numOfOnes counter require a counter, one is declared endc org 0x00 clrf numOfOnes movlw 0x08 8, the number of the bits to be tested movwf counter Again rrf btfsc incf decfsz decremented, testNum, F STATUS, C numOfOnes, F counter, F

;since repetition structures

;counter is initialized by

; after each test the counter is

goto Again 0, it will skip to nop and program ends nop it will repeat goto Again end

; if the counter reaches ; if the counter is > 0,

1. Simulate the above code. 2. Rewrite the code using subroutine to replace the block in Again loop. 4. Compare the Program Memory of both codes, to view program memory go to View Program Memory,. What is the address of subroutine? 4. Simulate the code again and keep an eye on PC and Stack, to view stack go to View Hardware Stack . Before calling subroutine What is the address of PC? What is now stored at the TOS (Top of Stack)? How many levels of stack are used? While executing subroutine What is the address of PC? What is now stored at the TOS (Top of Stack)? (Refer to the Hardware Stack window) How many levels of stack are used? After the instruction return. What is the address of PC? What is now stored at the TOS? How many levels of stack are used?

To get the result of subroutine without stepping into each instruction use Step over in Debugging bar. 5. Calculate manually the expected time for executing the code supposing we are using 4 MHz oscillator. 6. Make the simulator calculating the time: -go to Debugger >settings, select OSc/Trace tab and set the Processor frequency to 4 MHz - Set breakpoints at the start and end of the code -go to debugger Stop watch -run the program and make the first point the zero point for the watch, reach the next breakpoint and get the time reading from the stop watch. Part 3: Demo Use mE Programmer to write the Demo code to the PIC, and test it on bread board 1. Start new project 3

2. Add the source code for PORTB Blinking, build the project 3. Open me Pic Flash from desktop, select PIC16f877 for target Device. 4. Load the created HEX file and press Write to write it to PIC. Make sure that you have connected Easy PIC kit to your PC. 5. Debug the output.

Potrebbero piacerti anche