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.

[SOLVED] Simple EEPROM writing and Reading in PIC18F4550

Status
Not open for further replies.

xpress_embedo

Advanced Member level 4
Joined
Jul 5, 2011
Messages
1,154
Helped
161
Reputation
396
Reaction score
189
Trophy points
1,353
Location
India
Activity points
10,591
Hello!! Yesterday i write a code for PIC16F877A for reading and writing data to EEPROM

Code:
#include<htc.h>
#include<string.h>

#define _XTAL_FREQ 20000000

void EEPROM_Write();
void EEPROM_Write_String(unsigned char [],unsigned char address);
unsigned char EEPROM_Read();

unsigned int len,i;
void main()
{
    unsigned char x;
    TRISB = 0x00;
    //Writing Operation
    EEPROM_Write_String("EMBEDDED LABORATORY!!",0);    //Message String and Starting Address
    EECON1bits.WREN = 0;    //EEPROM Write Disable
    //Reading the Data at 0x0 EEPROM Address
    EEADR = 0x0;
    x = EEPROM_Read();
    PORTB = x;
    while(1);
}   

void EEPROM_Write()
{
    EECON1bits.EEPGD = 0;    //Point to EEPROM
    EECON1bits.WREN = 1;    //EEPROM Write Enable Bit
    INTCONbits.GIE = 0;        //Disable all Interrupts
    EECON2 = 0x55;
    EECON2 = 0xAA;
    EECON1bits.WR = 1;
    INTCONbits.GIE = 1;
    while(!PIR2bits.EEIF);
    PIR2bits.EEIF = 0;
}

void EEPROM_Write_String(unsigned char msg[],unsigned char address)
{
    len = strlen(msg);
    for(i=0;i<len;i++)
    {
        EEADR = address + i;
        EEDATA = msg[i];
        EEPROM_Write(msg[i]);
    }
}

unsigned char EEPROM_Read()
{
    EECON1bits.EEPGD = 0;    //Point to EEPROM
    EECON1bits.RD = 1;        //EEPROM Read Enable Bit
    return(EEDATA);
}

It works fine...

But when today i tried the Same thing for PIC18F4550 it didn't work..
Here is my code
Code:
#include <htc.h>
#include <string.h>
#include "delay.h"

/**********CONFIGURATION BITS SETTING**********/
#pragma config FOSC = HSPLL_HS	// Using 20 MHz crystal with PLL
#pragma config PLLDIV = 5     	// Divide by 5 to provide the 96 MHz PLL with 4 MHz input
#pragma config CPUDIV = OSC1_PLL2 // Divide 96 MHz PLL output by 2 to get 48 MHz system clock
#pragma config FCMEN = OFF	 // Disable Fail-Safe Clock Monitor
#pragma config IESO = OFF  	// Disable Oscillator Switchover mode
#pragma config PWRT = OFF 	 // Disable Power-up timer
#pragma config BOR = OFF   	// Disable Brown-out reset
#pragma config WDT = OFF  	 // Disable Watchdog timer
#pragma config MCLRE = ON  // Enable MCLR Enable
#pragma config LVP = OFF   // Disable low voltage ICSP
#pragma config ICPRT = OFF // Disable dedicated programming port (only on 44-pin devices)
#pragma config CP0 = OFF   // Disable Code Protection Bit
/************************************************/

void EEPROM_Write();
void EEPROM_Write_String(unsigned char [],unsigned char address);
unsigned char EEPROM_Read();

unsigned char x,len,i;
void main() @ 0x0000A0
{
	EEPROM_Write_String("EMBEDDED LABORATORY!!!!",0);
	//Message String and Starting Address
	EECON1bits.WREN = 0;	//Disable Writing to EEPROM
	//Reading the Data at 0x0 EEPROM Address
	EEADR = 0x0;
	x = EEPROM_Read();
	while(1);	
}

void EEPROM_Write()
{
	EECON1bits.EEPGD = 0;	//Points to EEPROM
	EECON1bits.CFGS = 0;	//Points to Flash and EEPROM
	EECON1bits.WREN = 1;	//EEPROM Write Enable bit
	INTCONbits.GIE = 0;		//Disable all Interrupts
	EECON2 = 0x55;
	EECON2 = 0xAA;
	INTCONbits.GIE = 1;		//Enable Interrupts again
	while(!PIR2bits.EEIF);
        PIR2bits.EEIF = 0;
}

void EEPROM_Write_String(unsigned char msg[],unsigned char address)
{
	len = strlen(msg);
	for(i=0;i<len;i++)
	{
		EEADR = address + i;
		EEDATA = msg[i];
		EEPROM_Write(msg[i]);
	}
}

unsigned char EEPROM_Read()
{
	EECON1bits.EEPGD = 0;	//Point to EEPROM
	EECON1bits.CFGS = 0;	//Points to Flash and EEPROM
	EECON1bits.RD = 1;		//EEPROM Read Enable Bit
	return(EEDATA);
}

But this doesn't work
but after giving some delay

INTCONbits.GIE = 1; //Enable Interrupts again
while(!PIR2bits.EEIF);
PIR2bits.EEIF = 0;

replacing above with
INTCONbits.GIE = 1; //Enable Interrupts again
DelayMs(100);


Everything works fine

Pls help

Thanks in advance
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top