初级会员

- 积分
- 65
- 金钱
- 65
- 注册时间
- 2012-7-6
- 在线时间
- 5 小时
|
发表于 2012-7-20 15:21:56
|
显示全部楼层
#define E2_END E2END
#define _EEPROM_TYPE_ __attribute__((section(".eeprom")))
#define EEPROM_BLANK_BYTE 0xFF
//_____ M A C R O S ____________________________________________________________
//--- Eeprom basic functions
U16 _EPROvertimer ; //= (F_CPU/4e3/10)*100; //100ms超时
#define Eeprom_prog_completed {\
_EPROvertimer = (F_CPU/4e3/10)*100; \
while ((EECR & (1<<EEWE))&&(_EPROvertimer > 0)){ \
_EPROvertimer --; \
} \
}
//--- Eeprom functions
#define Eeprom_rd_byte(addr) \
({ \
U8 result; \
asm \
( \
"movw r30, %1" "\n\t" \
"out %2, r30" "\n\t" \
"out %3, r31" "\n\t" \
"sbi %4, %5" "\n\t" \
"in %0, %6" "\n\t" \
: "=r" (result) \
: "r" ((U16)addr), \
"I" (_SFR_IO_ADDR(EEARL)), \
"I" (_SFR_IO_ADDR(EEARH)), \
"I" (_SFR_IO_ADDR(EECR)), \
"M" (EERE), \
"I" (_SFR_IO_ADDR(EEDR)) \
: "r30", "r31" \
); \
result; \
})
#define Eeprom_wr_byte(addr, data) \
({ \
asm \
( \
"movw r30, %0" "\n\t" \
"out %2, r30" "\n\t" \
"out %3, r31" "\n\t" \
"out %4, %1" "\n\t" \
"sbi %5, %6" "\n\t" \
"sbi %5, %7" "\n\t" \
: \
: "r" ((U16)addr), \
"r" (data), \
"I" (_SFR_IO_ADDR(EEARL)), \
"I" (_SFR_IO_ADDR(EEARH)), \
"I" (_SFR_IO_ADDR(EEDR)), \
"I" (_SFR_IO_ADDR(EECR)), \
"M" (EEMWE), \
"M" (EEWE) \
: "r30", "r31" \
); \
})
//------------------------------------------------------------------------------
//_____ D E C L A R A T I O N S ________________________________________________
// @fn eeprom_rd_byte
//!
//! This function allows to read a byte in up to 64K bytes of Eeprom memory.
//!
//! @warning
//!
//! @param: addr_byte Address to read the byte in Eeprom memory.
//!
//! @return: Value read in the Eeprom memory.
//!
extern U8 eeprom_rd_byte(U16 addr_byte);
//------------------------------------------------------------------------------
// @fn eeprom_wr_byte
//!
//! This function allows to write a byte in up to 64K of Eeprom memory.
//!
//! @warning
//!
//! @param: addr_byte Address in Eeprom memory to write the byte.
//! value Value to write in the Eeprom memory.
//!
//! @return none
//!
extern void eeprom_wr_byte(U16 addr_byte, U8 value);
//------------------------------------------------------------------------------
// @fn eeprom_rd_block
//!
//! This function allows to read up to 65535 bytes (64K Bytes-1 byte) of
//! the Eeprom memory.
//!
//! @warning: 1 - This function isn't able to address the fully 64K bytes
//! but we cannot find in the device a source buffer up to
//! 64K bytes.
//!
//! @param: src Start address in Eeprom memory where write data.
//! *dest Address of data to write.
//! n number of byte to write.
//!
//! @return none
//!
extern void eeprom_rd_block(U16 src, U8* dest, U16 n);
//------------------------------------------------------------------------------
// @fn eeprom_wr_block
//!
//! This function allows to write up to 65535 bytes (64K Bytes-1 byte) in
//! the Eeprom memory.
//!
//! @warning: 1 - This function isn't able to address the fully 64K bytes
//! but we cannot find in the device a source buffer up to
//! 64K bytes.
//!
//! @param: *src Address of data to write.
//! dest Start address in Eeprom memory where write data.
//! n number of byte to write.
//!
//! @return none
//!
extern void eeprom_wr_block(U8* src, U16 dest, U16 n);
//------------------------------------------------------------------------------
// @fn eeprom_full_erase
//!
//! This function erases the whole Eeprom memory.
//!
//! @warning none
//!
//! @param none
//!
//! @return none
//!
extern void eeprom_full_erase(void);
.c
//_____ F U N C T I O N S ______________________________________________________
//------------------------------------------------------------------------------
// @fn eeprom_rd_byte
//!
//! This function allows to read a byte in up to 64K bytes of Eeprom memory.
//!
//! @warning
//!
//! @param: addr_byte Address to read the byte in Eeprom memory.
//!
//! @return: Value read in the Eeprom memory.
//!
//------------------------------------------------------------------------------
U8 eeprom_rd_byte(U16 addr_byte)
{
//-----推荐在对EEPROM操作的所有步骤中清除全局中断标志来避免错误。
//【2010/06/22】清除全局中断后对Timer0/1/3有影响
//cli();
Eeprom_prog_completed
return (Eeprom_rd_byte(addr_byte));
//sei();
}
//------------------------------------------------------------------------------
// @fn eeprom_wr_byte
//!
//! This function allows to write a byte in up to 64K of Eeprom memory.
//!
//! @warning
//!
//! @param: addr_byte Address in Eeprom memory to write the byte.
//! value Value to write in the Eeprom memory.
//!
//! @return none
//!
//------------------------------------------------------------------------------
void eeprom_wr_byte(U16 addr_byte, U8 value)
{
//-----推荐在对EEPROM操作的所有步骤中清除全局中断标志来避免错误。
//【2010/06/22】清除全局中断后对Timer0/1/3有影响
//cli();
Eeprom_prog_completed
Eeprom_wr_byte(addr_byte, value);
//sei();
}
//------------------------------------------------------------------------------
// @fn eeprom_rd_block
//!
//! This function allows to read up to 65535 bytes (64K Bytes-1 byte) of
//! the Eeprom memory.
//!
//! @warning: 1 - This function isn't able to address the fully 64K bytes
//! but we cannot find in the device a source buffer up to
//! 64K bytes.
//!
//! @param: src Start address in Eeprom memory where write data.
//! *dest Address of data to write.
//! n number of byte to write.
//!
//! @return none
//!
//------------------------------------------------------------------------------
void eeprom_rd_block(U16 src, U8* dest, U16 n)
{
for (; n != 0 ; n--)
{
*dest = eeprom_rd_byte(src);
src++; dest++;
//wdt_reset();
}
}
//------------------------------------------------------------------------------
// @fn eeprom_wr_block
//!
//! This function allows to write up to 65535 bytes (64K Bytes-1 byte) in
//! the Eeprom memory.
//!
//! @warning: 1 - This function isn't able to address the fully 64K bytes
//! but we cannot find in the device a source buffer up to
//! 64K bytes.
//!
//! @param: *src Address of data to write.
//! dest Start address in Eeprom memory where write data.
//! n number of byte to write.
//!
//! @return none
//!
//------------------------------------------------------------------------------
void eeprom_wr_block(U8* src, U16 dest, U16 n)
{
for (; n != 0 ; n--)
{
eeprom_wr_byte(dest,*src);
src++; dest++;
}
}
//------------------------------------------------------------------------------
// @fn eeprom_full_erase
//!
//! This function erases the whole Eeprom memory.
//!
//! @warning none
//!
//! @param none
//!
//! @return none
//!
//------------------------------------------------------------------------------
void eeprom_full_erase(void)
{
U16 addr;
for(addr = 0; addr < (U16)(E2_END+1); addr++)
{
if ((eeprom_rd_byte(addr)) != EEPROM_BLANK_BYTE)
{
eeprom_wr_byte(addr, EEPROM_BLANK_BYTE);
}
}
}
供参考啊 |
|