Where to store data and how to access?

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've got a bunch of 8-bit data of varable length (from one to 7 bytes in length) that I need to access byte-wise.

My intial plan was to make a table of start locations and point Y at it and then to get, say the 3rd item: I'd get Y+3, for the LOCATION and then subtract Y+4 from Y+3 to get the LENGTH. Then I'd point X at the start of the real data and then read in X+LOCATION until X+LOCATION+SIZE with a loop.

Is this the best way? I'm going to have probably 90 chunks of data probably averaging 5 bytes long. Should I place this in the flash (program memory), EEPROM or SDRAM? Why should I choose one over the other? This info won't be changing over time.

Thanks,
Jason

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i think u can use EEPROM.....u can store and retrieve upto 512 bytes of data...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi!
Flash is used to store a constants which you will use in your program, for example this may be a SIN table, table with the commands for any device, ASCII strings for LCD, constants for any corrections for example, and in more cases for LUT.
EEPROM also may serve this purposes, but It's ordinary used to store any adjusted values for your device, which the user can program by him self. New AVRs support writing to flash instead of EEPROM, but Its recomended to use if the user have a lot of data to store, for several tens of bytes EEPROM is more optional because It's byte addressable and have more endurance for Write/Erase cycles I THINK???.
RAM is used for your temporary data like received or transmited command, some result of any calculation and etc.....

For your case if this bytes are not varaible, so they are constantswrite them to flash and use LPM/ELPM instruction.

If they are changed every time your device is powered -> user current settings => Use RAM (LD/ST/LDS/STS instructions).

If they are not changed so much when you power your device, but they are user programmable => use EEPROM if they are not so much, or use FLASH if they are several KBytes for example (Write/Read byte to EEPROM or Write/Read a PAGE(s) to/from FLASH).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When you put DB tables in Flash it's important to remember that all your .DB lines must have EVEN numbers of bytes. If you do something like this:

.db 0x01,0x02,0x03,0x04,0x05

.. the assembler will add an extra ".db 0x00" to make an even number. This can mess up your pointer calculations if you forget.

With variable length data, I generally use another table of word pointers like so:

pointer_list:
	.dw	s_null		; 00
	.dw	s_pwr		; 01
	.dw	s_init		; 02
 ... more pointers

s_null:	.db	0,"                ",0
s_pwr:	.db	0,0x1F,0x28,0x67,0x40,0x01,0x01," VERSION 2.12"
	.db	0x0D,0x0A," ADDRESS ",0
s_init:	.db	0,0x1F,0x28,0x67,0x40,0x01,0x01," INITIALIZING",0,0
 ... more strings

.. so you can find your place in pointer_list with a simple fixed calculation, then pick up the address of the data you want with a routine like this:

; data string number (00..nn) in R16
	ldi	ZH,high(pointer_list*2)	; address of table of pointers
	ldi	ZL, low(pointer_list*2)
	lsl	r16		; data # times 2 for word addr
	add	ZL,r16		; make table address
	clr	r16
	adc	ZH,r16		; propagate carry
	lpm	r16,Z+		; get address from table
	lpm	ZH,Z
	lsl	r16		; get address at table
	rol	ZH		; x2
	mov	ZL,r16		; into Z
; now Z points to the data you want
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

peret wrote:
When you put DB tables in Flash it's important to remember that all your .DB lines must have EVEN numbers of bytes.

The answer to this is to remeber that each new .db line will start on a new even (word) boundary, this should allow you to keep your calculations in check. Any additional padded bytes should be at the highest (unused) address in an odd length list, making the padding transparent.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

Last Edited: Tue. Aug 17, 2004 - 12:16 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks folks! I think I'm going to put my data in EEPROM (because it is byte addressable, constant as far as using it and MIGHT need to be changed later) and I'm going to store my table locations in flash or SRAM. Thanks for all the information and tips.

Jason