首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PIC18F2520 mplab x xc8 EEPROM

PIC18F2520 mplab x xc8 EEPROM
EN

Stack Overflow用户
提问于 2016-07-18 20:25:21
回答 2查看 3.6K关注 0票数 2
代码语言:javascript
复制
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中的行

代码语言:javascript
复制
#if _EEPROMSIZE > 0 && defined(_PLIB)

没有触发,而是相应的#else似乎既没有定义_EEPROMSIZE也没有定义_PLIB。

为什么xc8不支持旧版本(eeprom_read和eeprom_write)?

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

我们试着看看微芯片代码配置会做什么,但中央处理器PIC18F2520不支持MCC。

代码语言:javascript
复制
The chip do have 256 byte eeprom according to

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

问候

EN

回答 2

Stack Overflow用户

发布于 2016-07-19 14:09:40

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

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

一种选择是返回到直接寄存器操作,以执行EEPROM读/写(有关一个特定PIC18 micro的EEPROM寄存器定义,请参见http://ww1.microchip.com/downloads/en/DeviceDoc/39626e.pdf的第75页)。我已经写了下面的函数,这些函数抽象了它,并提供了与Microchip移除的功能类似的功能:

代码语言:javascript
复制
//! @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)
}
票数 1
EN

Stack Overflow用户

发布于 2017-04-01 09:20:17

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38436675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档