eeprom.c

来自「原创」· C语言 代码 · 共 71 行

C
71
字号
#include <iom16v.h>
#include <macros.h>
 
 
unsigned char EEPROMread( int location)
{
    while (EECR & 0x02);                // Wait until any earlier write is done.
                                        // This is just a safety incase a write
                                        // was done and is not completed when
                                        // the read was called. If this test is
                                        // not done, the current write operation
                                        // will fail due to that the address or
                                        // data is changed.

	EEAR = location;
    
    EECR |= 0x01;                       // Set READ strobe
    
    return (EEDR);                      // Return byte
}


void EEPROMReadBytes(int addr, void *ptr, int size)
	{
	char *dst = ptr;

	while (size--)
		{
		*dst = EEPROMread(addr);
		addr++;
		dst++;
		}
	}
	
	
int EEPROMwrite( int location, unsigned char byte)
{
	unsigned char oldSREG;

    while (EECR & 0x02);                // Wait until any earlier write is done

	EEAR = location;

    EEDR = byte;

	oldSREG = SREG;
	SREG &= ~0x80;		// disable interrupt

    EECR |= 0x04;                       // Set MASTER WRITE enable
    EECR |= 0x02;                       // Set WRITE strobe

	SREG = oldSREG;
    return 0;                           // return Success.
                                        // Could be expanded so that
                                        // the routine checks that the address
                                        // is within the range of the chip.
}                   



void EEPROMWriteBytes(int addr, void *ptr, int size)
	{
	char *src = ptr;

	while (size--)
		{
		EEPROMwrite(addr, *src);
		addr++;
		src++;
		}
	}	

⌨️ 快捷键说明

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