Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Internal EEPROM of 18F4550 problem

Status
Not open for further replies.

Urahara

Member
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".:confused:

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

Any thoughts? Thks!
 
You may be writing to the config register as you haven't cleared EECON1bits.CFGS.

Mike.
 
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.
 
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.
 
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
 
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?
 
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.
 
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. :D I did RTFM but the M wasnt consistent. Anyway, glad it is ok now.

Thks for the prompt response Mike/Pommie!
 
Hi All I have a problem in writing EEPROM of my PIC 18f452 in MPLAB. When i write the code and simulate using MPLAB SIM it shows nothing written after simulation and the RAM still seen empty. Although I have written the code provided in the data sheet. But MPLAB EEPROM Window in responceless after exec. this code
Kindly Help Me!
reply me at -> arsalan.javeed@gmail.com
movlw 4h
movwf EEADR
movlw a'A'
movwf EEDATA

bcf EECON1,EEPGD
bcf EECON1,CFGS
bsf EECON1,WREN
bcf INTCON,GIE
movlw 055h
movwf EECON2
movlw 0aah
movwf EECON2
bsf EECON1,WR
nop
bsf INTCON,GIE
bcf EECON1,WREN
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top