对·EEPROM读写浮点数(AVR,IAR)
供网友参考,已在MEGA128上测试通过//eeprom.c
#include <iom128.h>
#include <intrinsics.h>
#include "eeprom.h"
/*EEPROM写子入程序*/
void EEPROM_write(unsigned int Address, unsigned char Data)
{
while(EECR & (1<<1));/* 等待上一次写操作结束 */
EEAR = Address; /* 设置地址和数据寄存器*/
EEDR = Data;
unsigned char cSREG;
cSREG = SREG; /* 保存SREG */
__asm("CLI\n\t"); /* 禁止中断 */
EECR |= (1<<2);/* 置位EEMWE */
EECR |= (1<<1); /* 置位EEWE 以启动写操作*/
SREG = cSREG; /* 恢复SREG,使能中断 */
}
/*从EEPROM中读取数据*/
unsigned char EEPROM_read(unsigned int Address)
{
while(EECR & (1<<1)); /* 等待上一次写操作结束 */
EEAR = Address; /* 设置地址寄存器*/
unsigned char cSREG;
cSREG = SREG; /* 保存SREG */
__asm("CLI\n\t"); /* 禁止中断 */
EECR |= (1<<0); /* 设置EERE 以启动读操作*/
SREG = cSREG; /* 恢复SREG,使能中断 */
return EEDR; /* 自数据寄存器返回数据 */
}
/*********************************************************************************************************
** function name :
** description :写浮点数,测试已经通过
** Arguments :
** Returns :
** global varible :
** call module :
**
** Author :
** Date :
**-------------------------------------------------------------------------------------------------------
********************************************************************************************************/
/*写浮点数,测试已经通过
void EEPROM_WriteFloat(unsigned int addr, float data)
{
unsigned char i;
for(i=0;i<sizeof(float);i++)
{EEPROM_write(addr+i,((unsigned char *)&data)); }
}
*/
//写浮点数,测试已经通过
void EEPROM_WriteFloat(unsigned int addr, float data)
{
unsigned char * eeprom_ptr;
unsigned char i;
eeprom_ptr = (unsigned char *)&data;
for(i=0;i<sizeof(float);i++)
{EEPROM_write(addr+i,*eeprom_ptr++); }
}
/*********************************************************************************************************
** function name :
** description :读浮点数,测试已经通过
** Arguments :
** Returns :
** global varible :
** call module :
**
** Author :
** Date :
**-------------------------------------------------------------------------------------------------------
********************************************************************************************************/
/*读浮点数,测试已经通过
float EEPROM_ReadFloat(unsigned int addr)
{
float data;
unsigned char i;
unsigned char * eeprom_ptr;
eeprom_ptr = (unsigned char *)&data;
for(i=0;i<sizeof(float);i++)
{ ((unsigned char *)&data) = EEPROM_read(addr+i); }
return data;
}
*/
//读浮点数,测试已经通过
float EEPROM_ReadFloat(unsigned int addr)
{
float data;
unsigned char i;
unsigned char * eeprom_ptr;
eeprom_ptr = (unsigned char *)&data;
for(i=0;i<sizeof(float);i++)
{ *eeprom_ptr++ = EEPROM_read(addr+i); }
return data;
}
//eeprom.h
#ifndef _EEPROM_H_
#define _EEPROM_H_
extern void EEPROM_write(unsigned int Address, unsigned char Data);
extern unsigned char EEPROM_read(unsigned int Address);
extern void EEPROM_WriteFloat(unsigned int addr, float data);
extern float EEPROM_ReadFloat(unsigned int addr);
#endif 支持一个~~~~~~~
页:
[1]