1

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

    //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 编程语言(不是汇编),我在 Windows 7(32 位和 64 位)下使用 CCS C 编译器(PCWHD)(4.057),我的程序员是 PICKKit 2)。

4

0 回答 0