how EEPROM write works in Arduino

Status
Not open for further replies.

aliyesami

Full Member level 6
Joined
Jan 7, 2010
Messages
369
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Location
USA
Visit site
Activity points
4,190
I saw the following in the arduino-examples section but i am not understanding why the analog input is being divided by 4.
if e.g the input value is 1023 then it will require 2 bytes to store since 1023 is 3FF hex. but if i divide it by 4 then i am not storing the original value rather the divided value .
can someone explain how i can save the input analog values to EEPROM ?

thanks


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void loop()
{
   // need to divide by 4 because analog inputs range from
   // 0 to 1023 and each byte of the EEPROM can only hold a
   // value from 0 to 255.
   int val = analogRead(0) / 4;
   // write the value to the appropriate byte of the EEPROM.
   // these values will remain there when the board is
   // turned off.
   EEPROM.write(addr, val);
      // advance to the next address.  there are 512 bytes in 
   // the EEPROM, so go back to 0 when we hit 512.
   addr = addr + 1;
   if (addr == 512)
     addr = 0;
   delay(100);

 
Last edited by a moderator:

This can be done for some of the following possible reasons:
  • The core of your Arduino platform presumably may be 8-bit data bus.
  • EEPROM referred on above code is an external I2C or SPI whose data bus is sized to 8-bit.
In both cases, scaling original acquired data to 8 bits magnitude will lose some precision after restore.
At certain applications this don´t means properly a problem.


+++
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…