View previous topic :: View next topic |
Author |
Message |
micr_brain
Joined: 29 Jun 2007 Posts: 5
|
16bit to eeprom? |
Posted: Sun Jan 13, 2008 9:29 pm |
|
|
Hi electronics friends.
How write 16bit to internal EEPROM ?
thanks
micr_brain |
|
|
marc10k
Joined: 15 Jan 2007 Posts: 11 Location: Germany
|
|
Posted: Mon Jan 14, 2008 1:15 am |
|
|
This code is used for a Pic18
Code: |
/******************************************************************************
* *
* Access to the internal EEPROM of the Microcontroller *
* *
* R_W -> 0..read, 1..write *
* Base_Address-> internal EEPROM address *
* Data -> data to read from/write into internal EEPROM *
* Length -> how long is variable ( 8..8 bit, 16..16 bit, *
* 24..24 bit, 32..32 bit) *
* Returns -> 1..everything OK, 0..EEPROM write failed *
* *
******************************************************************************/
#inline
int1 Internal_EEPROM(int1 R_W, int Base_Address ,int32& Data, int Length)
{
int j, i, loop_end;
int32 ReRead;
loop_end = Length/8; // determine how many bytes need to be read/written
switch (R_W)
{
case 0: // Read from internal EEPROM
Data = 0x00;
for(i=0; i < loop_end; i++)
{
*(((int8 *)&Data)+loop_end-i-1) = Read_EEPROM(Base_Address + i);
}
return(OK);
break;
case 1: // Write to internal EEPROM
for (j = 0; j < INT_ROM_RETRY; j++) // write until error exceeds threshold
{
ReRead = 0x00;
for(i=1; i <= loop_end; i++)
Write_EEPROM(Base_Address+i-1, *((int8 *)&Data + loop_end - i)); // write data to the EEPROM
for(i=0; i < loop_end; i++)
*(&(int8 *)ReRead+loop_end-i-1) = Read_EEPROM(Base_Address + i); // read written data
if (ReRead != Data) { continue; } else return(OK);
}
Error_Report(Error_Int_EEPROM);
return(FAILURE);
} break;
} |
|
|
|
micr_brain
Joined: 29 Jun 2007 Posts: 5
|
|
Posted: Mon Jan 14, 2008 6:18 am |
|
|
thanks marc10k
I am useing PIC16 (16F877a )
I want to write a 16bit int to EEPROM
and read read 16bit int from EEPROM
I am a bigner
Thanks micr_brain |
|
|
marc10k
Joined: 15 Jan 2007 Posts: 11 Location: Germany
|
|
Posted: Mon Jan 14, 2008 6:28 am |
|
|
I changed the code a bit to just write and read 16bit. Now you can call the function for reading:
Internal_EEPROM(0, your_adresse, your_16bit_variable)
writing is:
Internal_EEPROM(1, your_adresse, your_16bit_variable)
I havent tried the code but it should work fine. Otherwise use the old finction and set the length to 16 like:
Internal_EEPROM(0, your_adresse, 16, your_16bit_variable)
Marcus
Code: |
/******************************************************************************
* *
* Access to the internal EEPROM of the Microcontroller *
* *
* R_W -> 0..read, 1..write *
* Base_Address-> internal EEPROM address *
* Data -> data to read from/write into internal EEPROM *
* Returns -> 1..everything OK, 0..EEPROM write failed *
* *
******************************************************************************/
#inline
int1 Internal_EEPROM(int1 R_W, int Base_Address ,int32& Data)
{
int j, i;
int32 ReRead;
switch (R_W)
{
case 0: // Read from internal EEPROM
Data = 0x00;
for(i=0; i < 2; i++)
{
*(((int8 *)&Data)+1-i) = Read_EEPROM(Base_Address + i);
}
return(OK);
break;
case 1: // Write to internal EEPROM
for (j = 0; j < INT_ROM_RETRY; j++) // write until error exceeds threshold
{
ReRead = 0x00;
for(i=1; i <= 2; i++)
Write_EEPROM(Base_Address+i-1, *((int8 *)&Data + 2 - i)); // write data to the EEPROM
for(i=0; i < 2; i++)
*(&(int8 *)ReRead+1) = Read_EEPROM(Base_Address + i); // read written data
if (ReRead != Data) { continue; } else return(OK);
}
Error_Report(Error_Int_EEPROM);
return(FAILURE);
} break;
} |
|
|
|
ralph79
Joined: 29 Aug 2007 Posts: 87
|
What if it was much more simple |
Posted: Mon Jan 14, 2008 12:13 pm |
|
|
something like this:
Code: |
...........
void write_2_bytes_eeprom(int8 position_eeprom, int16 value_2_save)
{
write_eeprom(position_eeprom + 1, value_2_save & 0xFF);
value_2_save >>= 8;
write_eeprom(position_eeprom, value_2_save & 0xFF);
}
int16 read_2_bytes_eeprom(int8 position_eeprom)
{
int16 value_readed = 0;
value_readed = read_eeprom(position_eeprom);
value_readed <<= 8;
value_readed |= read_eeprom(position_eeprom + 1);
return value_readed;
}
.....
void main(void)
{
int16 val_eeprom = 0x0000;
write_2_bytes_eeprom(0x00, 0x1234);
write_2_bytes_eeprom(0x02, 0x1234);
val_eeprom = read_2_bytes_eeprom(0x00);
while(1)
{
..........
}
}
|
|
|
|
micr_brain
Joined: 29 Jun 2007 Posts: 5
|
|
Posted: Tue Jan 15, 2008 4:45 am |
|
|
Thanks marc10k and ralph79
I tested ralph79's code. it is nice .
I have one more doubt !
how write a array to eeprom and read
Thanks
micr_brain |
|
|
ralph79
Joined: 29 Aug 2007 Posts: 87
|
Not tested but... |
Posted: Tue Jan 15, 2008 8:19 am |
|
|
Not tested but...I think that something like this should work
Code: |
int16 buf[10];
int8 i, pos_eeprom = 0;
for (i = 0; i < 10; i++)
{
write_2_bytes_eeprom(pos_eeprom, buf[i]);
pos_eeprom += 2;
}
for (i = 0; i < 10; i++)
{
buf[i] = read_2_bytes_eeprom(pos_eeprom);
pos_eeprom += 2;
} |
Best regards |
|
|
|