2014-11-15 165 views
0

我正试图与PIC24F16KA101 MCU的内部存储器进行交互。在阅读数据表和本网站的讨论(提供了一个非常有用的示例代码)之后used in the project 现在,如果我把代码放在程序下面工作就好了,因为我能够成功地读取与我之前编写的相同的值。但是,如果在写入之后,我拔出MCU并仅执行一次EEPROOM的读取,则不会返回写入的值。这里可能是什么问题?为什么我可以写入然后成功读取,但在关闭电源后无法读取? 在此先感谢所有帮助 达米安读取/写入内部EEPROM时遇到问题PIC24F16KA101

int __attribute__ ((space(eedata))) ee_addr; 
void EepSetup(); 
void EepErase(void); 
int EepWrite(int index, int data); 
int EepRead(int index); 

int main(int argc, char** argv) 
{ 
    unsigned int data = 123; 
    unsigned int data_read = 0; 

    Init_UART1(); 
    UART1WriteString("START EEPROM PROGRAM \n"); 
    EepSetup(); 
    UART1WriteString("WRITING DATA TO MEMORY \n"); 
    EepWrite(1,data); 

    //if the code works, just comment the upper section and read eeprom after 
    //disconecting the power source 
    UART1WriteString("READING DATA FROM MEMORY \n"); 
    data_read = EepRead(1); 
    UART1WriteString("Value Read: "); 
    UART1WriteInt(data_read,16); 
    UART1WriteString("\n"); 
    __delay_ms(1000); 
    return (EXIT_SUCCESS); 
} 
void EepSetup(){ 
    //Disable Interrupts For 5 instructions 
    asm volatile("disi #5"); 
    //Issue Unlock Sequence 
    asm volatile("mov #0x55, W0 \n" 
    "mov W0, NVMKEY \n" 
    "mov #0xAA, W1 \n" 
    "mov W1, NVMKEY \n"); 
} 
void EepErase(void) { 
    NVMCON = 0x4050;   // Set up NVMCON to bulk erase the data EEPROM 
    asm volatile ("disi #5"); // Disable Interrupts For 5 Instructions 
    __builtin_write_NVM();  // Issue Unlock Sequence and Start Erase Cycle 
    while(_WR) 
    ; 
} 

int EepRead(int index){ 
    unsigned int offset; 

    TBLPAG = __builtin_tblpage(&ee_addr); // Initialize EE Data page pointer 
    offset = __builtin_tbloffset(&ee_addr); // Initizlize lower word of address 
    offset += index * sizeof(int); 
    return __builtin_tblrdl(offset); // read EEPROM data 
} 

int EepWrite(int index, int data){ 
    unsigned int offset; 
    NVMCON = 0x4004; // Set up NVMCON to erase one word of data EEPROM 
    TBLPAG = __builtin_tblpage(&ee_addr); // Initialize EE Data page pointer 
    offset = __builtin_tbloffset(&ee_addr); // Initizlize lower word of address 
    offset += index * sizeof(int); 
    __builtin_tblwtl(offset, data); 
    asm volatile ("disi #5"); // Disable Interrupts For 5 Instructions 
    __builtin_write_NVM();  // Issue Unlock Sequence and Start Erase Cycle 
    while(_WR); 
    return (EXIT_SUCCESS); 
} 

回答

0

我只是想出了什么问题了,它发生,如果你使用PICkit 3 MPLABX你必须检查的程序员一个选项,以保护EEPROM存储器,所以代码功能正常,您只需在编程器设置中检查“保留EEPROM存储器”选项即可。我希望这可以帮助别人。

干杯,达米安