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.

[SOLVED] long integer number save in flash memory

Status
Not open for further replies.

buddhikaneel

Member level 1
Joined
Jul 27, 2011
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,542
Hi.
I want to save long integer number (like 01234567890) in Flash Memory or EEPROM. how can i do it.
I try to do it like this

(256*256*256*256) (256*256*256) (256*256) (256)
73 150 2 210 (Decimal Format)
now i can save this 4 segment in EEProm or Flash Memory. But i don't know is this way correct or wrong. Can any body tell how can i do it easy way...
Mikroc Pro is compiler.

Thanks.
 

Hi buddhikaneel,

Why not save the long as it is represented in the program code, a series of bytes, four to be precise.

Simply perform an implicit type conversion from long to unsigned char byte1, right shift the long eight places, perform another implicit type conversion from long to unsigned char byte2, and so forth...

You can store each byte sequential in EEPROM either during or after the routine.

Then when retrieving the 4 bytes from EEPROM, you simply reverse the routine.

BigDog
 
if you can send me simply code to do this please....
Thanks for the reply
 
Last edited:

There are several ways to accomplish this task, a simple example:

Code:
int convertLongToBytes( unsigned char * convBytes, unsigned long target):

void main(void)
{
	unsigned char longBytes[4];
    unsigned long test = 2147483647;

    convertLongToBytes( longBytes, test);

    while(1);

  
}

int convertLongToBytes( unsigned char * convBytes, unsigned long target)
{
	unsigned char i;

    for(i=0; i < 4; i++)
    {
		convBytes[i] = target;
		target = target >> 8;
	}

 	return i;
}

Calling the routine convertLongToBytes(), passing a pointer to an array of unsigned char longBytes[] along with the unsigned long target, will convert the long and store the four bytes in the longBytes[], the unisigned long target will remain unchanged since it was passed by value.

I should also mention the long integer should be in fact an unsigned long or cast as such before passing it to the routine. Some compilers pad the MSBs with a value of 1 when shifting a signed integer type.

The routine also returns the total number of bytes used in the conversion, for possible adaption to a more flexible routine in the future.

I'll leave it to you, to write the reverse routine, it shouldn't be to difficult. Post any further problems you may have.

BigDog
 
Finally i did what i want. your code is helped me. Thanks a lot.
 

Hi,

You could also use a single unsigned byte variable in the convertLongToBytes() and then pass it to another routine which writes the byte directly to EEPROM sequentially.

Doing so would remove the need of an unsigned char array.

Like I said before, there are lots of options.

Just break down your project into manageable tasks and write a routine for each task.

After you have debug each task you can combine them to form your program.

BigDog
 
if this number is known during programming and is not going to change during execution then you should be able to declare it as a long int flash constant.

About the eeprom (I assume the internal eeprom), maybe there is a write block function that can do a write of a custom length variable , I know that something like this exist in AVR (avr-libc: <avr/eeprom.h>: eeprom_write_block) but I don't know if there is a similar function for PIC

Alex
 
It will change during execution. what ever i did it. Thanks for comment
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top