![]() |
![]() |
![]() |
|
|
|||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
|
|
Thread Tools | Display Modes |
|
|
(permalink) |
|
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; 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! |
|
|
|
|
|
|
(permalink) |
|
You may be writing to the config register as you haven't cleared EECON1bits.CFGS.
Mike. |
|
|
|
|
|
|
(permalink) |
|
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. |
|
|
|
|
|
|
(permalink) |
|
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. |
|
|
|
|
|
|
(permalink) |
|
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 |
|
|
|
|
|
|
(permalink) |
|
You need to post your code if you want to get any help.
Mike. |
|
|
|
|
|
|
(permalink) |
|
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 */
}
|
|
|
|
|
|
|
(permalink) |
|
How do you know it is being lost?
Mike. |
|
|
|
|
|
|
(permalink) |
|
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. |
|
|
|
|
|
|
(permalink) |
|
Can you post your read routine?
Mike. |
|
|
|
|
|
|
(permalink) |
|
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 */
}
So you cannot fault me for what happened. Thks for the prompt response Mike/Pommie! |
|
|
|
|
|
|
(permalink) |
|
Good to hear it's all working now.
Mike. |
|
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
|
|
||||
| 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 |