EEPROM.H
#pragma used+
///写单字节EEPROM
void EEPROM_write(unsigned int uiAddress, unsigned char ucData);
///读单字节EEPROM
unsigned char EEPROM_read(unsigned int uiAddress);
///写双字节EEPROM
void EEPROM_write2(unsigned int uiAddress, unsigned int ucData);
///读双字节EEPROM
unsigned int EEPROM_read2(unsigned int uiAddress);
///读4字节EEPROM
unsigned long EEPROM_read4(unsigned int uiAddress);
///写4字节EEPROM
void EEPROM_write4(unsigned int uiAddress, unsigned long ucData);
#pragma used-
#pragma library eeprom.lib
EEPROM.C
//#include "eeprom.h"
/* EEPROM Control Register */
#define EERIE 3
#define EEMWE 2
#define EEWE 1
#define EERE 0
#if funcused EEPROM_write
///写单字节EEPROM
void EEPROM_write(unsigned int uiAddress, unsigned char ucData)
{
//#asm("cli")
////* 等待上一次写操作结束
while(EECR & (1<<EEWE));
////* 设置地址和数据寄存器
EEAR = uiAddress;
EEDR = ucData;
////* 置位EEMWE
EECR |= (1<<EEMWE);
////* 置位EEWE 以启动写操作
EECR |= (1<<EEWE);
//#asm("sei")
}
#endif
#if funcused EEPROM_read
///读单字节EEPROM
unsigned char EEPROM_read(unsigned int uiAddress)
{
#asm("cli")
///* 等待上一次写操作结束
while(EECR & (1<<EEWE));
///* 设置地址寄存器*/
EEAR = uiAddress;
///* 设置EERE 以启动读操作
EECR |= (1<<EERE);
///* 自数据寄存器返回数据
return EEDR;
//#asm("sei")
}
#endif
#if funcused EEPROM_read2
///读双字节EEPROM
unsigned int EEPROM_read2(unsigned int uiAddress)
{ unsigned int ucdata;
// #asm("cli")
///* 等待上一次写操作结束
while(EECR & (1<<EEWE));
///* 设置地址寄存器*/
EEAR = uiAddress+1;
///* 设置EERE 以启动读操作
EECR |= (1<<EERE);
///* 自数据寄存器返回数据
ucdata = EEDR;
ucdata =ucdata<<8;
while(EECR & (1<<EEWE));
///* 设置地址寄存器*/
EEAR = uiAddress;
///* 设置EERE 以启动读操作
EECR |= (1<<EERE);
///* 自数据寄存器返回数据
ucdata |= EEDR;
return ucdata;
//#asm("sei")
}
#endif
#if funcused EEPROM_write2
///写双字节EEPROM
void EEPROM_write2(unsigned int uiAddress, unsigned int ucData)
{
//#asm("cli")
////* 等待上一次写操作结束
while(EECR & (1<<EEWE));
////* 设置地址和数据寄存器
EEAR = uiAddress;
EEDR = ucData & 0x00ff;
////* 置位EEMWE
EECR |= (1<<EEMWE);
////* 置位EEWE 以启动写操作
EECR |= (1<<EEWE);
while(EECR & (1<<EEWE));
////* 设置地址和数据寄存器
EEAR = uiAddress+1;
EEDR = (ucData & 0xff00)>>8;
////* 置位EEMWE
EECR |= (1<<EEMWE);
////* 置位EEWE 以启动写操作
EECR |= (1<<EEWE);
//#asm("sei")
}
#endif
#if funcused EEPROM_read4
///读4字节EEPROM
unsigned long EEPROM_read4(unsigned int uiAddress)
{ unsigned long ucdata;
// #asm("cli")
///* 等待上一次写操作结束
while(EECR & (1<<EEWE));
///* 设置地址寄存器*/
EEAR = uiAddress+3;
///* 设置EERE 以启动读操作
EECR |= (1<<EERE);
///* 自数据寄存器返回数据
ucdata = EEDR;
ucdata =ucdata<<8;
EEAR = uiAddress+2;
///* 设置EERE 以启动读操作
EECR |= (1<<EERE);
///* 自数据寄存器返回数据
ucdata |= EEDR;
ucdata =ucdata<<8;
EEAR = uiAddress+1;
///* 设置EERE 以启动读操作
EECR |= (1<<EERE);
///* 自数据寄存器返回数据
ucdata |= EEDR;
ucdata =ucdata<<8;
while(EECR & (1<<EEWE));
///* 设置地址寄存器*/
EEAR = uiAddress;
///* 设置EERE 以启动读操作
EECR |= (1<<EERE);
///* 自数据寄存器返回数据
ucdata |= EEDR;
return ucdata;
//#asm("sei")
}
#endif
#if funcused EEPROM_write4
///写4字节EEPROM
void EEPROM_write4(unsigned int uiAddress, unsigned long ucData)
{
//#asm("cli")
////* 等待上一次写操作结束
while(EECR & (1<<EEWE));
////* 设置地址和数据寄存器
EEAR = uiAddress;
EEDR = ucData & 0x000000ff;
////* 置位EEMWE
EECR |= (1<<EEMWE);
////* 置位EEWE 以启动写操作
EECR |= (1<<EEWE);
ucData = ucData>>8;
while(EECR & (1<<EEWE));
////* 设置地址和数据寄存器
EEAR = uiAddress+1;
EEDR = ucData & 0x000000ff;
////* 置位EEMWE
EECR |= (1<<EEMWE);
////* 置位EEWE 以启动写操作
EECR |= (1<<EEWE);
ucData = ucData>>8;
while(EECR & (1<<EEWE));
////* 设置地址和数据寄存器
EEAR = uiAddress+2;
EEDR = ucData & 0x000000ff;
////* 置位EEMWE
EECR |= (1<<EEMWE);
////* 置位EEWE 以启动写操作
EECR |= (1<<EEWE);
ucData = ucData>>8;
while(EECR & (1<<EEWE));
////* 设置地址和数据寄存器
EEAR = uiAddress+3;
EEDR = ucData & 0x000000ff;
////* 置位EEMWE
EECR |= (1<<EEMWE);
////* 置位EEWE 以启动写操作
EECR |= (1<<EEWE);
//#asm("sei")
}
#endif
https://static.assets-stash.eet-china.com/album/old-resources/2009/3/18/6335faa5-ff40-4952-a760-04547102a0f7.rar
用户588864 2011-5-11 13:59