CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

16F877 internal EEPROM can't write/read

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
David Lu
Guest







16F877 internal EEPROM can't write/read
PostPosted: Wed Feb 26, 2003 7:49 pm     Reply with quote

Hi,All:
I am trying to use C language to write and read internal EEPROM on my 16f877, but it can not working.
my program is:
void write_eeprom ( char addr, char data ) // Write eeprom
{
while ( EECON1 & WR); // check completion of last write
EEADRH=0x21;
EEADR = addr; // set address register
EEDATA = data; // set data register
clear_bit ( EECON1, EEPGD );
set_bit( EECON1, WREN );
EECON2 = 0x55;
EECON2 = 0xAA;
set_bit( EECON1, WR ); // write command
clear_bit( EECON1, WREN ); // inhibit further writing
}

char read_eeprom ( char addr ) // Read eeprom
{ EEADRH=0x21;
EEADR = addr; // set address register
clear_bit (EECON1, EEPGD );
set_bit(EECON1, RD ); // read command
return (EEDATA);
}
Is there any special setting for MPLAB ?

Thanks!

David
___________________________
This message was ported from CCS's old forum
Original Post ID: 12151
ajt



Joined: 07 Sep 2003
Posts: 110

View user's profile Send private message

Re: 16F877 internal EEPROM can't write/read
PostPosted: Wed Feb 26, 2003 7:58 pm     Reply with quote

Why not use the built in read and write EEPROM functions provided in the CCD compiler? All you are trying to do is embodied in them and they work fine.

:=Hi,All:
:= I am trying to use C language to write and read internal EEPROM on my 16f877, but it can not working.
:= my program is:
:=void write_eeprom ( char addr, char data ) // Write eeprom
:={
:= while ( EECON1 & WR); // check completion of last write
:= EEADRH=0x21;
:= EEADR = addr; // set address register
:= EEDATA = data; // set data register
:= clear_bit ( EECON1, EEPGD );
:= set_bit( EECON1, WREN );
:= EECON2 = 0x55;
:= EECON2 = 0xAA;
:= set_bit( EECON1, WR ); // write command
:= clear_bit( EECON1, WREN ); // inhibit further writing
:=}
:=
:=char read_eeprom ( char addr ) // Read eeprom
:={ EEADRH=0x21;
:= EEADR = addr; // set address register
:= clear_bit (EECON1, EEPGD );
:= set_bit(EECON1, RD ); // read command
:= return (EEDATA);
:=}
:= Is there any special setting for MPLAB ?
:=
:=Thanks!
:=
:=David
___________________________
This message was ported from CCS's old forum
Original Post ID: 12152
_________________
Al Testani
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: 16F877 internal EEPROM can't write/read
PostPosted: Wed Feb 26, 2003 8:48 pm     Reply with quote

:= while ( EECON1 & WR); // check completion of last
This line is probably incorrect. You probably have WR defined as a bit position in which case it should be

while (bit_test(EECON1, WR));

Also since you are not writing to the program memory, the following line is not needed:
:= EEADRH=0x21;

Microchip also recommends disabling global ints before writing 0x55 and 0xAA to EECON2 and reenabling them after startin the write operation. If they suggest it, they probably have found the likelyhood of problems if you don't.

Mark

:=Hi,All:
:= I am trying to use C language to write and read internal EEPROM on my 16f877, but it can not working.
:= my program is:
:=void write_eeprom ( char addr, char data ) // Write eeprom
:={
:= while ( EECON1 & WR); // check completion of last write
:= EEADRH=0x21;
:= EEADR = addr; // set address register
:= EEDATA = data; // set data register
:= clear_bit ( EECON1, EEPGD );
:= set_bit( EECON1, WREN );
:= EECON2 = 0x55;
:= EECON2 = 0xAA;
:= set_bit( EECON1, WR ); // write command
:= clear_bit( EECON1, WREN ); // inhibit further writing
:=}
:=
:=char read_eeprom ( char addr ) // Read eeprom
:={ EEADRH=0x21;
:= EEADR = addr; // set address register
:= clear_bit (EECON1, EEPGD );
:= set_bit(EECON1, RD ); // read command
:= return (EEDATA);
:=}
:= Is there any special setting for MPLAB ?
:=
:=Thanks!
:=
:=David
___________________________
This message was ported from CCS's old forum
Original Post ID: 12154
David Lu
Guest







Thanks Mark
PostPosted: Wed Feb 26, 2003 9:04 pm     Reply with quote

Mark:
Thanks!
I will try it tomorrow in company.
David
___________________________
This message was ported from CCS's old forum
Original Post ID: 12155
David Lu
Guest







16F877 internal EEPROM still can't write/read
PostPosted: Thu Feb 27, 2003 7:42 pm     Reply with quote

Hi,Mark:
I try it today,but still not working.
I use MPLAB IDE , I try to change some items of ICD Options,and put high voltage on MCLR/Vpp(Pin 1 of 16F877),but
when I check the EEPROM (in EEPROM Window), each unit is still 0xFF, EECON1 and EECON2 does not changed.
I want get some help to solve my problem.
Thanks
David
___________________________
This message was ported from CCS's old forum
Original Post ID: 12197
Thomas Hartmann
Guest







Re: 16F877 internal EEPROM can't write/read
PostPosted: Fri Feb 28, 2003 3:04 am     Reply with quote

Hi David,

the same problem you have has const me 2 days of debugging, this week.
I use a 16F876 with the in circuit debugger "ICD" from Microchip + MPLAB 5.0.

This is how I solved the problem:

1. MPLAB does NOT automatically update the EEPROM Window.
You have to update it manually. Since I use the ICD I have a window open to control the ICD. There I have to set a tick to a box called "EEPROM Data" and then click a button called "Read".

2. I precicely followed the instructions from the 16F876's data sheet on writing the EEPROM.
I also watched the EEDATA- and the EEADR-registers to see if my data and the destination address was loaded at all.
After knowing they were loaded into these registers I focused on why it was not written etc. Step by step.

For your reference, I will attach my ( working! ) code

:-)
Thomas


------------ < CODE > -------------------------------
// liest Daten aus dem EEprom
//#inline
void read_eeprom_data( unsigned int8 un8Start, unsigned int8 un8Length, unsigned int8* pun8Dest )
{
unsigned int8 i;

// disable interrupts
disableInterrupts_Timer1_RS232();

// set destination to "EEPROM" by clearing EEPGD in EECON1
EECON1 &= ~EECON1_EEPGD;

// read data
for( i=0; i<un8Length; i++)
{
// write address to register
// compiler will select bank 2
EEADR = un8Start + i ; // set destination address (EEPROM)

// read NOW !
EECON1 |= EECON1_RD;

// get data from register
*(pun8Dest + i) = EEDATA;
}

// enable interrupts
enableAllInterrupts();
}



// write data to EEprom
// see also data sheet of the PIC16F873A, 3.4 Writing to Data EEPROM Memory
//#inline
void write_eeprom_data( unsigned int8 un8Start, unsigned int8 un8Length, unsigned int8* pun8Src )
{
unsigned int8 i;
unsigned int8 debugData; // DEBUG

// set destination to "EEPROM" by clearing EEPGD in EECON1
EECON1 &= ~EECON1_EEPGD;
// enable writing of EEPROM -> set WREN bit in EECON1 register
EECON1 |= EECON1_WREN;

// write data
for( i=0; i<un8Length; i++)
//for( i=0; i<10; i++)
{
// wait until previous write is complete
while( EECON1 & EECON1_WR )
{
// simply don't nothing. Should not take longer than typ.4msec, max.8ms ( data sheet )
}

// disable interrupts
disableAllInterrupts(); // prevent writing of rubbish

// write address and data to registers
// compiler will select bank 2
EEADR = un8Start + i ; // set destination address (EEPROM)
EEDATA = *(pun8Src + i); // set data that has to be written

// clear write error flag
EECON1 &= ~EECON1_WRERR;

// magic write sequence
EECON2 = 0x55; // special command 1
EECON2 = 0xAA; // special command 2
EECON1 |= EECON1_WR; // set WR bit => WRITE!

// enable interrupts
enableAllInterrupts(); // interrupts may be enabled until we are ready to write the next byte
}

// disable ( accidental ) writing of EEPROM -> clear WREN bit in EECON1 register
EECON1 &= ~EECON1_WREN;
// set destination back to "PROGRAM MEMORY" by setting EEPGD in EECON1
EECON1 |= EECON1_EEPGD;

// clear interrupt flag
PIR2 &= ~PIR2_EEIF;
}
----------------< /CODE >----------------------------







:=Hi,All:
:= I am trying to use C language to write and read internal EEPROM on my 16f877, but it can not working.
:= my program is:
:=void write_eeprom ( char addr, char data ) // Write eeprom
:={
:= while ( EECON1 & WR); // check completion of last write
:= EEADRH=0x21;
:= EEADR = addr; // set address register
:= EEDATA = data; // set data register
:= clear_bit ( EECON1, EEPGD );
:= set_bit( EECON1, WREN );
:= EECON2 = 0x55;
:= EECON2 = 0xAA;
:= set_bit( EECON1, WR ); // write command
:= clear_bit( EECON1, WREN ); // inhibit further writing
:=}
:=
:=char read_eeprom ( char addr ) // Read eeprom
:={ EEADRH=0x21;
:= EEADR = addr; // set address register
:= clear_bit (EECON1, EEPGD );
:= set_bit(EECON1, RD ); // read command
:= return (EEDATA);
:=}
:= Is there any special setting for MPLAB ?
:=
:=Thanks!
:=
:=David
___________________________
This message was ported from CCS's old forum
Original Post ID: 12207
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group