📄 oldeepromdg128.c
字号:
/***********************************
Name: EEPROMDG128.c
Function:Erase and Program the EEPROM
Author: QIU ZHAOPENG
Data: 2008.10.01
************************************/
#include <hidef.h> /* common defines and macros */
#include <mc9s12dg128.h> /* derivative information */
#include "EEPROMDG128.h"
#define EraseInSector 0x40 //erase with sector command
#define ProgramInAlignedWord 0x20 //program with U16 command
//EEPROM address is 0x800 ~ 0xFFF for DG128,from:http://forums.freescale.com/freescale/board/message?board.id=16BITCOMM&message.id=3321&query.id=42737#M3321
#define EepromBeginAdd 0x0800 //eeprom begin address,0x0800=2k
#define EepromEndAdd 0x0c00 //eeprom End address,0x0c00=3k,0x0fff=4k
#define NumberOfWord 2 //the TotalDistance's occupation
void ULongToUInt(U32 *LongData,U16 *U16H,U16 *U16L)
{
*U16H=(U16)(*LongData/65536);
*U16L=(U16)(*LongData%65536);
}
void UIntToULong(U32 *LongData,U16 *U16H,U16 *U16L)
{
U32 intH,intL;
intH=(U32)(*U16H);
intL=(U32)(*U16L);
*LongData=(intH*65536+intL);
}
void EepromErase(void)
{
U8 *erase;
erase=(volatile unsigned char*)(EepromBeginAdd);
ECLKDIV=0x4A; //16M/8=2M,2M/(1+10)=181.8k in the range 150k--200k
DisableInterrupts;
//erase the eeprom
while(erase <= (volatile unsigned char*)(EepromEndAdd))
{
while(!ECLKDIV_EDIVLD); //if the ECLKDIV has been writen once after reset
while(!ESTAT_CBEIF); //whether the command buffer is empty
while(!EPROT_EPOPEN); //whether the eeprom is enabled to program or erase
ECMD=EraseInSector;
ESTAT_CBEIF=1;
while(!ESTAT_CCIF) //whether the command is complete
{}
erase+=4;
}
EnableInterrupts;
}
//program one data(4bytes) to EEPROM
void EepromProgram(U32 data)
{
int i=0;
U16 *eeaddr; //eeprom pointer
U16 dataHL[2];
dataHL[0]=0;
dataHL[1]=0;
EepromErase();
eeaddr=(volatile U16*)(EepromBeginAdd);
ECLKDIV=0x4A; //16M/8=2M,2M/(1+10)=181.8k in the range 150k--200k
DisableInterrupts;
ULongToUInt(&data,&dataHL[0],&dataHL[1]);
while(i<NumberOfWord)
{
while(!ECLKDIV_EDIVLD); //if the ECLKDIV has been writen once after reset
while(!ESTAT_CBEIF); //whether the command buffer is empty
while(!EPROT_EPOPEN); //whether the eeprom is enabled to program or erase
*eeaddr=dataHL[i];
ECMD=ProgramInAlignedWord; //program the eeprom
ESTAT_CBEIF=1;
while(!ESTAT_CCIF) //whether the command is complete
{}
eeaddr++;
i++;
}
EnableInterrupts;
}
U32 ReadFromEeprom(void)
{
U16 *eeaddr;
U16 dataH=0;
U16 dataL=0;
U32 data=0;
eeaddr=(volatile U16*)(EepromBeginAdd);
DisableInterrupts;
dataH=*eeaddr;
dataL=*(eeaddr+1);
UIntToULong(&data,&dataH,&dataL);
EnableInterrupts;
return(data);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -