中科因仑“3+1”工程特种兵精英论坛
标题:
对·EEPROM读写浮点数(AVR,IAR)
[打印本页]
作者:
谭力源
时间:
2016-5-28 18:30
标题:
对·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
欢迎光临 中科因仑“3+1”工程特种兵精英论坛 (http://bbs.enlern.com/)
Powered by Discuz! X3.4