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.

EEPROM write& read in pic16f877a

Status
Not open for further replies.

svinoth86

Newbie level 6
Joined
Nov 13, 2010
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,420
how to write in eeprom and read in pic16f877a......?

---------- Post added at 15:06 ---------- Previous post was at 15:05 ----------

#include<htc.h>

/////////////////////////////////////////lcd.c//////////////////////////////////////////////
#define rs RB1
#define rw RB2
#define en RB3
delay(unsigned char d)
{
while(d--);
}
lcd_data(unsigned char c)
{
PORTD=c;
rs=1;
rw=0;
en=1;
delay(50);
en=0;
}

lcd_cmd(unsigned char c)
{
PORTD=c;
rs=0;
rw=0;
en=1;
delay(50);
en=0;
}
lcd_str(unsigned char *s)
{
while(*s)
{
lcd_data(*s);
s++;
}
}

lcd_init()
{
lcd_cmd(0x38);
lcd_cmd(0x0c);
lcd_cmd(0x80);

}
////////////////////////////////////////////////////////////////////////////////////////////

rom_read()
{
unsigned char a;
EEADR=0x0f;//adddress location
EEPGD=0;//point eeprom
RD=1;//to start rread operation
while(RD==1);//wait to complete read operation
a=EEDATA;//data is in EEDATA
return a;
}
void rom_write()
{
//////EECON1reg=EEPGD — — — WRERR WREN WR RD/////
EEADR=0x0f;//adddress location
EEDATA=0x42;//data
EEPGD=0;//point eeprom
WREN=1;//enable program operation
//EEIF=0;
EECON2=0x55;
EECON2=0xAA;
WR=1;
WREN=0;//disable program operation
while(EEIF==0);//wait to complete write
EEIF=0;
}

//////////////////////////////serial communication//////////////////////////////////////////
sr_tx(unsigned char t)
{
TXREG=t;
while(!TXIF);
TXIF=0;
}
sr_init()
{
TXSTA=0x24; //CSRC TX9 TXEN SYNC — BRGH TRMT TX9D
RCSTA=0x90;//SPEN RX9 SREN CREN ADDEN FERR OERR RX9D
SPBRG=0x19;//BR =9600b/s, Baud Rate = {FOSC(4MHZ)/(16 (X + 1))}
}
////////////////////////////////////////////////////////////////////////////////////////////


/*void convert(unsigned char e) // to get hex value
{
if(e<10)
lcd_data(e+0x30);
else
lcd_data(e+0x31);
}*/


void main()
{
// unsigned char a,b=16,c,y;
unsigned char a;
sr_init();


TRISB=0x00;
PORTB=0x00;
TRISD=0x00;
PORTD=0x00;
lcd_init();
// lcd_data('a');

while(1)
{
rom_write();//to write data to eeprom
a=rom_read();
lcd_data(a);
sr_tx(a);
/*c=a/b;
y=a%b;
convert(c);
convert(y);*/

//// strore the readed value to other location/////

/*EEADR=0x12;
EEDATA=a;
EEPGD=0;
WREN=1;
//EEIF=0;
EECON2=0x55;
EECON2=0xAA;
WR=1;
WREN=0;
while(EEIF==0);
EEIF=0; */


//lcd_data('b');
while(1);
}

}
 

Hi

I do not use C, only assembler. So I can not tell if your code is correct, but if you check page 37 of the datasheet for the 16f877 it explains the sequence of events and any special actions that need to be performed including disabling the interrupts etc.

HTH
 
Looks like you are using the Hi-Tech compiler?
Depending what version you are using, if you look in the include directory at the 'pic.h' file or the 'eeprom_routines.h' file, you will find that there are library functions for reading and writing eeprom with the prototypes defined in the header files.
 

Hi friend,

My EEPROM read and write function is below for pic16f877a
Compiler == > MicroC

void WriteEEPROM(char Addre, char DataE)
{
while(EECON1.WR == 1);
EEADR = Addre;
EEDATA = DataE;
EECON1.EEPGD = 0;
EECON1.WREN = 1;
EECON2 = 0x55;
EECON2 = 0xAA;
EECON1.WR = 1;
EECON1.WREN = 0;
}

char ReadEEPROM(char addr)
{
EEADR = addr;
EECON1.EEPGD = 0;
EECON1.RD = 1;
asm nop; asm nop;
return EEDATA;
}

it is work fine for me.

Hope this is help you.
Shyam
INDIA
 

hi friend i need the circuit connection for this program.........
thanku in advance

---------- Post added at 14:14 ---------- Previous post was at 14:12 ----------

hi friend i need the connection diagram or circuit diagram for this program....

---------- Post added at 14:15 ---------- Previous post was at 14:14 ----------

how to write in eeprom and read in pic16f877a......?

---------- Post added at 15:06 ---------- Previous post was at 15:05 ----------

#include<htc.h>

/////////////////////////////////////////lcd.c//////////////////////////////////////////////
#define rs RB1
#define rw RB2
#define en RB3
delay(unsigned char d)
{
while(d--);
}
lcd_data(unsigned char c)
{
PORTD=c;
rs=1;
rw=0;
en=1;
delay(50);
en=0;
}

lcd_cmd(unsigned char c)
{
PORTD=c;
rs=0;
rw=0;
en=1;
delay(50);
en=0;
}
lcd_str(unsigned char *s)
{
while(*s)
{
lcd_data(*s);
s++;
}
}

lcd_init()
{
lcd_cmd(0x38);
lcd_cmd(0x0c);
lcd_cmd(0x80);

}
////////////////////////////////////////////////////////////////////////////////////////////

rom_read()
{
unsigned char a;
EEADR=0x0f;//adddress location
EEPGD=0;//point eeprom
RD=1;//to start rread operation
while(RD==1);//wait to complete read operation
a=EEDATA;//data is in EEDATA
return a;
}
void rom_write()
{
//////EECON1reg=EEPGD — — — WRERR WREN WR RD/////
EEADR=0x0f;//adddress location
EEDATA=0x42;//data
EEPGD=0;//point eeprom
WREN=1;//enable program operation
//EEIF=0;
EECON2=0x55;
EECON2=0xAA;
WR=1;
WREN=0;//disable program operation
while(EEIF==0);//wait to complete write
EEIF=0;
}

//////////////////////////////serial communication//////////////////////////////////////////
sr_tx(unsigned char t)
{
TXREG=t;
while(!TXIF);
TXIF=0;
}
sr_init()
{
TXSTA=0x24; //CSRC TX9 TXEN SYNC — BRGH TRMT TX9D
RCSTA=0x90;//SPEN RX9 SREN CREN ADDEN FERR OERR RX9D
SPBRG=0x19;//BR =9600b/s, Baud Rate = {FOSC(4MHZ)/(16 (X + 1))}
}
////////////////////////////////////////////////////////////////////////////////////////////


/*void convert(unsigned char e) // to get hex value
{
if(e<10)
lcd_data(e+0x30);
else
lcd_data(e+0x31);
}*/


void main()
{
// unsigned char a,b=16,c,y;
unsigned char a;
sr_init();


TRISB=0x00;
PORTB=0x00;
TRISD=0x00;
PORTD=0x00;
lcd_init();
// lcd_data('a');

while(1)
{
rom_write();//to write data to eeprom
a=rom_read();
lcd_data(a);
sr_tx(a);
/*c=a/b;
y=a%b;
convert(c);
convert(y);*/

//// strore the readed value to other location/////

/*EEADR=0x12;
EEDATA=a;
EEPGD=0;
WREN=1;
//EEIF=0;
EECON2=0x55;
EECON2=0xAA;
WR=1;
WREN=0;
while(EEIF==0);
EEIF=0; */


//lcd_data('b');
while(1);
}

}



hi friend i need the connection diagram or circuit diagram for this program.... thanks in advance
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top