Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
clawson
PostPosted: Apr 11, 2012 - 05:01 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62219
Location: (using avr-gcc in) Finchingfield, Essex, England

Code:
   ADCSRA|=(1<<ADSC);//i use Sharp sensor for
//detectio, and this all works exept writing in //EEPROM
   if (ADC>=500)

You aren't waiting for the conversion to complete? Make that:
Code:
   ADCSRA|=(1<<ADSC);//i use Sharp sensor for
//detectio, and this all works exept writing in //EEPROM
   while(ADCSRA & (1<<ADSC)); // wait while the bit remains high
   if (ADC>=500)

Anyway on the EEPROM thing:
Code:
long EEMEM Putanja1;
long EEVar;
long Motor1AbsPos;

long ReadLongFromEeprom(void)
{
   long temp;
   eeprom_read_block((void*)&temp, (const void*)&EEVar, sizeof(long));
   return(temp);
}

(a) nothing here would create a .eep file

(b) as nothing else is done to write to the EEPROM one has to assume it contains all 0xFF after a chip erase

(c) the read_block call is wrong. Why using _block_ when it's a 23bit "dword" you are reading anyway? eeprom_read_dword() would seem the more obvious call to read the 4 bytes of a "long". As written the call is:
Code:
eeprom_read_block((void*)&temp, (const void*)&EEVar, sizeof(long));

OK, the destination is fine - temp is "long" and the 3rd parameter is passing sizeof(long) so this will be reading 4 bytes. However the source address for that read is wrong. You are using the address of EEVar yet this is a RAM based variable? Surely what you intended to read from here was "Putanja1" so its the address of that (which is an address in EEPROM as that variable is declared as EEMEM) that you should be passing.

If you want a .eep file and you want it to have an initial value then use something like:
Code:
long EEMEM Putanja1 = 12345678;
long Motor1AbsPos;

long ReadLongFromEeprom(void)
{
   long temp;
   eeprom_read_block((void*)&temp, (const void*)&Putanja1, sizeof(long));
   return(temp);
}

When you build this program a .eep file will then be created. If you program that into the AVR's EEPROM when you also put the program code in then the eeprom_read_block will then read 12345678 back from the "Putanjal" location and store it into "temp". However as I say eeprom_read_dword() is a more obvious function so:
Code:
long EEMEM Putanja1 = 12345678;
long Motor1AbsPos;

long ReadLongFromEeprom(void)
{
   return eeprom_read_dword((void*)&Ptanjal);
}

will do what you require here.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
natalijana
PostPosted: Apr 11, 2012 - 10:23 PM
Newbie


Joined: Sep 03, 2011
Posts: 15


I have a few more questions:

1.
I ran first part where I put some data in EEPROM, and loaded code into FLASH and EEPROM,than I comment that part and ran "Read" part. So, do I have to program again EEPROM, each time when I load code in FLASH? (Probably silly question, but i didn't work with EEPROM before)
2.
And, when I look hex. editor there is something like
:040000006400000098
:00000001FF
where "64" is initial EEMVE variable like Address, but what is this "98" ?

Thank you for your explanation, it help me a lot!
 
 View user's profile Send private message  
Reply with quote Back to top
js
PostPosted: Apr 12, 2012 - 03:53 AM
10k+ Postman


Joined: Mar 28, 2001
Posts: 20311
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

Quote:
but what is this "98"
It's the inverted checksum of the line. Not data.

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
natalijana
PostPosted: Apr 12, 2012 - 08:49 AM
Newbie


Joined: Sep 03, 2011
Posts: 15


I figure it out and finally- all works!

Thank you all. This is one fantastic forum! Very Happy
 
 View user's profile Send private message  
Reply with quote Back to top
natalijana
PostPosted: Apr 13, 2012 - 06:11 PM
Newbie


Joined: Sep 03, 2011
Posts: 15


Again, I have a problem. I want to put 2 Arrays in EEPROM, and after to read them. I am certain that data are correct written into EEPRom (the .eep seys so because I defined Arrey[13] and in .eep found 4 lines with smth like arrey- the number I only used), but when I want to read them, there is a problem.
Did I correctly call "Read" function:
Code:
   eeprom_read_block ((void *)&Array, (const void *)ArrayEE, sizeof ArrayEE[0]);
   eeprom_read_block ((void *)&Value, (const void *)&ValueEE, sizeof ValueEE[0]);


I also tried instead of ArrayEE with &ArrayEE, but nothing. I defined smthEE like EEMEM.
Also there is a chance that I wrote smth bad in "for" loop, if it's that case, please just tell me, so I can try to fix that. Here is a code:
Code:

for(i=0;i<13;i++)
{
   for(j=0;j<13;j++)
   {
      if((i==j))
      {
         SetAbsPos(101,0);
         switch(Array[i])
         {
            case 1:
               SetSpeed(0,100,101);
               MotorStart(1,0,0);
               _delay_ms(100);
               GetAbsPos(101,&Motor1AbsPos);
               while(Motor1AbsPos<=Value[j])
               {
                  
                  SetAbsPos(101,Motor1AbsPos);
                  _delay_ms(100);
                  GetAbsPos(101,&Motor1AbsPos);
               }
               break;


Thanks, again.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Apr 13, 2012 - 06:21 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62219
Location: (using avr-gcc in) Finchingfield, Essex, England

Why read the whole array into RAM? Why not pick up the element you need one at a time? Anyway if you really want to read the whole array you do not want
Code:
sizeof ArrayEE[0]

That's only the size of a single element not the whole array.

Anyway here's a simple example reading the elements of an array one at a time:
Code:
#include <avr/eeprom.h>

uint8_t eedata[] EEMEM = { 17, 89, 109, 214, 5, 226, 82 };

for (int i=0; i < 7; i++) {
  PORTB = eeprom_read_byte(&eedata[i]);
}

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
natalijana
PostPosted: Apr 13, 2012 - 06:29 PM
Newbie


Joined: Sep 03, 2011
Posts: 15


Ok! I'll try. Thank you for quick answer.
 
 View user's profile Send private message  
Reply with quote Back to top
abcminiuser
PostPosted: Apr 13, 2012 - 08:25 PM
Moderator


Joined: Jan 23, 2004
Posts: 9817
Location: Trondheim, Norway

I updated the tutorial PDF contents on my site the other day to reflect the API changes made to the EEPROM library over the last few years. Download the PDF from here for the latest version:

http://fourwalledcubicle.com/AVRArticles.php

- Dean Twisted Evil

_________________
Atmel Studio 6.1 is now released, grab it here.
Report AS6/ASF bugs here.
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
natalijana
PostPosted: Apr 14, 2012 - 06:31 PM
Newbie


Joined: Sep 03, 2011
Posts: 15


I am sorry to bother you again, but it seems that I can't put an array in EEPROM, I've read Dean's tutorial and I think I understood how it works, but it won't to read/write in EEPROM. Is there something else I can do that not include program, some adjustments in maybe AVR Studio 4 or ISP communication?

Thank you. Embarassed
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Apr 14, 2012 - 06:41 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18520
Location: Lund, Sweden

You should produce a minimal but complete, clean building and runnable program that demonstrates the problems you describe. Ideally we're talking about perhaps 20 lines of code. Post that program here and we can help.

You are aware of that AVR Studio 4 has a checkbox for if the EEPROM contents should be preserved or ereased dirung flash programming?
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
natalijana
PostPosted: Apr 14, 2012 - 07:03 PM
Newbie


Joined: Sep 03, 2011
Posts: 15


Yes, I checked that box. Here is code:
Code:
#include<avr/io.h>
#include <avr/eeprom.h>
long Value[13];
int EEMEM ArrayEE[13];
long EEMEM ValueEE[13];
int Array [13];
int main()
{
/*
do{
   senzor=Senzori();
   Array[k]=senzor;
switch (senzor)
{

}//switch
}while(senzor!=5);
   eeprom_update_block ((const void *)&Array, (void *)&ArrayEE, 13);
   eeprom_update_block ((const void *)&Value, (void *)&ValueEE, 13);
_delay_ms(10);
*/
uint16_t Red;
uint32_t Vrednost;
for(i=0;i<13;i++)
{
Red=eeprom_read_word ((const uint16_t *)&ArrayEE[i]);
Vrednost= eeprom_read_dword ((const uint32_t *)&ValueEE[i]);
   
   switch (Red)
   {
   }// switch
while(1);
}//main


I checked the Array and it form good values, but the problem is that it won't to put in EEPROM, I've been looking that code for days, but I couldn't find where I'm wrong?


Last edited by natalijana on Apr 14, 2012 - 08:37 PM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Apr 14, 2012 - 08:09 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62219
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

but the problem is that it won't to put in EEPROM,

I find your code close to impossible to read but how do you know it's not writing EEPROM? It does look like at least 5 elements of Array[] should hold some values by the time you do the eeprom_update_block().

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Apr 14, 2012 - 08:17 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18520
Location: Lund, Sweden

1. How are you determining that the EEPROM does not get written.

2. When I said "minimal" I really meant it. For now, throw out all code that deals with the motors etc. Ideally we're talking about a test program of perhaps 10 lines. It is not unlikely that you will reveal the problem yourself during this process. This is how the seasoned folks deal with a problem they do not spot right away - it is much about reducing the problem space. Anyhow, it is likely that more people will read your code and try to help you. (By showing code that is full of irrelevant stuff (for solving the problem at hand) you are asking other people to take their time to do the isolation of the problem - that time should be spent by you, not (perhaps several) other persons.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
natalijana
PostPosted: Apr 14, 2012 - 08:22 PM
Newbie


Joined: Sep 03, 2011
Posts: 15


I tested Array[] and it repeated LED turn on/off pattern I had defined(with both "write/read" section out of comment) , but when I first did "Write" and comment that path, and than tried to read that, there was no pattern i had created before( so i came with that conclusion). I hope you understand what i tried to say.
 
 View user's profile Send private message  
Reply with quote Back to top
natalijana
PostPosted: Apr 15, 2012 - 03:06 PM
Newbie


Joined: Sep 03, 2011
Posts: 15


Hello again,

I solved part of a problem. The problem was switch loop inside of for loop, I only put several "if" instead, but, now I have problem with "for" loop. It just read last element of Arrays. Do you know the reason? Maybe, I put smth else instead of "for", but what when I have to read Arrays?
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Apr 15, 2012 - 03:28 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18520
Location: Lund, Sweden

Dear natalijana..

Imagine being in our place for just a few moments. Think about what you need to tell us in order for us to try to give good answers.
We can not guess what source code you have - you must show it. Or i) face the risk of getting answers like "the error is the missing semicolon in line 42", and ii) wearing out your welcome.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
natalijana
PostPosted: Apr 15, 2012 - 03:49 PM
Newbie


Joined: Sep 03, 2011
Posts: 15


Dear JohanEkdahl,

I know you are right (my code is 6. comment above this comment). But, if I'm no welcome anymore, I'll understand. I have only beautiful words to say about your work here in this forum.

Thank you for everything.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Apr 15, 2012 - 07:27 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62219
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

(my code is 6. comment above this comment)

No that's how the code WAS a while back. Then you said:
Quote:

I solved part of a problem. The problem was switch loop inside of for loop, I only put several "if" instead, but, now I have problem with "for" loop. It just read last element of Arrays. Do you know the reason?

How can anyone here possibly guess what changes you actually made?

Can you consider following Johan's advice and stripping your problem down to just 10 lines or so? (as he says, when we do that to try and isolate a problem we often find the solution during the cut-down process).

BTW the purpose of this thread is only to give feedback to improve the tutorial in the first post - not to solve generic EEPROM programming problems. Can I suggest we take this to a new thread in AVR Forum or (because it may be specifically about GCC handling of EEPROM) the GCC Forum?

Moderator.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
rrozek
PostPosted: Aug 27, 2012 - 05:45 PM
Newbie


Joined: Jul 27, 2011
Posts: 4
Location: wrocław

hi all. sorry to bother you but I need your help or I will go crazy...

I hope at least some of you still visit this thread Wink
Someone mentioned above to make problem as short as possible to get correct answer fast, so here it is.
Following the tut i just copied to new project part of code to see what happens. i program in eclipse with avrdude 5.11.
When i build this program:
Code:

#include <avr/eeprom.h>
#include <stdio.h>
#include <stdlib.h>

//EEMEM uint8_t data;

void main ( void )
{
uint8_t ByteOfData ;
ByteOfData = 0x12 ;
eeprom_update_byte (( uint8_t *) 46, ByteOfData );
}

i get on my release new file called 'project.eep' and when i open it, i get this:
Code:

:00000001FF

btw its the same thing as at the end of hex file.

No matter if I put it in infinite loop or not, it does not show me the '0x12' value anywhere.
What is more, if i use sth like this:

Code:

#include <avr/eeprom.h>
#include <stdio.h>
#include <stdlib.h>

EEMEM uint8_t data;

void main ( void )
{
uint8_t ByteOfData ;
ByteOfData = 0x12 ;
eeprom_update_byte (( uint8_t *) data, ByteOfData );
}

i get allocated space in eeprom:

Code:

:0100000000FF
:00000001FF

but still it is written with nothing. BUT, if i write
Code:

EEMEM uint8_t data=0x12;

i get correct value:
Code:

:0100000012ED
:00000001FF

where is the problem.. Sad can anyone help me with this? PLEASE PLEASE PLEASE!![/code]
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Aug 27, 2012 - 06:48 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62219
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:
where is the problem.

What problem? It's behaving exactly as you'd expect. You only get initial values for initialised globals. Your first code only write 0x12 to location 46 at run time. Why would you have expected anything in the .eep for that?

Your second bit of code simply reserves a location in EEPROM called "data" but does nothing to set an initial value at compile time. As the compiler/linker don't know what else to do they actually assign it an initial value of 0x00 (just like .bss variables in RAM) so that's why you see:
Code:
:0100000000FF

which is setting that 0x00.

Only whe yiou explicitly make a compile time initialisation of the (global) variable:
Code:
EEMEM uint8_t data=0x12;

do you finally get a .eep to set this initial value.

This is completely predictable C behaviour so I'm not sure why you would have expected to see anything else?

Think of .eep as just being like .data initialisers but for EEPROM (because that's what it is).

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits