How save a number in ATmega8A EEprom ?

Go To Last Post
19 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I want to work with eeprom memory for first time. I read data sheet atmega8 and I have some question.

if i want save for example 1234 value in eeprom, this value in below code of data sheet is equal ucDATA? mean EEDR= 1234;

void EEPROM_write(unsigned int uiAddress, unsigned char ucData)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEWE))
;
/* Set up address and data registers */
EEAR = uiAddress;
EEDR = ucData;
/* Write logical one to EEMWE */
EECR |= (1<<EEMWE);
/* Start eeprom write by setting EEWE */
EECR |= (1<<EEWE);
}

what is uiAddress? is this 0-521? because eeprom for mega8 is 512 byte.?

for read 1234 value that save in above code, do use below code?

unsigned char EEPROM_read(unsigned int uiAddress)
{
/* Wait for completion of previous write */
while(EECR & (1<<EEWE))
;
/* Set up address register */
EEAR = uiAddress;
/* Start eeprom read by writing EERE */
EECR |= (1<<EERE);
/* Return data from data register */
return EEDR;
}

If possible ,Please give me an example for better understanding.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

http://www.github.com/abcminiuse...

 

This pdf was written by the author of the EEPROM tutorial in the TUTORIAL forum here at AVRFREAKS

 

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Avr-gcc has functions to make it easier
Though I cannot spot any clue in his code that says it is GCC being used ;-)

 

Having said that I think ALL the C compilers have some mechanism for accessing EEPROM don't they?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, EEDR is an 8 bit register so you can't actually fit your example value of 1234 there cheeky

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But 1234 is (1234>>8) + (1234 & 0xFF) and both those are 8 bit and fit in EEDR (but at adjacent EEAR locations).

 

But the point is you should not need to worrry about any of this. Either your C compiler supports something like:

__eprom unsigned int eeVAR;

eeVAR = 1234;

Which hides everything about the way this actually gets stored (even WHERE it gets stored). Or you use something like:

EEMEM unsigned int eeVAR;

eeprom_update_word(&eeVAR, 1234);

which is a little more cumbersome but you still don't have to split the 1234 into separate bytes yourself.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok, I wronged.I must to use this code.

eeprom_update_word((uint16_t*)14,1234);

Thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Where does the 14 in that come from?
.
(I know, I am asking if you do?)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

14 is eeprom address.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah but once you start saving 10 different things (of varying lengths) into the EEPROM how do you know location 14 isn't going to bump into something else? In fact did you even realize you are actually using locations 14 AND 15?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

no, I am learning about using eeprom. I selected 14 random! for test my code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And my point is that it is dangerous to use (randomly selected) numbers. Let the linker pick the addresses for you (as it does in RAM) by using EEMEM variables. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what is EEMEM variables?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That was your Google search term. How did that work out for you? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include <avr/eeprom.h>

volatile unsigned char byte ;        // variables in ram
volatile unsigned int  word ;

volatile unsigned char eebyte EEMEM; // variables in eeprom
volatile unsigned int  eeword EEMEM;

int main(void)
{

  while(1)
  {
    eeprom_write_byte(&eebyte, 0xCC);  // write to eeprom
    byte = eeprom_read_byte(&eebyte);  // read from eeprom

    eeprom_write_word(&eeword,0xDDDC);
    word = eeprom_read_word(&eeword);

    eeprom_update_word(&eeword,0xDDDD); // better than write
    word = eeprom_read_word(&eeword);
  }
}

 

Last Edited: Sun. Sep 10, 2017 - 04:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

That was your Google search term. How did that work out for you? 

I just Googled it and got precise hits. These days we have unprecedented access to information,but yet the majority seem to have no idea how to access it. How many posters here could answer their own questions by just Googling the title of their topic? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman:

 

I agree.

But for a beginner even some of those precise hits can be hard to understand.

I think we can put a clear code example for a simple question time from time.

 

Otherwise we could put here an UNIVERSAL ANSWER:

"Read the datasheet and google internet". 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for support a beginner. I agree too. I'm a beginner in AVR and learning with examples is easiest for me compared to internet search .

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mostafa147 wrote:
learning with examples is easiest for me compared to internet search

That statement makes no sense. Searching the internet does not rule out finding examples.

 

I suppose you actually are saying that being spoon-fed is easier for you than searching the net yourself. We're saying you should try feeding yourself first.

 

Here's just the first hit from Googling "AVR EEMEM example": https://tinkerlog.com/2007/06/16... . There are more good hits for that query.

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Sun. Sep 10, 2017 - 10:58 AM