Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to write long int (32bits) to eeprom c codes

Status
Not open for further replies.

engineer khan

Member level 3
Joined
Aug 31, 2012
Messages
66
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,833
i need to write or save unsigned long int into eeprom but eeprom has 8bits location how to convert it then back retrieve it to its original form?

please give me hints i m confused too much thanks in advance
 

you need to store those 32 bits in successive loaction either from MSB to LSB or from LSB to MSB and retrieve back from the same location in same format how you written...
 
  • Like
Reactions: Dont

    Dont

    Points: 2
    Helpful Answer Positive Rating
Code:
void EE_Write32 (long address, lond value)
{
 EE_Write8(adderss, (value & 0xFF000000) >> 24);
 EE_Write8(adderss, (value & 0x00FF0000) >> 16);
 EE_Write8(adderss, (value & 0x0000FF00) >> 8);
 EE_Write8(adderss,  value & 0x000000FF);
}
 

but how to convert it into 4 portions each 8bits then how to reconvert it to one 32bits number wfter reading

- - - Updated - - -

its great i got the conversion concept 32bits number to 4 parts each 8bits and stored it but on reading the eeprom i will get 8bits portion then 4portions each 8bits need to be converted back into 32bits number how to combine it....thanks too much, Easyrider83
 

The same way:
value32 = (value8_3 << 24) | (value8_2 << 16) | (value8_1 << 8) | value8_0
 

Hello Easyider!

This method is certainly fine, but I usually prefer this one:


Code C - [expand]
1
2
3
4
EEWrite32(uint32 address, uint32 value) {
    uint8 * valarray = (uint8 *)(&value);
    for(int i = 0 ; i < 4 ; ++i) EEWrite8(address+i, valarray[i]);
}



The advantages: it is more compact, and it works the same way, on big endian and little endian machines.
I like it because it takes data as it is and where it is. It is clean beacuse it does not feed a 32-bit parameter
((value & 0xFF000000) >> 24) is anyway a 32-bit value from the processor point of view, with 24 leading
0s) into a function that expects a 8-bit parameter.

I have never tested which one is faster, but since the EEPROM write is incredibly slow, I guess there is not that
much difference.
But anyway, that's more of a matter of taste.

Dora.

Code:
void EE_Write32 (long address, lond value)
{
 EE_Write8(adderss, (value & 0xFF000000) >> 24);
 EE_Write8(adderss, (value & 0x00FF0000) >> 16);
 EE_Write8(adderss, (value & 0x0000FF00) >> 8);
 EE_Write8(adderss,  value & 0x000000FF);
}
 

Dear doraemon, you are too long time working with 8 bit architecture.
And never compare C code. Always compare only assembly output.
 

Hello!

Dear doraemon, you are too long time working with 8 bit architecture.

Two things:
- I have never used a 8 bit microcontroller;
- I have used the above source myriads of times.
Ok, I don't pretend I have that much experience in microcontroller programming.
However, if I may comment what you said, you would be a lot more convincing by
giving us a concrete overview of what may happen by using the code I was suggesting.
Do you have any concrete environment / processor configuration where this would not
work?

And never compare C code. Always compare only assembly output.

Well, you have your programming methods, I have mine. You compare assembly?
I compare the results and the speed. I just compared the speed, and the array
method is faster. And in all the environments I used the array method, there was no error.

Dora.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top