how to store 10 bit numbers in atmega328 eeprom
As little-endian 16 bit numbers.
atmega328 has a
(bytes)
1024 eeprom it means can store 255 numbers X 1024 but i want to store 0 to 500 value of data. how can i do this ?
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.
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));
1 KB not 1 Kb?
Is this data changing? If not... you can always put it in flash (PROGMEM)?
atmega328 has a 1 kb eeprom it means can store 255 numbers X 1024 but i want to store 0 to 500 value of data. how can i do this ?
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.
[/quote]
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.
[/quote]
TomS_ can i give me an example cord ? thank you !
TomS_ can i give me an example cord ? thank you !
Well, 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).
You want to use the eeprom_write_word function
https://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_word
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
Atmega328 has 1024 bytes of EEPROM thus you can save data of 256 x 1024, you should check
Atmega328 Datasheet to get the details.
SD card is best, can save any amount of data.
Well 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.
atmega328 has a (bytes)
1024 eeprom it means can store 255 numbers X 1024 but i want to store 0 to 500 value of data. how can i do this ?
you 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:
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.
ATmega328 EEPROM is of 255 bits, so saving 10 bit won't be an issue.
You should check
Atmega328 Pinout first.
ATmega328 EEPROM is of 255 bits, so saving 10 bit won't be an issue.
The ATmega328 EEPROM is 1024*8 bits.