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.

PIC 16f877A EEPROM Programming Help

Status
Not open for further replies.

firevsice55

Junior Member level 1
Joined
May 1, 2010
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,392
We have a system in which data is stored in the eeprom after every 10 minutes.
There is a situation, power down occurs, so the problem is to find the latest data, and start storing new data from consecutive locations from latest data onwards.
There can also be a situation that when power down occurs, only a part of the data is stored, so we have to discard that data, and overwrite only that part of the data and start storing data in consecutive locations.

We also have to check that the data we are storing is correct and not corrupt, so validity of the data is also to be checked

Please help me out in the logic development.
 

I would recommend keep several sets of your data in the EEPROM along with a pointer to last verified set.

Depending on the amount and types of data, you may want to temporarily storage the data in typedef struct, at the 10 minute interval you write the struct byte by byte to the EEPROM, read the data back and verify it with the original struct in RAM and then update the pointer.

If this processes is interrupted before the data is verified and pointer updated, upon reset the pointer will still point to the last valid data set and the process can continue as before. Using this technique there is no need to revalidate any previous stored data set.

The EEPROM data memory of the PIC16F87XA allows single byte read and writes, rather than specific block/sector writes. So there is little danger of previously written data being corrupted during a power outage.

BigDog
 
  • Like
Reactions: FvM and alexan_e

    alexan_e

    Points: 2
    Helpful Answer Positive Rating

    FvM

    Points: 2
    Helpful Answer Positive Rating
I would recommend keep several sets of your data in the EEPROM along with a pointer to last verified set.

Depending on the amount and types of data, you may want to temporarily storage the data in typedef struct, at the 10 minute interval you write the struct byte by byte to the EEPROM, read the data back and verify it with the original struct in RAM and then update the pointer.

If this processes is interrupted before the data is verified and pointer updated, upon reset the pointer will still point to the last valid data set and the process can continue as before. Using this technique there is no need to revalidate any previous stored data set.

The EEPROM data memory of the PIC16F87XA allows single byte read and writes, rather than specific block/sector writes. So there is little danger of previously written data being corrupted during a power outage.

BigDog


Thank you for the reply.
But my question to your answer is, upon reset or power down wouldn't all the data we have on the pic be changed to their default values, so basically i will lose all the current values in the variables after reset. So then how would I have access to my latest data on the eeprom.
 

You would store the pointer to the last valid data set in the EEPROM as well.

Upon reset the pointer would be retrieved from EEPROM and your process could restart where it left off.

A question I have is the data set variable in length or fixed?

BigDog
 

You would store the pointer to the last valid data set in the EEPROM as well.

Upon reset the pointer would be retrieved from EEPROM and your process could restart where it left off.

A question I have is the data set variable in length or fixed?

BigDog


The data set is of fixed variable length.

as you know eeprom can be written many times over.
okay I got idea of storing the pointer as well in the eeprom. But then consider a situation wherein the eeprom is already full of data, and it starts writing into the eeprom over the older data from the start location of the eeprom. So now in this condition how would I know which is the latest data, as the latest data set is surrounded by the old data as well as the new data.

And how could the process start from where it left off, because it has to find the last location where it wrote the data in the eeprom.
 

The data set is of fixed variable length.

Good, that simplifies things.

But then consider a situation wherein the EEPROM is already full of data, and it starts writing into the eeprom over the older data from the start location of the eeprom. So now in this condition how would I know which is the latest data, as the latest data set is surrounded by the old data as well as the new data.

And how could the process start from where it left off, because it has to find the last location where it wrote the data in the eeprom.

You're missing the point of the pointer in EEPROM. You are basically creating a linked list of only two or three links or data sets. After a link is update with the current data and verified the pointer in EEPROM is then and only then the pointer is updated to point to that link of the data set just updated. If the process is interrupted the pointer in EEPROM will still point to the last viable data set in the list. Since the data set length is fixed, when the process restarts upon reset, the next link is updated with the current data based on the current pointer plus the fixed length of the datasets, once the pointer in EEPROM is pointing at the second or third link, the updating processes starts back at the first link of the chain, similar to a circular buffer.

The most recent data set is always pointed to by the pointer in EEPROM, the next data set to write and verify the data is known by the pointer plus the fixed length.

With only two or three links or data sets you could even simplify the process further by storing the current link number in EEPROM address 0x00 followed by the pointer to each link in address 0x01, 0x02 and 0x03 sequentially. When the time comes to update the data set, you simply retrieve the next pointer in the sequence and the update the data, verify it, and then and only then update the link number contained in address 0x00. Once the process updates the last link whether it be #2 or #3 the process begins again at the top of the chain.

It's rather a straight forward procedure and should be easy to implement. I've used the same scheme on several projects to hold various types of calibration and position data.

BigDog
 
Last edited:
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
Good, that simplifies things.



You're missing the point of the pointer in EEPROM. You are basically creating a linked list of only two or three links or data sets. After a link is update with the current data and verified the pointer in EEPROM is then and only then the pointer is updated to point to that link of the data set just updated. If the process is interrupted the pointer in EEPROM will still point to the last viable data set in the list. Since the data set length is fixed, when the process restarts upon reset, the next link is updated with the current data based on the current pointer plus the fixed length of the datasets, once the pointer in EEPROM is pointing at the second or third link, the updating processes starts back at the first link of the chain, similar to a circular buffer.

The most recent data set is always pointed to by the pointer in EEPROM, the next data set to write and verify the data is known by the pointer plus the fixed length.

With only two or three links or data sets you could even simplify the process further by storing the current link number in EEPROM address 0x00 followed by the pointer to each link in address 0x01, 0x02 and 0x03 sequentially. When the time comes to update the data set, you simply retrieve the next pointer in the sequence and the update the data, verify it, and then and only then update the link number contained in address 0x00. Once the process updates the last link whether it be #2 or #3 the process begins again at the top of the chain.

It's rather a straight forward procedure and should be easy to implement. I've used the same scheme on several projects to hold various types of calibration and position data.

BigDog

Okay, I have understood, that the pointer is going to be holding the latest location written into the eeprom. So that we can access it after a reset or power down or any such interruption.
Is this pointer user defined?
Wont the pointer value be erased after the above interruption occurs?
And if we are saving the pointer in the eeprom then, how can we find it from the rest of the data?

Could you please the concept in a code..?
 

Is this pointer user defined?

Yes, you can use whatever section of the EEPROM you wish, as long as it contains enough bytes to store two or three copies of the typedef structure.
The location of the pointer itself is also at your discretion as well.

Wont the pointer value be erased after the above interruption occurs?

No, it's contained in EEPROM which is nonvolatile, unaffected by loss of power.

And if we are saving the pointer in the eeprom then, how can we find it from the rest of the data?

You'll effective by creating a constant pointer to a pointer in the code itself, which is stored in the program flash, also nonvolatile storage. The constant pointer to the EEPROM pointer will remain intact until you rewrite the program flash.

Could you please the concept in a code..?

Do you want me to do everything for you?

You need to attempt to write the routine yourself. If you have problems, post your code and I can assist you with particular issues.

After all this is your project, not mine and you will have to maintain the code in the future.

And to do so requires an understanding of the code, which you will not acquire if I write it for you.

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
Yes, you can use whatever section of the EEPROM you wish, as long as it contains enough bytes to store two or three copies of the typedef structure.
The location of the pointer itself is also at your discretion as well.



No, it's contained in EEPROM which is nonvolatile, unaffected by loss of power.



You'll effective by creating a constant pointer to a pointer in the code itself, which is stored in the program flash, also nonvolatile storage. The constant pointer to the EEPROM pointer will remain intact until you rewrite the program flash.



Do you want me to do everything for you?

You need to attempt to write the routine yourself. If you have problems, post your code and I can assist you with particular issues.

After all this is your project, not mine and you will have to maintain the code in the future.

And to do so requires an understanding of the code, which you will not acquire if I write it for you.

BigDog


Thank you, Big Dog.
I have finally understood what you are trying to say. I will write a code for it.

Thanks again.
 

Yes, please do. It is to your benefit to understand the code.

And if you encounter any problems or issues, post them here in this discussion thread and I will be more than happy to advise and assist you.

BigDog
 

Code:
#include <pic.h>
#include <eeprom_routines.h>

void delay_time(unsigned int cnt);
void valid_data_check(char address);

unsigned int data[5]={0x34,0x55,0x12,0x0a};

unsigned int eeprom_input_data,i;
char memory_address=0x01,maximum_memory_address=0xff,temp_address,first_loc=0x00,loc; 	// startin_memory_address
unsigned int size_of_data=0,counter=0,data_to_be_written=0,checksum=0; 

void main()
{
//	if(counter>0)
//		{
//			loc=eeprom_read(first_loc);
//			memory_address=loc;
//		}	
//	else 
//		{
//			eeprom_write(0x00,0x01);
//			delay_time(10);
//		}
//		
	data[4] = data[0]+data[1]+data[2]+data[3];			// Checksum calucated and stored in the data 5th location
	
	if (memory_address >= maximum_memory_address)       // we already have the maximum memory address value
		{
			delay_time(10);
			memory_address=0x01;						// always starts from the 2nd location of the eeprom
    	}
	else
		{
			temp_address=0x00;
			temp_address=memory_address;
			for(i=0;i<5;i++)
				{
					data_to_be_written=data[i];
					eeprom_write(temp_address,data_to_be_written);	// stores the data at the appropriate 
					delay_time(10);									// memory location
					temp_address++;
				}
			memory_address+=temp_address;
			eeprom_write(first_loc,temp_address);					// stores the last written address location 
			delay_time(10);											// in the first location of eeprom
		}
}


void valid_data_check(char address) 			//function for chcking the validity of data whenver required
    {											// by providing the starting location
	    int flag=0,check[5],j=0;
		char tmp;
		char mem_add=0x01;
		
		do
			{
				for(j=0;j<5;j++)
					check[i]=eeprom_read(memory_address);
	
				tmp=check[0]+check[1]+check[2]+check[3];		//determining the checksum value

				if(check[4]==tmp)								//verifying the checksum
					flag=1;
				else
					flag=0;
				
				mem_add+=5;										// moving onto the next data set
		    }
    		
     		while(mem_add == maximum_memory_address || flag == 0);
	}

void delay_time(unsigned int cnt)
{
	unsigned int i,j;
	for(i=0;i<cnt;i++)
		for(j=0;j<10000;j++);
}


I have written this code, I havent used the pointer method, instead I am storing the last written memory in the 0th location of eeprom.
After verifying the data is correct I am updating the 0th location of eeprom.
Wht do u think?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top