File 6 Microlab Input Output
File 6 Microlab Input Output
In this lab , we will learn how to simulate the button on the MPLAB.
We will define 8 inputs : RB7, RB6, RB5, RB4, RB3, RB2, RB1, RB0
They form together PORTB. However, If I press any button of these the byte value
will change. ( RB) means B0, RB1 mean B1 etc… )
Ex2: If we press (once) RB2 and RB5 for example, PORTB will = 0010 0100 = 24h.
Now if we press again RB2 for example , RB2 becomes 0 and RB5 remains 1 and
PORTB will read: 0010 0000 = 20 h
Solution
1. The CONFI and INIT parts remain the same as before because they set the
direction of ports. So the change starts at MAIN.
MAIN MOVF PORTB,W ; I read PORTB into W like I read a memory file
MOVWF PORTD ; I Write the same byte I have just read onto PORTD
GOTO MAIN ; I keep repeating because I need to keep reading
END ; PORTB as a variable…why? Because if anyone
; presses any switch of PORTB , the program will
; read it again and send the updated value to PORTD
1
Microlab File 6 – by Joseph Massoud
2
Microlab File 6 – by Joseph Massoud
3. I need then to create the switches so I can change PORTB and see the program
working properly.
I press stop/Pause ( ).
I select in the menu : Debugger / Stimulus/ New workbook.
3
Microlab File 6 – by Joseph Massoud
4. Then I resize vertically and horizontally the new window so it fits under the
Watch and next to the program without any limits in common.
4
Microlab File 6 – by Joseph Massoud
5. I press in the small case under PIN/SFR ; then I open the scroll and I choose RB0
5
Microlab File 6 – by Joseph Massoud
7. Then I will press under RB0 and select RB1 ; then next to it Toggle.
I continue RB2 Toggle , RB3 Toggle, RB4 Toggle, RB5 Toggle, RB6 Toggle
RB7 Toggle; ( I should enter them in the right order)
6
Microlab File 6 – by Joseph Massoud
8. I press ANIMATE ; and the system is operating ; but the values are still 00.
I press on the arrow left to RB0 (under word Fire) while the system is running .
RB0 becomes 1 ; and I can see it in the WATCH ( PORTB).
Automatically PORTD will change also and D0 becomes 1 , because the program
is running.
Remark : During simulation: If someone presses any button on PORTB during the
animate , the respective output bit on PORTD will change because the program is a
blind reading from input and writing to output.
7
Microlab File 6 – by Joseph Massoud
9. If we press again at the same place next to RB0 (under fire); the switch will be off
again and RB0 becomes 0 again; PORTB becomes fully 00; and in less than a
second PORTD will becomes 00 also because the program reads from PORTB to
PORTD. If we press another time the switch will ON again and so on.
Remark (clearing all input byte) : If for instance we pressed multiple buttons , so the
value of PORTB will follow . We consider (as example) it is now 0010 1101 .
If we want now to put all PORTB byte to 00h again , I need to press individually
each bit which is 1 ; in this example RB0 , RB2, RB3 and RB5 are 1 ; if I press on
them they will turn to 0 and all byte becomes 00 again .
Remark: When closing the file, it may ask you if you want to save the stimulus
window; You may press NO ; because you will create it again later
8
Microlab File 6 – by Joseph Massoud
Write a program LAB62.asm which reads input byte (PORTB) and write the
complement value on output PORTD.
Solution
10. The CONFI and INIT parts remain the same as before because they set the
direction of ports. So the change starts at MAIN.
11. MAIN MOVF PORTB,W ; read PORTB into W like I read a memory file
MOVWF TEMP1 ; save the value in temp1 because I want to use COMF
COMF TEMP1,W ;complement the value I read from PORTB into W
MOVWF PORTD ; Write the complemented value on PORTD
GOTO MAIN ; I keep repeating same operation keep reading
END
12. Write the program; QUICKBUILD- Activate simulator- Create the WATCH and
STIMULUS ; define RB0 till RB7 on the stimulus. ANIMATE.
Now during the operation , try to press on Fire of some of the inputs; we will see
that PORTB change accordingly and PORTD will have the complement value.
9
Microlab File 6 – by Joseph Massoud
C- Assignment 12
13. Write a program LAB63.ASM which keeps reading PORTB and then ADD it to
itself , so we can have the double of this input value. Write the result on PORTD.
Try it using stimulus and watch.
Remark: To verify better your results you may look at the values in the decimal
column and that PORTD is always double of PORTB
Remark: during your tests try NOT to put RB7 to 1, so the input value will remain
below 128 and its double below 255
D- Assignment 13
It means reading input PORTB and adding with constant 15 D then subtracting from
constant 12H
E- Assignment 14
It means reading input PORTB and add with the content of memory 0x23 and content
of memory 0x26 , then subtract from constant 13 decimal.
Try it using stimulus and watch.( try to change value of memories to take many cases)
GOOD LUCK
10