AVR internal EEPROM read and write function EEPROM.H

Publisher:心怀梦想Latest update time:2019-11-25 Source: 51heiKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#pragma used+

///Write single byte EEPROM

void EEPROM_write(unsigned int uiAddress, unsigned char ucData);


///Read single byte EEPROM

unsigned char EEPROM_read(unsigned int uiAddress);


///Write double byte EEPROM

void EEPROM_write2(unsigned int uiAddress, unsigned int ucData);


///Read double byte EEPROM

unsigned int EEPROM_read2(unsigned int uiAddress);


///Read 4 bytes EEPROM

unsigned long EEPROM_read4(unsigned int uiAddress);


///Write 4 bytes EEPROM

void EEPROM_write4(unsigned int uiAddress, unsigned long ucData);




#pragma used-


#pragma library eeprom.lib


EEPROM.C


//#include "eeprom.h"


/* EEPROM Control Register */

#define EERIE 3

#define EEMWE 2

#define EEWE 1

#define EERE 0


#if funcused EEPROM_write

///Write single byte EEPROM

void EEPROM_write(unsigned int uiAddress, unsigned char ucData)

{

//#asm("cli")

////* Wait for the last write operation to end

while(EECR & (1<////* Set up address and data registers

EEAR = uiAddress;

EEDR = ucData;

////* Set EEMWE

EECR |= (1<////* Set EEWE to start write operation

EECR |= (1<

//#asm("sei")

}

#endif



#if funcused EEPROM_read

///Read single byte EEPROM

unsigned char EEPROM_read(unsigned int uiAddress)

{

#asm("cli")

///* Wait for the last write operation to end

while(EECR & (1<///* Set the address register */

EEAR = uiAddress;

///* Set EERE to start read operation

EECR |= (1<///* Return data from data register

return EEDR;


//#asm("sei")

}

#endif


#if funcused EEPROM_read2

///Read double byte EEPROM

unsigned int EEPROM_read2(unsigned int uiAddress)

{ unsigned int ucdata;

// #asm("cli")

///* Wait for the last write operation to end



while(EECR & (1<///* Set the address register */

EEAR = uiAddress+1;

///* Set EERE to start read operation

EECR |= (1<///* Return data from data register

ucdata = EEDR;

ucdata =ucdata<<8;


while(EECR & (1<///* Set the address register */

EEAR = uiAddress;

///* Set EERE to start read operation

EECR |= (1<///* Return data from data register

ucdata |= EEDR;


return ucdata;

//#asm("sei")

}

#endif


#if funcused EEPROM_write2

///Write double byte EEPROM

void EEPROM_write2(unsigned int uiAddress, unsigned int ucData)

{


//#asm("cli")

////* Wait for the last write operation to end

while(EECR & (1<////* Set up address and data registers

EEAR = uiAddress;


EEDR = ucData & 0x00ff;

////* Set EEMWE

EECR |= (1<////* Set EEWE to start write operation

EECR |= (1<

while(EECR & (1<////* Set up address and data registers

EEAR = uiAddress+1;


EEDR = (ucData & 0xff00)>>8;

////* Set EEMWE

EECR |= (1<////* Set EEWE to start write operation

EECR |= (1<//#asm("sei")

}

#endif


#if funcused EEPROM_read4

///Read 4 bytes EEPROM

unsigned long EEPROM_read4(unsigned int uiAddress)

{ unsigned long ucdata;

// #asm("cli")

///* Wait for the last write operation to end



while(EECR & (1<///* Set the address register */

EEAR = uiAddress+3;

///* Set EERE to start read operation

EECR |= (1<///* Return data from data register

ucdata = EEDR;

ucdata =ucdata<<8;


EEAR = uiAddress+2;

///* Set EERE to start read operation

EECR |= (1<///* Return data from data register

ucdata |= EEDR;

ucdata =ucdata<<8;


EEAR = uiAddress+1;

///* Set EERE to start read operation

EECR |= (1<///* Return data from data register

ucdata |= EEDR;

ucdata =ucdata<<8;


while(EECR & (1<///* Set the address register */

EEAR = uiAddress;

///* Set EERE to start read operation

EECR |= (1<///* Return data from data register

ucdata |= EEDR;


return ucdata;

//#asm("sei")

}

#endif


#if funcused EEPROM_write4

///Write 4 bytes EEPROM

void EEPROM_write4(unsigned int uiAddress, unsigned long ucData)

{


//#asm("cli")

////* Wait for the last write operation to end

while(EECR & (1<////* Set up address and data registers

EEAR = uiAddress;


EEDR = ucData & 0x000000ff;

////* Set EEMWE

EECR |= (1<////* Set EEWE to start write operation

EECR |= (1<

ucData = ucData>>8;

while(EECR & (1<////* Set up address and data registers

EEAR = uiAddress+1;

EEDR = ucData & 0x000000ff;

////* Set EEMWE

EECR |= (1<////* Set EEWE to start write operation

EECR |= (1<

ucData = ucData>>8;

while(EECR & (1<////* Set up address and data registers

EEAR = uiAddress+2;

EEDR = ucData & 0x000000ff;

////* Set EEMWE

EECR |= (1<////* Set EEWE to start write operation

EECR |= (1<

ucData = ucData>>8;

while(EECR & (1<////* Set up address and data registers

EEAR = uiAddress+3;

EEDR = ucData & 0x000000ff;

////* Set EEMWE

EECR |= (1<////* Set EEWE to start write operation

EECR |= (1<//#asm("sei")

}

#endif


Keywords:AVR Reference address:AVR internal EEPROM read and write function EEPROM.H

Previous article:AVR MCU Study Notes – ADC
Next article:Lcd.h header file download - AVR microcontroller driver header file for character LCD

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号