Using Memory Space Efficiently: PIC Tutorial 6

Up till now, we have composed the PIC blink an LED on and off.
Subsequently we were capable of
with our PIC by including a switch, therefore varying the flash speed.

PIC Tutorial - 5..................................PIC Tutorial - 7
 

The sole
issue is, the program is quite lengthy and rather inefficient of memory space. It seemed ok
while i was including the commands for the first time, however there ought to be an easier way of
executing it. Positively there is, we will analyze how we were literally switching the LED on and off.

movlw          02h
movwf          PORTA
movlw          00h
movlw PORTA
At first we stuffed our w register with 02h, after that
transfered it to our PortA register to switch the
LED on. To switch it off, we packed w with 00h after which shifted it to our
PortA register. Inbetween all these routines we were forced to get in touch with a subroutine to
ensure that we could observe the LED
flashing. Therefore, we needed to transfer two sets of info a couple of times (one
time into the w register then to
PORTA) as well as call a subroutine two times (once for on then once for off).
Thus, how could we achieve this with added efficiency? Very simple. We utilize
a different instruction known as
XORF.
The XORF instruction works an Exclusive OR function on the register that we
stipulate with the info we provide. I believe I have to clarify what in the world an
Exclusive OR is before we continue.
In case we have two inputs, and one output, the input can only be a 1 if, and
as long as, the two inputs differ. While they are the same, then the output will probably be 0. The
following is a truth table, for individuals who choose to check out these:
A          B            F
0           0            0
0           1            1
1           0            1
1           1            0
We will at this point check out what goes on if we render B
the just like our earlier output, and simply
altering the value of A:
A           B          F
0            0          0
0            0          0
1            0          1
1            1          0
1            0          1

If we maintain the value of A same as 1, and we Exclusive OR it with the
output, the output would toggle. In case you can’t notice this from the truth table, below it can
be witnessed utilizing binary:

                        0       Current Output
EX-OR With 1 1       New Output
EX-OR With 1 0       New Output

Maybe you can find that by exlusive ORing the output with 1, we will be now
toglling the output from 0 to 1 to 0.
Hence, to switch our LED on and off, we only require a couple of sentences:
MOVLW     02h
XORWF      PORTA,1
What precisely we will be accomplishing is adding our w register with 02h. We
are in that case Exclusive ORing this number with no matter what is on our PortA. In case bit 1 is a 1, it is going
to alter to a 0. In case bit 1 is a 0, it is going to alter to a 1.
Let’s examine this code once or twice, to display how it's running binary:
                      PORTA
                      00010
xorwf               00000
xorwf               00010
xorwf               00000
xorwf               00010

We don’t actually have to load the identical value into our w register every
time, therefore it is possible to accomplish this one time at the start, and simply leap back to our toggle command.
Additionally, we don’t
ought to fix a value on our PortA register. The reason? Surely, since in case
on power up it is a 1, we can easily toggle it. I, alternatively a 0 on power up, we would even now
toggle it.

Therefore you would want to see our newly formed code. The
first one represents our blinking LED code, while the second shows the one with the addition of the switch:

Lets wish you can find that simply by making use of one easy
instruction, we now have cut down the scale of
our program. The truth is, in order to display just how much we could reduce
our programs by, We have demonstrated the two programs, just what were composed, and their dimensions in
the table below:
Program               Alter                           Dimensions (Bytes)
Flashing LED         Original                             120
Flashing LED         Subroutine Added          103
Flashing LED         XOR Function Used       91
LED With Switch   Original                           132
LED With Switch   XOR Function Used       124.

Therefore, not just have we discovered a few novel instructions, we certainly
in addition have decreased the size of our scripting!



Leave a Comment