写入内部EEPROM-PIC16F684时出现问题

时间:2014-09-03 08:52:45

标签: c pic eeprom

在家庭安全警报系统中,我们有一个配备PT2440的遥控器 固定编码(无跳码,无加密/解密)和中心 带有MCU的接收器系统:PIC16F684。我必须使用内部EEPROM(256字节)。 经过大量的编程和测试后,我的问题减少为:我可以写入内部 主循环之前的内存,但主循环内部的写操作 失败。这是我的主要代码

    //This function Writes data to given address in internal EEPROM of PIC MCU
    void internal_EEPROM_putc(unsigned char addr, unsigned char data)
    {
       unsigned char INTCON_SAVE;
       EEADR = addr;
       EEDATA = data;
       EEPGD = 0;            // 0 = Access data EEPROM memory
       WREN = 1;             // enable writes to internal EEPROM
       INTCON_SAVE = 0x9B;   // Save INTCON register contants
       GIE = 0;              // Disable interrupts, Next two lines SHOULD run without 
                             //  interrupts
       EECON2=0x55;
       EECON2=0xAA;
       WR = 1;    // begin write to internal EEPROM
       INTCON = INTCON_SAVE; //Now we can safely enable interrupts if previously used
       delay_cycles(1); // like NOP

       while(!WR) // Wait till write operation complete
           delay_cycles(1);
       WREN=0; // Disable writes to EEPROM on write complete (EEIF flag on set PIR2 )
    }
    // This function reads data from address given in internal EEPROM of PIC
    unsigned char internal_EEPROM_getc(unsigned char addr)
    {
       EEADR = addr;
       EEPGD= 0; // 0 = Access data EEPROM memory
       RD   = 1; // EEPROM Read
       return EEDATA;       // return data
    }

    void main()
    {
        // IT WORKS!
        internal_EEPROM_putc(0x12,0x34); //Write 0x34 to EEPROM address 0x12
        delay_cycles(1);
        c = internal_EEPROM_getc(0x12); // Read EEPROM address 0x12 in to variable C
        if (c != 0x34) { 
          RC1 = ON; // activate a relay, if read and write mismatches
          delay_ms(500);
          RC1 = OFF;
        } // there is no relay activation

        while (TRUE) {
           // IT DOESN'T WORK!
           internal_EEPROM_putc(0x13,0x56); //Write 0x34 to EEPROM address 0x12
           delay_cycles(1);
           c = internal_EEPROM_getc(0x13); // Read EEPROM address 0x12 in to variable C
           if (c != 0x56) {
             RC1 = ON;
             delay_ms(500);
             RC1 = OFF;
           } // no relay activation
        }
    }

有什么想法? 仅供参考,我使用的是C编程语言(不是汇编),我使用的是CCS C编译器(PCWHD)(4.057) 在Windows 7(32位和64位)下,我的程序员是PICKKit 2)。

0 个答案:

没有答案