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

📄 eeprom.c

📁 本文档用于描述一体化组合模块485监控板监控对象
💻 C
字号:
#include "main.h"
#include "variable.h"
#include "eeprom.h"


////////////////////////////////////////////
U8 Seek_EEPROM(void)
{
	U8 i;
	
	i=EepromDesc.NO+1;

	if(i>(EEPROM_SIZE-1))
		i=0;

	while((i!=EepromDesc.NO)&&(EepromBuffDesc[i].MemAddr!=0))
	{
		if(++i>(EEPROM_SIZE-1))
			i=0;
	}

	if(i==EepromDesc.NO)
	{
		i=EepromDesc.NO-1;
	
		if(i>(EEPROM_SIZE-1))
			i=EEPROM_SIZE-1;
	}

	return i;
}

void Write_EEPROM(void)
{
	U8 i;
	if( !EepromDesc.Busy )
	{
		i=EepromDesc.NO;
		if(i>(EEPROM_SIZE-1))
			i=0;
		
		do{
			if(EepromBuffDesc[i].MemAddr!=0)
			{
				EepromDesc.NO=i;
				if(Write_EEPROM_block(EepromBuffDesc[i].MemAddr,(U8*)(EepromBuffDesc[i].BuffAddr),EepromBuffDesc[i].Len)==TRUE)
				{
					EepromBuffDesc[EepromDesc.NO].MemAddr=0;
					EepromDesc.Error=0;
					if(++EepromDesc.NO>(EEPROM_SIZE-1))
						EepromDesc.NO=0;				
				}
				else
				{
					if(++EepromDesc.Error>2)
					{
						if(++EepromDesc.NO>(EEPROM_SIZE-1))
							EepromDesc.NO=0;
						EepromBuffDesc[i].MemAddr=0;
						EepromDesc.Error=0;
					}
				}
				EepromDesc.Time=0;
				EepromDesc.Busy=TRUE;
				break;
			}
			else
			{
				if(++i>(EEPROM_SIZE-1))
					i=0;
			}
		}while(i!=EepromDesc.NO);
	}	
}

// function to test if the EEPROM is ready for a read or write operation
// returns non zero if ready, zero if not ready
unsigned char RTEEPROMReady(void)
{
	return !(EECR & 0x02);
}



// function to initiate an EEPROM write
// writes the specified data byte to the specified location
// this will fail if the EEPROM is not ready!
void RTEEPROMwrite(U16 location, unsigned char databyte)
{
	unsigned char savedSREG;
	EEAR = location;						// set address
	EEDR = databyte;						// set data	
	savedSREG = SREG;						// keep setting so it can be restored
	SREG &= 0x7f;                                                   //disable all interrupts			
	EECR |= 0x04;							// set "write enable" bit
	EECR |= 0x02;							// set "write" bit
	SREG = savedSREG;						// restore SREG
	EEAR = 0;
}



// function to read from the EEPROM
// reads a byte from the specified location
// this will fail if the EEPROM is not ready!
unsigned char RTEEPROMread(U16 location)
{
	EEAR = location;			// set address
	EECR |= 0x01;				// set "read enable" bit
	EEAR = 0;
	return (EEDR);
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                


U8 eeprom_read_block(U16 MemAddr,U8 *buff,U8 len)
{
 U8 i;
 for(i=0;i<len;i++)
      {
       while(RTEEPROMReady()==0)
	   	;
       *buff = RTEEPROMread(MemAddr);
	buff++;
	MemAddr++;
      }
 return 1;	
}

U8 Write_EEPROM_block( U16 MemAddr,U8 *buff, U8 len)
{
  U8 i;
  for(i=0;i<len;i++)
      {
       while(RTEEPROMReady()==0)
	   	;
       RTEEPROMwrite(MemAddr,*buff);
	MemAddr++;
	buff++;
      }
 return 1;	
}






⌨️ 快捷键说明

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