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

📄 eepromdg128.c

📁 Code Warrior 4.7 Target : MC9S12DG128B Crystal: 16.000Mhz busclock: 8.000MHz pllclock:16.000MHz
💻 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 NumberOfWord    2             //the TotalDistance's occupation

void U32toU16(U32 *LongData,U16 *U16H,U16 *U16L)
{ 
	*U16H=(U16)((*LongData)>>16);
	*U16L=(U16)((*LongData)|0x0000ffff);
}

void U16toU32(U32 *LongData,U16 *U16H,U16 *U16L)
{
	U32 intH,intL;
	
	intH=(U32)(*U16H);
	intL=(U32)(*U16L);
	*LongData=((intH<<16)|intL);
}

int EepromErase(U16 st_addr,U16 blk) 
{
	U8 *erase;
  if((st_addr>EepromEndAddr)||((st_addr<EepromBeginAddr))||(blk>0xfff)) return 1;	
	
	erase=(volatile unsigned char*)(st_addr);
	ECLKDIV=0x4A;	//16M/8=2M,2M/(1+10)=181.8k in the range 150k--200k
	
	DisableInterrupts;
	
	//erase the eeprom	
	while(erase <= (volatile unsigned char*)(st_addr+blk))
	{
		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;
	return 0;
}

//program one data(4bytes) to EEPROM
int WtEepromU32(U16 st_addr,U32 data)
{
	int i=0;
	U16 *eeaddr,datalen=0;	//eeprom pointer
	U16 dataHL[2];

	dataHL[0]=0;
	dataHL[1]=0;
	
	if(EepromErase(st_addr,4)) return 1;  	
	eeaddr=(volatile U16*)(st_addr);
	ECLKDIV=0x4A;	//16M/8=2M,2M/(1+10)=181.8k in the range 150k--200k 	
	DisableInterrupts; 	
	U32toU16(&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;
	return 0;
}

U32 RdEepromU32(U16 st_addr)
{
	U16 *eeaddr;
	U16 dataH=0;
	U16 dataL=0;
	U32 data=0;
	
	if((st_addr>EepromEndAddr)||(st_addr<EepromBeginAddr)) st_addr=EepromBeginAddr;	
	
	eeaddr=(volatile U16*)(st_addr);
	
	DisableInterrupts;
	dataH=*eeaddr;
	dataL=*(eeaddr+1);
	U16toU32(&data,&dataH,&dataL);
	EnableInterrupts;	
	return(data);
}
U16 RdEepromU16(U16 st_addr)
{  		
	U16 data=0;
	
	DisableInterrupts;
	data=*(U16*)(st_addr); 	
	EnableInterrupts;	
	return(data);
}


⌨️ 快捷键说明

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