【问题标题】:PIC18F2520 mplab x xc8 EEPROMPIC18F2520 mplab x xc8 EEPROM
【发布时间】:2016-07-18 12:25:21
【问题描述】:
Platform MPLAB X
CPU PIC18F2520
Compiler XC8 v1.38

我们正在将一个项目从旧的 (htc) 编译器迁移到 MPLAB X 平台,但无法访问 EEPROM。

旧的编译器支持 eeprom_read 和 eeprom_write 但 XC8 没有,好吧,有定义它们的定义,但它们是“空的”。 (xc.h 包括 htc.h,其中包括 pic18.h) 在 pic.h 行

#if _EEPROMSIZE > 0 && defined(_PLIB)

不是触发而是对应的#else _EEPROMSIZE 和 _PLIB 似乎都没有定义。

为什么这么老(eeprom_read和eeprom_write) xc8 不支持?

我们应该怎么做才能访问 EEPROM?

我们试图看看 Microchip Code Configure 会做什么, 但是MCC不支持CPU PIC18F2520。

The chip do have 256 byte eeprom according to

http://ww1.microchip.com/downloads/en/DeviceDoc/39631E.pdf

问候

【问题讨论】:

    标签: pic mplab eeprom


    【解决方案1】:

    是的,正如您所发现的,Microchip 已在 MPLAB X IDE 中删除了 PIC18 系列微控制器的 EEPROM 读/写功能。它将宏保留为空壳(EEPROM_READ()eeprom_read() 等),不会引发编译器错误/警告,但实际上不会做任何事情

    pic18.h 建议如果#if _EEPROMSIZE > 0 && defined(_PLIB) 为真,那么将填充宏,但是通过手动添加PLIB 宏定义,编译器会抱怨无法找到plib.h。我找不到合适的地方下载“plib”。

    一种选择是恢复为直接寄存器操作以执行 EEPROM 读/写(有关特定 PIC18 微控制器的 EEPROM 寄存器定义,请参见 http://ww1.microchip.com/downloads/en/DeviceDoc/39626e.pdf 的第 75 页)。我编写了以下函数,这些函数将其抽象出来并提供与 Microchip 删除的类似的功能:

    //! @brief      Reads a single byte of data from the EEPROM.
    //! @param      address     The EEPROM address to write the data to (note that not all
    //!                         16-bits of this variable may be supported).
    //! @returns    The byte of data read from EEPROM.
    //! @warning    This function does not return until read operation is complete.
    uint8_t Eeprom_ReadByte(uint16_t address)
    {
    
        // Set address registers
        EEADRH = (uint8_t)(address >> 8);
        EEADR = (uint8_t)address;
    
        EECON1bits.EEPGD = 0;       // Select EEPROM Data Memory
        EECON1bits.CFGS = 0;        // Access flash/EEPROM NOT config. registers
        EECON1bits.RD = 1;          // Start a read cycle
    
        // A read should only take one cycle, and then the hardware will clear
        // the RD bit
        while(EECON1bits.RD == 1);
    
        return EEDATA;              // Return data
    
    }
    
    //! @brief      Writes a single byte of data to the EEPROM.
    //! @param      address     The EEPROM address to write the data to (note that not all
    //!                         16-bits of this variable may be supported).
    //! @param      data        The data to write to EEPROM.
    //! @warning    This function does not return until write operation is complete.
    void Eeprom_WriteByte(uint16_t address, uint8_t data)
    {    
        // Set address registers
        EEADRH = (uint8_t)(address >> 8);
        EEADR = (uint8_t)address;
    
        EEDATA = data;          // Write data we want to write to SFR
        EECON1bits.EEPGD = 0;   // Select EEPROM data memory
        EECON1bits.CFGS = 0;    // Access flash/EEPROM NOT config. registers
        EECON1bits.WREN = 1;    // Enable writing of EEPROM (this is disabled again after the write completes)
    
        // The next three lines of code perform the required operations to
        // initiate a EEPROM write
        EECON2 = 0x55;          // Part of required sequence for write to internal EEPROM
        EECON2 = 0xAA;          // Part of required sequence for write to internal EEPROM
        EECON1bits.WR = 1;      // Part of required sequence for write to internal EEPROM
    
        // Loop until write operation is complete
        while(PIR2bits.EEIF == 0)
        {
            continue;   // Do nothing, are just waiting
        }
    
        PIR2bits.EEIF = 0;      //Clearing EEIF bit (this MUST be cleared in software after each write)
        EECON1bits.WREN = 0;    // Disable write (for safety, it is re-enabled next time a EEPROM write is performed)
    }
    

    【讨论】:

    • 是的,我做了同样的评论,Microchip 确实有一个宏,_LOAD_EEADR 可以用来代替 EEADRH 和 EEADR(我猜它与 CPU 无关),因为在我们那里的 PIC 上没有诸如寄存器,只有 EEADR
    • EEPROM_READ 宏在我选择了具有 EEPROM 的设备并且包含 时起作用。默认填充宏仍然有效,即 __EEPROM_DATA ( 0x00, 0x0A, 0x00, 0x00, 0x04, 0xFF, 0xFE, 0xE9 );
    【解决方案2】:

    使用 MCC 选择内存模块,它将创建具有所有所需功能的 memory.c

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多