RE: [AVR-Chat] Reading from flash in Assembly
2004-12-01 by Paul Curtis
John, Paul, > Hi Paul, > > This untried, but pretty sure that I would go for: > > //assume that we have 16 blocks of 256 in the array... > LDS r16,step //this one 0-255? > LDS r17,num //this one 0-15? > CLR r18 > LDI r30,LOW(array) > LDI r31,HIGH(array) > ADD r30,r16 > ADC r31,r17 > LPM r18,r30 > OUT PORTD,r18 > > /*...and can be two cycles quicker if you dedicate a > permanent pointer to 'step' and 'num' sequentially in SRAM: > LD r16,X > LDD r17,X+1 > */ Actually, it can be faster still. LDS r30,step //this one 0-255? LDS r31,num //this one 0-15? SUBI r30,LOW(-array) SBCI r31,HIGH(-array) LPM r18,r30 OUT PORTD,r18 Assuming you align your array on a 256-byte boundary, we can drop one instruction: LDS r30,step //this one 0-255? LDS r31,num //this one 0-15? SUBI r31,HIGH(-array) LPM r18,r30 OUT PORTD,r18 If, however, you need a true C solution, you can use something like: static const __code char *table[256] = (void *)0x4000; // or whatever your compiler required for telling it about a pointer to code space at 0x4000 Then you can write: char c = table[y][x]; -- Paul Curtis, Rowley Associates Ltd http://www.rowley.co.uk CrossWorks for MSP430, ARM, and (soon) Atmel AVR processors