|
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)
|