Thanks for the fast response - will test it out. ----- Original Message ----- From: "David VanHorn" To: Sent: Friday, June 11, 2004 7:58 PM Subject: Re: [AVR:]Look up Table > 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 --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.703 / Virus Database: 459 - Release Date: 10/06/2004 -- 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