Data
Tables
There is
a nice
feature
in the
instruction
set that
allows
you to
use a
data
table.
A data
table is
simply a
list of
data
values,
where
each one
is read
depending
on some
criteria.
For
example,
you
might
have a
circuit
that
uses a
PIC
where it
counts
the
number
of times
an input
pin goes
high in
1
second.
You can
then
display
the
number
on a 7
segment
display.
Once the
timing
has
started,
the PIC
counts
the
number
of times
the pin
goes
high.
After 1
second
it goes
to the
table
and
looks up
the
information
it needs
to
display
the
number
on the
display
that
corresponds
to the
number
of times
the pin
went
high.
This is
useful,
because
we don’t
know
what the
number
will be
until
the PIC
has
completed
its
count.
By using
a table,
we can
let the
PIC
decide
which
number
to
display.
Now,
before
we
carry on
to
explain
how the
data
table
works,
We
have to
explain
how the PIC
keeps
track of
whereabouts
in the
program
it is
when the
program
is
running.
It helps
if you
have
done
some
programming
in
BASIC.
If not,
don’t
worry,
you
should
still be
able to
see the
concept.
Imagine
we have
a BASIC
program
like the
one
shown
below:
10
LET K=0
11
K=K+1
12
IF K>10
THEN
GOTO 20
ELSE
GOTO 11
20 PRINT
K
21 END
The
program
starts
at line
10.
Once K
is set
to 0, it
then
proceeds
to line
11.
After we
have
added 1
to K we
then
move on
to line
12.
Here we
are
asking
if K is
greater
than
10. If
it is,
then we
go to
line 20,
if not
we go
back to
line
11.
Line 20
prints
the
value of
K, and
line 21
ends the
program.
BASIC
uses
line
numbers
to help
the
programmer
keep
track of
where
things
are, as
labels
are not
allowed.
The PIC
uses
labels
to jump
between
locations
– or
does
it? We
use the
labels
so that
we know
where
things
are, and
also so
that we
can tell
the PIC
in an
easy way
where to
go.
What
actually
happens
is the
PIC uses
an
internal
line
counter
called a
Program
Counter.
The
Program
Counter
(abbreviated
to PC)
keeps
track of
the
memory
location
of where
the
current
instruction
is.
When we
tell the
PIC to
go to a
particular
label,
it know
the
memory
location
and
hence
increase
the PC
until it
reads
that
memory
location.
This is
exactly
the same
way as
we read
the
BASIC
program
above.
Below is
a
section
of code,
with the
memory
locations,
or the
contents
of the
PC, next
to each
instruction:
PC
Instruction
0000 movlw
03
0001 movwf
0C
0002
Loop
decfsc
0C
0003 goto
Loop
0004
end
In the
example
above,
We
have set
the PC
to
0000.
At this
location
we have
the
instruction
movlw
03.
When the
PIC has
executed
this
instruction,
it
increments
the PC
so that
the next
instruction
is
read.
Here the
PIC sees
movwf
0C. The
PC is
incremented
again.
Now the
PIC
reads
decfsc
0C. If
the
contents
of 0C
are not
0, then
the PC
is
incremented
by 1,
and the
next
instruction,
goto
Loop,
tells
the PC
to go
back to
location
0003,
which is
where we
have
said
Loop.
If the
contents
of 0C is
0, then
the PC
is told
to
increment
by 2, in
other
words
skip the
next
instruction.
This
puts the
PC at
location
0004,
where
the
program
ends.
The
locations
are set
by the
assembler,
and we
don’t
normally
need to
worry
what the
PC is
doing.
Until,
that is
we need
to
control
it like
we are
about to
do when
using
data
tables.
The best
way to
explain
how a
data
table
works,
is to
start
off with
an
example.
PC equ
02
movlw
03
call
table
:
table
addwf
PC
retlw
01
retlw
02
retlw
03
retlw
04
retlw
05
retlw
06
retlw
07
return
The
first
instruction
is
assigning
the
label PC
with the
address
of the
Program
Counter
(02h).
We are
then
placing
the
value of
03h into
the w
register.
We then
make a
call to
table.
The
first
line in
the
subroutine
table
adds the
contents
of the W
register
(03h) to
the
program
counter.
This
causes
the
program
counter
to
increase
by 3, or
to put
it
another
way,
causes
the
program
counter
to move
down 3
lines.
When the
counter
reaches
3 lines
down it
the PIC
sees the
instruction
retlw.
This
command
passes
the
value
following
it into
the W
register,
and then
returns
from the
subroutine.
RETLW
actually
means
Return,
Literal
to W.
Notice
we
put a
comma
after
the word
Return.
As we
are in a
subroutine,
we need
a Return
instruction
to come
out of
it.
Hence
the RET
in the
instruction.
After
the RETLW
instruction
is a
number,
and this
is what
is
placed
in the W
register.
In this
case it
is the
number
3.
We can
assign
any
number
to the W
register,
as long
as when
this
number
is added
to the
Program
Counter
in the
table
subroutine,
we will
find a
retlw
instruction.
In the
above
example
this
means we
can have
any
number
from 1
to 7.
If we go
past the
subroutine,
we could
end up
executing
another
part of
the
program.
Because
of this,
it is
always a
good
idea to
put the
data
table
right at
the end
of the
PIC
program,
so if we
do
overshoot
then we
will
reach
the end
of the
program
anyway.
Click
here >>>>
Tutorial
11
|