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.

PIC16F690 EEPROM Problem

Status
Not open for further replies.

Alpha Beta

Newbie level 2
Joined
Mar 30, 2011
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,299
Hey All,

I'm running into a problem while trying to use the EEPROM on the PIC16F690. I'm using MPLAB IDE v8.36 and a PICKIT 2. I stripped the code down to its essentials to see if I could get that to work. My goal is to be able to have a write function based on an external input that I can read later, even if power is lost at some point.

This test code writes a value (in this case, 0x03) to an address (0x11), reads it into a variable (test), then flashes an LED to show what number was read. When I include both the write and read functions, the LED flashes 3 times, which is expected. However, when I comment out the write function, the read function gets a value of FF and flashes 255 times. I looked under View > EEPROM and it looks like the 0x03 isn't getting stored in the first place.

My questions:
-If the 0x03 isn't getting stored, why does the read function work when the write is not commented out?
-How do I get the write function to correctly store a value?

Any ideas?



Code:
#include <htc.h>

#define LED 	RA2

void delay(int);
void eeWrite(char address, char data);
char eeRead(char address);

__CONFIG(INTIO & WDTDIS & PWRTDIS & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS & IESODIS & FCMDIS);

int main(int argc, char *argv[])
{
	ANSEL = 0;
	ANSELH = 0;
	TRISA = 0x30;	//--110000 RA5:RA4 are inputs, RA3:RA0 are outputs
	PORTA = 0;
	int test;
	
	eeWrite(0x11, 0x03); //use first, comment second
	test = eeRead(0x11);

	for(int i = 0; i < test; i++)
	{
		LED = 1;
		delay(500);
		LED = 0;
		delay(500);
	}
	
	while(1) {}
	
	return 0;
}
void delay(int time)
{
	int t=time;
	int i=0;
	while (t>0)
	{
		while(i<10)
		{
			asm("nop");
			i++;
		}
		i = 0;
		t--;
	}
}

void eeWrite(char address, char data)
{
	char INTCON_SAVE;

	EEADR  = address;
	EEDATA = data;
	EEPGD = 0;				// 0 = Access data EEPROM memory
	WREN = 1;				// enable writes to internal EEPROM
	INTCON_SAVE=INTCON;		// Save INTCON register contants
	INTCON = 0;				// Disable interrupts, Next two lines SHOULD run without interrupts
	
	EECON2=0x55;        	// Required sequence for write to internal EEPROM
	EECON2=0xaa;        	// Required sequence for write to internal EEPROM
	
	WR = 1;					// begin write to internal EEPROM
	INTCON=INTCON_SAVE; 	// Now we can safely enable interrupts if previously used
	
	asm("nop");
	
	while(EEIF==0) {asm("nop");}	//wait until write operation is complete
	
	WREN = 0;				// Disable writes to EEPROM on write complete
	EEIF = 0;				//Clear EEPROM write complete flag. (must be cleared in software. So we do it here)
}

// This function reads data from address given in internal EEPROM of PIC 
char eeRead(char address)
{
    EEADR = address;
    EEPGD = 0; 				// 0 = Access data EEPROM memory
    RD = 1;					// EEPROM Read
    return EEDATA;			// return data
}
 

I think that when you comment out the write function and reprogram, the programmer clears the eeprom memmory back to ff.

You should have an option to preserve eeprom somewhere?

Also, if you are using Hi-Tech, there are library routines for reading and writing eeprom and flash so you dont have to write your own.

This is defined in <pic.h>

/* library function versions */
extern void eeprom_write(unsigned char addr, unsigned char value);
extern unsigned char eeprom_read(unsigned char addr);
#endif // end EEPROM routines.

You will find "eeprom_routines.h" in the Hi-Tech include directory.
 

Thanks for your help. I added in the predefined functions, and you were right about the programming clearing the EEPROM. To fix this I just used another storage space to see if the chip had already been written or not.

For anyone who wants a working example, here you go:

Code:
#include <htc.h>

#define LED 	RA2

void delay(int);
extern void eeprom_write(unsigned char addr, unsigned char value);
extern unsigned char eeprom_read(unsigned char addr);

__CONFIG(INTIO & WDTDIS & PWRTDIS & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS & IESODIS & FCMDIS);

int main(int argc, char *argv[])
{
	ANSEL = 0;
	ANSELH = 0;
	TRISA = 0x30;	//--110000 RA5:RA4 are inputs, RA3:RA0 are outputs
	PORTA = 0;
	int test;
	
	if(eeprom_read(0x12) == 0xFF)
	{
		test = 1;
		eeprom_write(0x12, 0x01);
		eeprom_write(0x11, 0x03);
	}
	else
		test = eeprom_read(0x11);

	for(int i = 0; i < test; i++)
	{
		LED = 1;
		delay(500);
		LED = 0;
		delay(500);
	}
	
	while(1) {}
	
	return 0;
}
void delay(int time)
{
	int t=time;
	int i=0;
	while (t>0)
	{
		while(i<10)
		{
			asm("nop");
			i++;
		}
		i = 0;
		t--;
	}
}
 

Good to see you have got it working.
One point is that you do not need to write the eeprom function prototypes,
they are included when you include <htc.h>
If you look at htc.h, you will see it includes pic.h.
If you look in pic.h, you will see it includes eeprom_routines.h and other useful include files.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top