Platform MPLAB X
CPU PIC18F2520
Compiler XC8 v1.38
我们正在将一个项目从旧的(htc(编译器转移到MPLAB X平台,但无法访问EEPROM。
旧的编译器支持eeprom_read和eeprom_write,但支持XC8没有,好吧,有定义它们,但它们是"空的"。(xc.h包括htc.h,htc.h包括pic18.h(图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
问候
是的,正如您所发现的,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读/写(请参见第75页http://ww1.microchip.com/downloads/en/DeviceDoc/39626e.pdf用于一个特定PIC18微的EEPROM寄存器定义(。我写了以下函数,这些函数将其抽象出来,并提供与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)
}
使用MCC选择内存模块,它将创建具有所需所有功能的内存.c