School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,124 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,045 people online right now. Registration is fast and FREE... Join Now!




PIC16F690 writing EEPROM

 

PIC16F690 writing EEPROM, Problem writing to eeprom, WR bit not setting.

Liezel

21 Jun, 2009 - 04:41 AM
Post #1

New D.I.C Head
*

Joined: 25 Apr, 2007
Posts: 10


My Contributions
I am trying to write data to the PIC16F690 Eeprom. I use PICKit 2 and MPLAB 8.15 with the HI-TECH C compiler.
This is what I have tried first:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <pic.h>
#include <conio.h>
#include <htc.h>


__EEPROM_DATA("a", "b", "c", "d", "e", "f", "g", "h");
_XTAL_FREQ = 4000000;
TEMPREG = 0x20; //User defined register in general purpose register area

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

//Typedefs as per Coding standards
typedef unsigned char CH;


CH i,k;
v_setupRS232();

i = 0x077; // Initialize Variables

eeprom_write(0,i);
__delay_ms(20); //wait in case writing takes long


while(1) {
k = eeprom_read(0); // Retrieve the values

v_putCh(k);
}
}//end program
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


All I read back out on the screen via the RS232 interface is the 'a' that I have hard coded into the eeprom in the beginning. Thus I conclude that I can read from eeprom, but not write to it.
Next I tried to write my own writing to eeprom function and called that in my program instead of 'eeprom_read()' defined in the htc.h.
Here is the code for my function (directly as from the datasheet of the PIC).

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//function to write to eeprom
void v_write_to_eeprom(CH i1_address, CH ch_data)
{
RP0 = 0;
RP1 = 1;
EEIF = 0;
EEADR = i1_address;
EEDAT = ch_data;
RP0 = 1;
EEPGD = 0;
WREN = 1;
GIE = 0;
EECON2 = 0x55;
EECON2 = 0xAA;
WR = 1;
while(EEIF == 0)
{
continue; //wait till bit is set
}
EEIF = 0;
WREN = 0;
EEIF = 0;
GIE = 1;
RP0 = 0;
return;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

This function caused my program to hang, because the EEIF flag never got set.
I replace the
while(EEIF == 0)
{
continue; //wait till bit is set
}

with
while(WR == 1)
{
v_putCh(k); //wait till bit is set
}

This proved to me that the WR bit is not 1, because that section of code never executed.
What could cause the WR bit to not be set???? I believe that is the answer to my problem. Anyone that can help.
(PS. v_putCh() is my function to send a char via RS232 to my screen)


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 02:15PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month