PIC18LF25K42芯片,采用MPLABX 的MCC找不到 EEPROM的资源,调用PIC18.H的EEPROM_READ(addr),EEPROM_WRITE(addr, value)采用XC8(1.45)编译不能通过,于是看数据手册自编 了EEPROM读写程序:
/*////////////////////////////////////////////////////////
函数名:unsigned char ReadEeprom(unsigned char address)
功 能: PIC18LF25K42 EEPROM读一字节数据 适用EEPROM小于等于256字节
输 入: 无
输 出: 返回 地址address的1字节数据
修 改:
*///////////////////////////////////////////////////////////
unsigned char ReadEeprom(unsigned char address)
{
unsigned char temp;
NVMCON1 = 0;
NVMADRL = address;
NVMCON1bits.RD = 1;
temp = NVMDAT;
return temp;
}
/*///////////////////////////////////////////////////////
函数名:void WriteEeprom(unsigned char addr ,unsigned char Data)
功 能: PIC18LF25K42 EEPROM写一字节数据 适用EEPROM小于等于256字节
输 入: addr EEPROM地址;addr EEPROM要写入的一个字节数据
输 出: 无
修 改:
*///////////////////////////////////////////////////////
void WriteEeprom(unsigned char addr ,unsigned char Data)
{
NVMCON1 = 0; // Setup Data EEPROM Access
NVMADRL = addr ; //Setup Address
//; Data Memory Value to write
NVMDAT = Data;
NVMCON1bits.WREN = 1; // enable writes
INTERRUPT_GlobalInterruptDisable(); //disable interrupts
NVMCON2 = 0x55; //unlock sequence
NVMCON2 = 0xAA; //unlock sequence
// Set WR bit to begin write
NVMCON1bits.WR = 1; //begin write
INTERRUPT_GlobalInterruptEnable(); //enable interrupts
NVMCON1bits.WREN = 0; // enable writes
} |