You are on page 1of 10

Microlab File 6 – by Joseph Massoud

Handling Input Output on the simulator


A- Program 1 : same value

PORTB has been configured as input PORT (using TRISB)


On our board 8 buttons are connected to PORTB.
PORTB like PORTD is a file (register) like a memory location.
It is however part of the I/O interface; which means , like PORTD, it is a register for
the microprocessor but have a physical side to get voltage 0 or 5
( Review Introduction in class about I/O interface : DataA and DataB); here PORTB
is equivalent to DataA of the introduction.

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… )

Ex: If by default all inputs reads 0, so the PORTB = 0000 0000 = 00 h


If we press the first time the switch RB2 during the animate of the program, RB2 bit
becomes 1 and the PORTB as a byte will become: 0000 0100 = 04 h
If we press again (release) the same switch RB2 , this bit will get 0 again and the
PORTB = 00h again.

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

The first program is:


We suppose having 8 switches on PORTB and 8 leds on PORTD .
Write a program LAB61.asm which reads input byte (PORTB) and write the
same value we read on the output PORT which is PORTD.

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. We write the program, and open Watch with W, PORTB, PORTD .


We animate and the PORTD remains 00 since PORTB=00.
Remark: it doest work when I try to modify the value of input port (PORTB).

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

6. Then I press under action and I choose Toggle.

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 : If we want to change something in the program , we need to stop simulation


using the pause button; we then change the code and QUICKBUILD , and then
press animate 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

B- Program 2 : complement value

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

14. Write a program LAB64.ASM which executes the following:

(PORTB) + D’23’ - H ‘12’ …….on PORTD

It means reading input PORTB and adding with constant 15 D then subtracting from
constant 12H

Try it using stimulus and watch.

E- Assignment 14

15. Write a program LAB65.ASM which executes the following:

(PORTB) + (0x23) + (0x26) - D’13’ …….on PORTD

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)

REMINDER: to change content of memory we should be in STOP mode; to


change value of PORTB ( FIRE ) we should be in ANIMATE mode.

GOOD LUCK

10

You might also like