/*STC32G_EEPROM.h
Designed by Bill Liu
Version 0.0
Modified last by Bill Liu on 07/18/2022
*/
#include "STC32G_EEPROM.h"
//****************************************************************
void EEPROM_Disable()
{
IAP_CONTR = 0;
IAP_CMD = 0;
IAP_TRIG = 0;
IAP_ADDRE = 0xff; //set a disable operate addr
IAP_ADDRH = 0xff; //set a disable operate addr
IAP_ADDRL = 0xff; //set a disable operate addr
}
//End of EEPROM_Disable(void)
//****************************************************************
void EEPROM_Trig()
{
F0 = EA; //save EA state
EA = 0; //INT disable
IAP_TRIG = 0x5A;
IAP_TRIG = 0xA5;
_nop_();
_nop_();
_nop_();
_nop_();
EA = F0; //restore EA
}
//End of EEPROM_Disable()
//****************************************************************
void EEPROM_SectorErase(u32 EE_address)
{
IAP_ENABLE();
IAP_ERASE();
IAP_ADDRL = (u8)EE_address;
IAP_ADDRH = (u8)(EE_address >> 8);
IAP_ADDRE = (u8)(EE_address >> 16);
EEPROM_Trig();
EEPROM_Disable();
}
//End of EEPROM_SectorErase(u32 EE_address)
//****************************************************************
void EEPROM_Write_nChar(u32 EE_address,ui8* sourceData,u16 number)
{
IAP_ENABLE();
IAP_WRITE();
do
{
IAP_ADDRL = (u8)EE_address;
IAP_ADDRH = (u8)(EE_address >> 8);
IAP_ADDRE = (u8)(EE_address >> 16);
IAP_DATA = *sourceData;
EEPROM_Trig();
EE_address++;
sourceData++;
}while(--number);
EEPROM_Disable();
}
//End of EEPROM_Write_nChar(u32 EE_address,ui8* sourceData,u16 number)
//****************************************************************
void EEPROM_Read_nChar(ui32 EE_address,ui8* destData, u16 number)
{
IAP_ENABLE();
IAP_READ();
do
{
IAP_ADDRL = (u8)EE_address;
IAP_ADDRH = (u8)(EE_address >> 8);
IAP_ADDRE = (u8)(EE_address >> 16);
EEPROM_Trig();
*destData = IAP_DATA;
EE_address++;
destData++;
}while(--number);
EEPROM_Disable();
}
//End of EEPROM_Read_nChar(ui32 EE_address,ui8* destData, u16 number)
//****************************************************************
void EEPROM_Write_Integer(u32 EE_address,i16 sourceData)
{
typedef union
{
i16 mData;
char arry[2];
}mySoure;
mySoure source;
source.mData = sourceData;
EEPROM_Write_nChar(EE_address,source.arry,2);
}
//End of EEPROM_Write_Integer(u32 EE_address,i16 sourceData)
//****************************************************************
void EEPROM_Write_Long(u32 EE_address,i32 sourceData)
{
typedef union
{
i32 mData;
char arry[4];
}mySoure;
mySoure source;
source.mData = sourceData;
EEPROM_Write_nChar(EE_address,source.arry,4);
}
//End of EEPROM_Write_Long(u32 EE_address,i32 sourceData)
//*************************************************************
void EEPROM_Write_Float(u32 EE_address,f32 sourceData)
{
typedef union
{
f32 mData;
char arry[4];
}mySoure;
mySoure source;
source.mData = sourceData;
EEPROM_Write_nChar(EE_address,source.arry,4);
}
//End of EEPROM_Write_Float(u32 EE_address,f32 sourceData)
//************************************************************
void EEPROM_Write_Double(u32 EE_address,f64 sourceData)
{
typedef union
{
f32 mData;
char arry[8];
}mySoure;
mySoure source;
source.mData = sourceData;
EEPROM_Write_nChar(EE_address,source.arry,8);
}
//End of EEPROM_Write_Double(u32 EE_address,f64 sourceData)
//***********************************************************
void EEPROM_Write_String(u32 EE_address,char* sourceStr)
{
EEPROM_Write_nChar(EE_address,sourceStr,strlen(sourceStr));
EEPROM_Write_nChar(EE_address+strlen(sourceStr),0,1);
}
//End of EEPROM_Write_String(u32 EE_address,char* sourceStr)
//***********************************************************************
i16 EEPROM_Read_Integer(u32 EE_address,i16* destData)
{
typedef union
{
i16 mData;
char arry[2];
}myDest;
myDest dest;
EEPROM_Read_nChar(EE_address,dest.arry, 2);
*destData = dest.mData;
return *destData;
}
//End of EEPROM_Read_Integer(u32 EE_address,i16* destData)
//***********************************************************************
i32 EEPROM_Read_Long(u32 EE_address,i32* destData)
{
typedef union
{
i32 mData;
char arry[4];
}myDest;
myDest dest;
EEPROM_Read_nChar(EE_address,dest.arry, 4);
*destData = dest.mData;
return *destData;
}
//End of EEPROM_Read_Long(u32 EE_address,i32* destData)
//***********************************************************************
f32 EEPROM_Read_Float(u32 EE_address,f32* destData)
{
typedef union
{
f32 mData;
char arry[4];
}myDest;
myDest dest;
EEPROM_Read_nChar(EE_address,dest.arry, 4);
*destData = dest.mData;
return *destData;
}
//End of EEPROM_Read_Float(u32 EE_address,f32* destData)
//***********************************************************************
f64 EEPROM_Read_Double(u32 EE_address,f64* destData)
{
typedef union
{
f64 mData;
char arry[8];
}myDest;
myDest dest;
EEPROM_Read_nChar(EE_address,dest.arry, 8);
*destData = dest.mData;
return *destData;
}
//End of EEPROM_Read_Double(u32 EE_address,f64* destData)
//***********************************************************************
void EEPROM_Read_String(u32 EE_address,char* str)
{
ui8 tem = 0xFF;
memset(str,0, strlen(str));
while(tem != 0)
{
EEPROM_Read_nChar(EE_address,&tem, 1);
*str = tem;
str++;
EE_address++;
}
}
//End of EEPROM_Read_String(u32 EE_address,char* str)
//***********************************************************************
void EEPROM_Read_StringnChar(u32 EE_address,char* str, ui16 number)
{
memset(str,0, strlen(str));
EEPROM_Read_nChar(EE_address, str, number);
}
//End of EEPROM_Read_nString(u32 EE_address,char* str, ui16 number)
评论0
最新资源