You are on page 1of 5

Lab 2: Input/output

Objective: in this experiment, we will learn how to manipulate the input


and output of our microcontroller by solving the following exercice.

Question: Read continuously input values (as a byte) and write to the
outputs (LED)

a) Same value
b) Complement
c) Opposite (negate)
d) Double value
e) Half value
f) Half +5

In each part , I will explain the steps for solving in order to accomplish the
objective of our experiment.

Solution:

a) Same value:

Suppose I have entered the value 3 (base 10) as an input to the micro, then
I have to see the same value in port B

3(base 10) 0011(base 2)

In port A (input section) we have:


0000 0011

A7 A6 A5 A4 A3 A2 A1 A0
In port B (output section) we will have the same value

0000 0011

B7 B6 B5 B4 B3 B2 B1 B0

From these boxes we can see that when we apply the input 3 I will see 2
LED ON ( B0 and B1) .

Code:

{ MAIN MOVF PORTA,W

MOVWF PORTB

GOTO MAIN }

This program applies to any input I choose.

b) Complement

To explain this part, I will take as an example the easiest number which is 0.

If 0 is assigned as an input to port A, that means I will see all the LEDs ON
because the complement of 0 is simply 1.

0000 0000
Port A
A7 A6 A5 A4 A3 A2 A1 A0

Code: 1111 1111 Port B

B7 B6 B5 B4 B3 B2 B1 B0
{ MOVF PORTA,W

MOVWF TEMP

COMF TEMP,W

MOVWF PORTB }

c) opposite (negate):

The goal of this part is to see the opposite value of any input.

For instance, if the input is 3(base 10), I want to see in the output the
number -3.

To do so, we will use the first complement method.

Steps of this method:

0000 0011 Port A

A7 A6 A5 A4 A3 A2 A1 A0

First Complement

1111 1100

ADD +1

Code:
1111 1101 Port B
B7 B6 B5 B4 B3 B2 B1 B0
{MOVF PORTA,W

MOVWF TEMP

COMF TEMP,W

ADDLW H'01'

MOVWF PORTB }

From this flow chart we can see that this part is the same as b)
(complement) and the only difference that we add +1 to our code.

d) Double value + e) Half value + f) half +5 :

The double and half are very similar to each other , because the two of
them follows the same way which is the rotation method, with a small
difference between them.

Explanation of the Rotation method:

Let us take for example number 4(base 10). The double is 8 and the half is
2.

Rotate Right Always clear the carry


C (carry) = 0 before rotation
4(base 10) 000 0100(base 2)
Rotate Left

{The 1 is moved to the right and a 0 will come from the carry to A7}

2(base 10) 0000 0010(base 2) Rotate Right

4(base 10) 000 0100(base 2) {same but left dir}

8(base 10) Rotate Left


0000 1000(base 2)

From these figures we can deduce that d + e + f have the same code; only
the sense of rotation will be different. And for f) we only add +5 to our code

Code:

{ MOVF PORTA,W

MOVWF TEMP

BCF STATUS,C ( clearing the carry to 0 )

RRF TEMP,W ( for double use RLF )

ADDLW H'05' ( additional line for f) )

MOVWF PORTB }

Conclusion: the question is solved and the objective is done

You might also like