Full Version: Help in lookup table
kekkon
Jul 11 2009, 2:17 AM
Hi, i am currently doing a project using lookup table which i am not really familiar with.

The 8051 reads through Port 1 where higher nibbles stand for 1 sets of inputs and the lower nibbles stand for another set,which is total of 2 sets of inputs after every 60seconds.

The question now I am facing is how to use the lookup table to read the 2 different sets of input to compare them whether they are equal, smaller or greater to each other.

Thanks for helping:-)

Ajay Bhargav
Jul 11 2009, 1:54 PM
well I understand what you need but i am not able to understand how lookup tables are involved in this.

you said u want to compare two nibbles of P1 so from where this lookup table comes?

or gimme better explanation with example

kekkon
Jul 14 2009, 1:23 PM

The lookup table is register like that store the inputs from P1 nibbles.

Example: first input is 0100 and the second is 0110 then the input data is move to the lookup table.

Then the data in the lookup table is being compare to determine whether they are bigger,equal or smaller.

Thanks :-)

ExperimenterUK
Jul 14 2009, 2:19 PM
The question is why use a lookup table to compare values

You would use the micro's comparison instructions to do that.

A lookup table is normally fixed, pre-set data, used to swap one value
for a different one.
kekkon
Jul 14 2009, 3:18 PM
Ya, I know. But this is a question given by my lecturer, i have to do what he told me to do :-|
afilash
Jul 14 2009, 3:54 PM
i agree with ExperimenterUK
just
that's it!!!
look up table may not be fixed, they can be set up inside ram location , for this case we don't need either

If this is a question given by your lecture and we are helping you ? then what's the point
without trying how you can learn something ? refer Mazidi for implementation of lookup tables :bye
ExperimenterUK
Jul 14 2009, 4:08 PM
Since this is a pointless exercise it is hard to know what
a good method is
However you could..
Fill an array (table) of 16 bytes with 0s
Get the first nibble, use it as an offset into the table,store 1 at that entry.

To compare,
Use the second nibble as an offset into the table and get the byte at that entry.
If the byte is 0 the second nibble is lower, if 1, it is the same
and if it is 2, it is higher.
(and I get the marks )

Generally I agree with afilash's comment about doing this yourself, but I think
a small pointer is OK just this once.
Ajay Bhargav
Jul 15 2009, 8:19 AM
are you using C or assembly?

in assembly lookup table is defined in CODE memory as follows:

```LookUp: ;this is a lable
DB: 03H,05H
```

i stored 03H and 05H in code memory using directive DB (define byte).
to use these values or to read them from lookup table we use MOVC instruction as shown below..

```CLR A ;clear A
;remember its good to have A as 0 before reading from lookup table and increment DPTR for next value
```
Ajay Bhargav
Jul 15 2009, 8:20 AM
[Topic moved to 8051 Discussion forum]
kekkon
Jul 15 2009, 11:31 PM
Thanks for all your help in this topic. :-)

I am using assembly language. (FX8031 kits). :-)

```cseg
Start:  CLR     P1.0
CLR     P0.0
CLR     A
MOV     TMOD,#11H
MOV     TCON,#01H
MOV     IE,#082H

MOV     DPTR,#LookUp
MOVC    A,@A+DPTR
MOV     R2,A
INC     DPTR
MOVC    A,@A+DPTR

SUBB    A,R2
JC      BIGGER
JNC     SMALLER
SJMP    EQUAL

CALL    DELAY_20MS
CLR     P0.0

ENDS
END
```

This is the code that I've tried to write. Is the method I am using correct ?:-)
ExperimenterUK
Jul 16 2009, 6:00 PM

This is the code that I've tried to write. Is the method I am using correct ?:-)

kekkon

What does it do ?
afilash
Jul 17 2009, 5:20 PM
Ok timeout!
previously i was thinking of helping you with just hints
but you are making things complicated
so i will frame a question and try to give its answer

question:
you will receive something (some data) on Port P1
the data you received will contain two separate portions
1. one on lower nibble
2. another on upper nibble

each of these nibble values corresponds to a data stored in rom location
say 0x02A0
lets assume the data values to be as follows

0x02A0 0000 30
0x02A1 0001 45
0x02A2 0010 23
0x02A3 0011 62
0x02A4 0100 31
0x02A5 0101 31
0x02A6 0110 73
0x02A7 0111 82
0x02A8 1000 36
0x02A9 1001 89
0x02AA 1010 42
0x02AB 1011 74
0x02AC 1100 41
0x02AE 1110 57
0x02AF 1111 97

what you have to do is
get the values corresponding to the nibbles and
calculate the difference
and show the result?

```BUS 	EQU	P1

ORG 	0000H
REBOOT:	ACALL APPLICATION
SJMP 	\$

APPLICATION:
MOV	BUS, #0FFH	;MAKE P1 AS INPUT PORT
MOV	A,P1		; GET THE INPUT		ASSUME IT TO BE 24

MOV	B,A		; SAVE IT FOR FUTURE PROCESS
ANL  	A,#0FH		; MASK LOWER NIBBLE		GET	2
MOV	R0,A		; SAVE IT IN R0
MOV	A,B		; RETRIVE SAVED INPUT
SWAP 	A		; SWAP IT TO OBTAIN HIGHER NIBBLE IN A[3..0]
ANL  	A,#0FH		; MASK HIGHER NIBBLE		GET	4

MOV 	DPTR, #DAT	; TABLE LOCATION
MOVC 	A, @A+DPTR	; GET THE CONTENT 	WITH A AS 4 WHICH IS 31H
MOV	B,A		; SAVE IT

MOV	A,R0		; GET 2
MOVC 	A, @A+DPTR	; WILL RESULT IN 23H

CJNE 	A,B,NOT_EQUAL	; WILL NOT BE EQUAL SINCE A<B
EQUAL:	;CODE FOR EQUAL CONDITION
;
;
RET

NOT_EQUAL:
JC	A_ISSMALLER
A_ISGREATER:
; CODE FOR A GREATER
;
;
RET

A_ISSMALLER:
; CODE FOR A SMALLER
;
;
RET

org 02A0h
dat: DB 30H,45H,23H,62H,31H,31H,73H,82H
DB	36H,89H,42H,74H,41H,39H,57H,97H

END

```

Please understand that i am 'NOT' writing code for you :-|
if this help you in any way, i will be more than happy
If you have any doubts please revert!
kekkon
Jul 18 2009, 2:23 AM
afilash
Jul 18 2009, 3:17 AM
:-s
kekkon
Jul 18 2009, 5:56 AM
Can anyone help me :-)
johnson_tee
Jul 18 2009, 6:34 AM
afilash,

0x02A0 0000 30
0x02A1 0001 45
0x02A2 0010 23
0x02A3 0011 62
0x02A4 0100 31
0x02A5 0101 31
0x02A6 0110 73
0x02A7 0111 82
0x02A8 1000 36
0x02A9 1001 89
0x02AA 1010 42
0x02AB 1011 74
0x02AC 1100 41
0x02AE 1110 57
0x02AF 1111 97

is for what use ？？
afilash
Jul 18 2009, 12:26 PM

its just an example
the thing is that for those input nibble values we have read through port P1, each nibble value corresponding to some value(say probability density function of some function F(x) !dance ) is given in the table inside the code space.

I was joking, don't get confused, they are just some values in the rom location
-or-
if you are not satisfied with this explanation
consider those location contains , say ascii values for those nibbles :-|
johnson_tee
Jul 18 2009, 12:46 PM

dat: DB 30H,45H,23H,62H,31H,31H,73H,82H
DB 36H,89H,42H,74H,41H,39H,57H,97H

???
Ajay Bhargav
Jul 18 2009, 12:51 PM
kekkon, afilash has given a good example to explain how to use loopup tables and comparing.

I think your Lab work need a revision. I think its not an appropriate way to learn using lookup tables. I recommend you to talk to your lecturer and get some advice from him or get an example from him on how things are need to be done exactly.

coz lookup tables are pre-stored values in code memory, and as you said you want to store the read values in lookup table, that is not possible. coz the name says.. look up is just for lookup purpose, you just read from it for reference not write into it.
kekkon
Jul 19 2009, 12:18 AM
I did not see the page 2 button.

A deep apologize for my stupid replied above. :-|
afilash
Jul 19 2009, 12:47 PM
@johnson_tee,
it will be more clear if you do it yourself
just compile the program in your favorite development environment.
just goto the .lst file you have got and look @ address 0x02A0 to 0x02AF
you will understand.
or just simulate the program and see what happens :-|
Ajay Bhargav
Jul 19 2009, 4:28 PM
kekkon, not a problem happy that now you know how to use the forum.. and what these different buttons do

Please take a close look at things and try to find your path into it. take afilash's code as reference and try to write your own according to what your lecturer said.
kekkon
Jul 25 2009, 8:52 AM
Ya, thanks for helping :-)

Been busy recently and have put this assignment aside, now I am going to try for the code that Afilash give.
kekkon
Jul 28 2009, 11:59 AM
some update from my assignment.

I have abandon the lookup table and just use some simple register to complete my assignment. :-)
ExperimenterUK
Jul 28 2009, 4:22 PM
I thought you *had* to use a look-up table.