Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 24th July 2008, 04:37 AM   (permalink)
Exclamation Internal EEPROM of 18F4550 problem

Hi

I ran into some problems with the internal EEPROM (256bytes) of the above uC. See attached code segments :

Code:
//Prepare for EEPROM write to address 0x00
EECON1bits.EEPGD = 0;
EECON1bits.WREN = 1; 			
EEADR = 0x00; 	 //<- to be swapped with address 0x02
EEDATA = 'A'; 	   
EECON2 = 0x55; 				
EECON2 = 0xaa; 
EECON1bits.WR = 1;
while (!PIR2bits.EEIF);	
PIR2bits.EEIF = 0; 				
EECON1bits.WREN = 0;EEDATA writes */

//Prepare for EEPROM write to address 0x02
EECON1bits.EEPGD = 0; 				
EECON1bits.WREN = 1; 				
EEADR = 0x02; 	//<- to be swapped with addres 0x00
EEDATA = '2'; 	
EECON2 = 0x55; 						
EECON2 = 0xaa; 				
EECON1bits.WR = 1;
while (!PIR2bits.EEIF);			
PIR2bits.EEIF = 0;
EECON1bits.WREN = 0;

// Read address 0x02 to verify previous EEPROM writes
EEADR = 0x02; 			
EECON1bits.EEPGD = 0; 		
EECON1bits.RD = 1; 				
string1[0] = EEDATA;
Essentially the above pgm assigned char A to address 0x00 and char 2 to address 0x02. Reading 0x02 after the writes gave me a "2".

When I swap the addresses, ie first do a write to 0x02 with char "A', then a write to 0x00 with char "2", a read of 0x02 gave me a "2" instead of the expected result of "A".

It seems that i get the latest write data regardless of which address I specify.

Any thoughts? Thks!
Urahara is offline  
Reply With Quote
Old 24th July 2008, 05:01 AM   (permalink)
Default

You may be writing to the config register as you haven't cleared EECON1bits.CFGS.

Mike.
Pommie is online now  
Reply With Quote
Old 24th July 2008, 08:40 AM   (permalink)
Default

Hi Mike

Your comments worked! Thks!

Interestingly, I cut and pasted the original write routine from the C18 User Guide(DS51228J) which does not have the instruction to set the CFGS bit. Guess you cannot trust all documentations.
Urahara is offline  
Reply With Quote
Old 24th July 2008, 11:43 AM   (permalink)
Default

The ability to read/write the config words in software is a fairly new feature and so a lot of the examples don't take it into account.

Anyway, good to hear it's now working.

Mike.
Pommie is online now  
Reply With Quote
Old 16th August 2008, 06:33 AM   (permalink)
Default

Hi all,

I have tried to write data to EEPROM in PIC 18F4520 but supprisingly EEPROM did not remain my data after reset or reprogram the chip. Could anyone tell me what happened? Thanks

hmk
hmk is offline  
Reply With Quote
Old 16th August 2008, 09:02 AM   (permalink)
Default

You need to post your code if you want to get any help.

Mike.
Pommie is online now  
Reply With Quote
Old 2nd September 2008, 07:12 AM   (permalink)
Default

Hi

I have just switched to another MCU 18F4620 and it seems that the problem reported by hmk has occurred. This MCU's EEPROM has 1024bytes vs 18F4550's 256bytes.

Code:
void write2eeprom(unsigned int eeprom_addr, unsigned char eeprom_data)
{
	EECON1bits.EEPGD = 0; 				/* Ensure EEPGD is clear for EEDATA access */
	EECON1bits.CFGS=0;					/* Select Data EEPROM */
	EECON1bits.WREN = 1; 				/* Ensure WREN is set to enable EEDATA writes */
        EEADRH = ((eeprom_addr>>8)&0x03);   //Bits 2-7 are masked off since EEPROM is only 1KB
        EEADR =(eeprom_addr&0xFF);
	EEDATA = eeprom_data; 				/* Set EEDATA to the value to write */
	EECON2 = 0x55; 						/* Write 0x55 to EECON2 */
	EECON2 = 0xaa; 						/* Write 0xAA to EECON2 */
	EECON1bits.WR = 1; 					/* Initiate write cycle by setting the WR bit */
	while (!PIR2bits.EEIF);				/* Wait for the EEIF flag to be set */
	PIR2bits.EEIF = 0; 					/* Clear the EEIF flag */
	EECON1bits.WREN = 0; 				/* Ensure WREN is set to enable EEDATA writes */
}
The data is lost whenever I power off/on the board. Any idea where I could have gone wrong?
Urahara is offline  
Reply With Quote
Old 2nd September 2008, 07:26 AM   (permalink)
Default

How do you know it is being lost?

Mike.
Pommie is online now  
Reply With Quote
Old 2nd September 2008, 07:56 AM   (permalink)
Default

After writing data to the EEPROM, I have another routine that reads it. The data is there when I do a read after a write.

I then proceed to power off, then power on the device. I am not able to read the data from here onwards.

I had the same problem with the earlier 18F4550 but after your recommendation to clear EECON1bits.CFGS bit, it works. No such luck for this chip though.
Urahara is offline  
Reply With Quote
Old 2nd September 2008, 08:05 AM   (permalink)
Default

Can you post your read routine?

Mike.
Pommie is online now  
Reply With Quote
Old 2nd September 2008, 08:25 AM   (permalink)
Default

Rechecked the read subroutine after you've asked for it. This was the one I used :

Code:
unsigned char eeprom2read(unsigned int eeprom_addr)
{
        EEADRH = ((eeprom_addr>>8)&0x03);   //Bits 2-7 are masked off since EEPROM is only 1KB
        EEADR =(eeprom_addr&0xFF);
	EECON1bits.EEPGD = 0; 			/* Ensure EEPGD is clear for EEDATA access */
	EECON1bits.RD = 1; 				/* Trigger a read by setting the RD bit */
	return EEDATA;					/* Read the result from EEDATA register */
}
Reread the datasheet. It said to clear PGD bit, and set the RD bit. It didnt say anything abt clearing the CFGS bit. But the assembly language example has an instruction to clear it. So I tried. Guess what? It is working now!!

So you cannot fault me for what happened. I did RTFM but the M wasnt consistent. Anyway, glad it is ok now.

Thks for the prompt response Mike/Pommie!
Urahara is offline  
Reply With Quote
Old 2nd September 2008, 08:55 AM   (permalink)
Default

Good to hear it's all working now.

Mike.
Pommie is online now  
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
PIC18F4620 Internal EEPROM Pyromanci Micro Controllers 1 29th January 2008 08:05 PM
18F4550 - PORTD inputs are shaky, even with internal pull up enabled toodles Micro Controllers 0 29th April 2007 02:05 AM
eeprom internal pic16f628 neelam29 Micro Controllers 5 17th February 2006 08:58 AM
internal i2c eeprom alamy Micro Controllers 9 14th March 2005 11:26 AM
Using Internal Data EEPROM on PIC16F877 lee_man_tbu Micro Controllers 0 4th December 2003 01:47 AM



All times are GMT. The time now is 06:25 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.