⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eeprom.c

📁 基于ucosSAEj1939的汽车组合仪表,硬件是MC9S12H128
💻 C
字号:
/***********************************
Name:      eeprom.c
Use:       Erase and Program the EEPROM 
Functions: EepromProgram(dword data); dword ReadFromEeprom;
Note:      refer to the book "单片机嵌入式应用的在线开发方法" ,邵贝贝
Author:    caolei  
Data:      2006.2.26
************************************/

#include <hidef.h>      /* common defines and macros */
#include <MC9S12H256.h>     /* derivative information */
#include "eeprom.h"

void ULongToUInt(unsigned long *LongData,unsigned int *IntH,unsigned int *IntL)
{

	*IntH=(unsigned int)(*LongData/65536);
	*IntL=(unsigned int)(*LongData%65536);
}

void UIntToULong(unsigned long *LongData,unsigned int *IntH,unsigned int *IntL)
{
	unsigned long intH,intL;
	
	intH=(unsigned long)(*IntH);
	intL=(unsigned long)(*IntL);
	*LongData=(intH*65536+intL);
}

void EepromErase(void) 
{
	byte *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(dword data)
{
	int i=0;
	word *eeaddr;	//eeprom pointer
	word dataHL[2];

	dataHL[0]=0;
	dataHL[1]=0;
	
	EepromErase();
	
	eeaddr=(volatile unsigned int*)(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;
}

dword ReadFromEeprom(void)
{
	word *eeaddr;
	word dataH=0;
	word dataL=0;
	dword data=0;
	
	eeaddr=(volatile unsigned int*)(EepromBeginAdd);
	
	DisableInterrupts;
	dataH=*eeaddr;
	dataL=*(eeaddr+1);
	UIntToULong(&data,&dataH,&dataL);
	EnableInterrupts;
	
	return(data);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -