'Look up tables'
Another beginner question:
What is the theory behind a look up table. How does the program know
when it has the data it wants?
Auckland, New Zealand
> Another beginner question:
> What is the theory behind a look up table. How does the program
> know when it has the data it wants?
I'm going to assume you are a real beginner, don't be insulted if I
am not addressing your question on the right level.
Lookup tables work like this:
Say you want a simple lookup table to calculate squares of numbers.
Your program calculates the number it wants squared, places that
number in the W register, then calls a subroutine. The subroutine
adds W to the Program counter. This is the key to a lookup table and
the answer to your question. The position in the table calculated by
this operation is the location of the answer answer. An example:
x ; number to be squared
y ; result
Squares ; subroutine
ADDWF PCL,F ; add w to program counter
RETLW 0 ; 0^2 = 0 return
RETLW 1 ; 1^2 = 1 return
RETLW 4 ; 2^2 = 4 return
RETLW 9 ; 3^2 = 9 return
RETLW 10 ; 4^2 = .d16 return
ETC. ETC. ETC.
MOVF x, w ; recall x
CALL Squares ; call subroutine to lookup
MOVWF y ; y now contains X^2
incf x, f ; add 1 to x
goto Main ; do it forever
Does that help?
-- Lawrence Lile
"Nyquist was an optimist."
=> Median Filter Source Code
=> AutoCad blocks for electrical drafting
|In message <toastmaster.com>, Lawrence Lile writes: 14584634121425
>> What is the theory behind a look up table. How does the program
>> know when it has the data it wants?
Just one additional pointer on lookup tables. On a PIC16C84/16F84 beware if
they span a 256 byte boundary! I learned this the hard way on a project
For example, if we use the example from Lawrence below, and put addresses in
Squares ; subroutine
0x0fc ADDWF PCL,F ; add w to program counter
0x0fd RETLW 0 ; 0^2 = 0 return
0x0fe RETLW 1 ; 1^2 = 1 return
0x0ff RETLW 4 ; 2^2 = 4 return
0x100 RETLW 9 ; 3^2 = 9 return
0x101 RETLW 10 ; 4^2 = .d16 return
This code will work fine until W is set to 3, in which case it will branch to
address 0x100. However, the PCLATH bits aren't set correctly so the RETLW will
cause the PIC to go haywire (at least I think it is the PCLATH that is set
See Myke Predo's book on PIC programming for more information on how to have
lookup tables longer than 256 bytes.
To ensure that your lookup tables are in the correct page, just place them on
the first page of your program (say, right after the Int handler vector).
Mark Crosbie http://www.best.com/~mcrosbie
Hewlett-Packard MS 47 LA cup.hp.commcrosbie
19447 Pruneridge Avenue (408) 447-2308
Cupertino, CA 95014 (408) 447-6766 FAX
More... (looser matching)
- Last day of these posts
- In 1998
, 1999 only
- New search...