piclist 2004\06\11\035732a >
Thread: Look up Table
www.piclist.com/techref/mems.htm?key=table
flavicon
face BY : David VanHorn email (remove spam text)



At 07:32 PM 6/11/2004 +1200, Hopkins wrote:

>How do you program a lookup table in an AVR (ATMega 8) using assembler code?

Assuming two regs named TEMP, TEMP2, and a ram var called Table_Offset with the pointer to the position in the table:

Get_Table_Byte:
       ldi     TEMP,0                  ;For the 16 bit add to follow
       lds     TEMP2,Table_Offset      ;Get the offset
       ldi     ZL,low(Table*2)         ;Point at the table
       ldi     ZH,high(Table*2)        ;
       add     ZL,TEMP2                ;Add the offset as a 16 bit op
       adc     ZH,TEMP                 ;so the table can be anywhere.
       lpm     ;Defaults to R0, some AVRs can put it in other registers.

       ret     ;Byte (Table offset) of TABLE is now in R0

For reading multiple bytes, continue with

adiw    ZL,1
lpm

and so on.

Data is stored in WORDS in the AVR rom, but recalled in BYTES.
Also, the assembler will append an extra null to the end of a .db that is an odd number of bytes long.

TABLE: .db 0x45,0x46,0x47

Ends up in rom as 45,46,47,00 << Extra null added by assembler.

A table that ends in a null, like:

TABLE: .db 0x45,0x46,0x43,0x47,0x00

ends up looking like this: 45,46,43,47,00,00

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

<5.1.1.6.2.20040611024745.01e51000@mail.cedar.net>

In reply to: <000a01c44f86$36291310$0000fea9@Roy>
See also: www.piclist.com/techref/mems.htm?key=table
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Look up Table

month overview.

new search...