about atmaga 328 EEPROM - Page 1
Forum section: electronics / general

author: ranganatennakoon
author_rank: Contributor
timestamp: April 29, 2020, 01:10:09 pm
content: how to store 10 bit numbers in atmega328 eeprom

author: newbrain
author_rank: Super Contributor
timestamp: April 29, 2020, 01:16:15 pm
content: As little-endian 16 bit numbers.

author: ranganatennakoon
author_rank: Contributor
timestamp: April 29, 2020, 04:00:13 pm
content: atmega328 has a(bytes)1024 eeprom it means can store 255 numbers X 1024but i want to store 0 to 500 value of data. how can i do this ?

author: senso
author_rank: Frequent Contributor
timestamp: April 29, 2020, 04:02:39 pm
content: Use the already working eeprom read/write/update functions and just write an int16 or uint16, as easy as it can be.I think it was eeprom_read_word/eeprom_write_word.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: April 29, 2020, 08:18:30 pm
content: Last time I remember using the AVR Eeprom I just dumped all variables in a structure and used something like:> Eeprom_update_block( Struct, sizeof( Struct));

author: NivagSwerdna
author_rank: Super Contributor
timestamp: April 29, 2020, 08:33:49 pm
content: 1 KB not 1 Kb?Is this data changing? If not... you can always put it in flash (PROGMEM)?

author: TomS_
author_rank: Frequent Contributor
timestamp: April 29, 2020, 09:38:37 pm
content: Quote from: ranganatennakoon on April 29, 2020, 04:00:13 pmWell if you want to be more specific, it holds 8 bit values which can represent numbers 0-255. A number with a value of 500 will be represented in 16 bits, aka two 8 bit values side by side.So you just have to break the number into it's two 8 bit values and store them individually.

author: ranganatennakoon
author_rank: Contributor
timestamp: April 30, 2020, 09:41:13 am
content: Well if you want to be more specific, it holds 8 bit values which can represent numbers 0-255. A number with a value of 500 will be represented in 16 bits, aka two 8 bit values side by side. So you just have to break the number into it's two 8 bit values and store them individually.TomS_ can i give me an example cord ? thank you !

author: TomS_
author_rank: Frequent Contributor
timestamp: April 30, 2020, 09:25:11 pm
content: Quote from: ranganatennakoon on April 30, 2020, 09:41:13 amWell, the idea was to give you some information which you could then use to discover the answer yourself so that you can learn.If you know that value X needs to be stored in 16 bits, and you need to be able to store that value into an EEPROM which only handles 8 bits at a time, and assuming there is no existing way to do this, then you need to discover how to split a 16 bit value into two 8 bit values.I'll give you some hints: one of them is using logical shifts. The other could be pointers and casting (but I would start with shifts).

author: sleemanj
author_rank: Super Contributor
timestamp: April 30, 2020, 09:47:30 pm
content: You want to use the eeprom_write_word functionhttps://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.htmlProvide address in the eeprom you want to store at, and the 16 bit number to store. The inverse is of course eeprom_read_wordCode:uint16_t val = 500;uint16_t addr = 0;eeprom_write_word((uint16_t *)addr, val);... time passes ...val = eeprom_read_word((uint16_t *)addr);Remember that the address is a byte address in flash, if you store a 16 bit word at 0, don't then go storing something at 1

author: jackthomson43
author_rank: Contributor
timestamp: May 01, 2020, 12:29:38 pm
content: Atmega328 has 1024 bytes of EEPROM thus you can save data of 256 x 1024, you should checkAtmega328 Datasheetto get the details.

author: mikerj
author_rank: Super Contributor
timestamp: May 01, 2020, 12:39:42 pm
content: Quote from: jackthomson43 on May 01, 2020, 12:29:38 pmWell it can only store as much data as the size of the card. You'd use a significant amount of the program memory in in a 328 with functions to use an SD card, plus the cost of the card and socket, the extra pins used on the micro, higher power consumption etc..The on board EEPROM is extremely easy to use and perfect for the small amounts of NV data that you typically need on small micros.

author: Mechatrommer
author_rank: Super Contributor
timestamp: May 01, 2020, 01:22:21 pm
content: Quote from: ranganatennakoon on April 29, 2020, 04:00:13 pmyou can store 10bits value in 16bits value if you want as others mentioned, its a lot hassle and trouble free and more readable. and hence can store 512 of your numbers in the eeprom in normal int16 array. but if you insist on 10bits shoulder to shoulder or running out of space, you can store 816 10bits numbers using packed bit-fields in struct (or union), but you have to make sure the struct is 8bits aligned, since we cant get around that machine architecture limitation/specification and cant do anything that we wish...http://icarus.cs.weber.edu/~dab/cs1410/textbook/5.Structures/unions.htmlhttps://stackoverflow.com/questions/17494853/using-bit-fields-for-avr-portseg:Code:struct my_data {unsigned val1 : 10;unsigned val2 : 10;unsigned val3 : 10;unsigned val4 : 10;}; // 5 bytes data size, 4x 10bits values.since the field-bit is more than 8 bits, you may do your homework, it may work it may not. or maybe you have to device fancier trick around that bit-field and union feature. ymmv.SD card? yes basically unlimited storage size afa most mcu firmwares are concerned, but i suspect it will be much slower than eeprom access, and needs few other external components. ymmv.

author: jackthomson43
author_rank: Contributor
timestamp: May 02, 2020, 06:48:21 am
content: ATmega328 EEPROM is of 255 bits, so saving 10 bit won't be an issue.You should checkAtmega328 Pinoutfirst.

author: mikerj
author_rank: Super Contributor
timestamp: May 02, 2020, 06:31:21 pm
content: Quote from: jackthomson43 on May 02, 2020, 06:48:21 amThe ATmega328 EEPROM is 1024*8 bits.