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
split63
PostPosted: May 27, 2009 - 04:05 AM
Wannabe


Joined: Oct 08, 2008
Posts: 87
Location: N. California

from a much earlier post on page 1:
Quote:
double ReadDoubleFromEeprom(void){
double temp;
eeprom_read_block((void*)&temp, (const void*)&EEVar, sizeof(double));
return(temp);

Can someone explain why the (void*) cast is necessary and what it does in this case?

I found that in the case of characters, I have not needed this. But when I tried this for a float, I had to add the (void*).

char temp[10];
eeprom_read_block(&temp,&EEVar, 10);
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: May 27, 2009 - 10:21 AM
10k+ Postman


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

Well start by looking at avr/eeprom.h where the prototype for eeprom_read_block is:
Code:
static __inline__ void
eeprom_read_block (void *__dst, const void *__src, size_t __n)

So it's destination pointer is given as void *. The reason for this is that the authors of eeprom_read_block() cannot possibly know what the given pointer might be pointing at. You could do:
Code:
char c;
int n;
long l;
struct {
 char * p;
 int array[10];
} s;

eeprom_read_block((void *)&c, &e_c, 1);
eeprom_read_block((void *)&n, &e_n, 2);
eeprom_read_block((void *)&l, &e_l, 4);
eeprom_read_block((void *)&s, &e_s, 12);

In the first the first parameter is a pointer to char, in the second a pointer to int, in the third a pointer to long and in the fourth a pointer to a 12 byte struct.

So when the type that a pointer variable is pointing at may not be known the C language allows for "void *" which kind of means "this could be pointing at anything".

The typecast is simply to tell the compiler to interpret the pointers as "void *" at the moment the eeprom_read_block() line is being compiled so they will match the defined paramater type given in the prototype.

Cliff

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
santiall
PostPosted: Jun 19, 2009 - 09:43 AM
Newbie


Joined: Nov 11, 2005
Posts: 5


Code:
#include <avr/eeprom.h>

uint8_t  EEMEM NonVolatileChar;
uint16_t EEMEM NonVolatileInt;
uint8_t  EEMEM NonVolatileString[10];

int main(void)
{
    uint8_t  SRAMchar;
    uint16_t SRAMint;
    uint8_t  SRAMstring[10];   

    SRAMchar = eeprom_read_byte(&NonVolatileChar);
    SRAMint  = eeprom_read_word(&NonVolatileInt);
    eeprom_read_block((void*)&SRAMstring, (const void*)&NonVolatileString, 10);
}


Hi, this is my first post here and I'm trying to use the EEPROM with GCC.

Shouldn't be the last line:

Code:
eeprom_read_block((void*)SRAMstring, (const void*)NonVolatileString, 10);


or

Code:
eeprom_read_block((void*)&SRAMstring[0], (const void*)&NonVolatileString[0], 10);


since the arrays are already the addresses the '&' operators need to be avoided.

Please correct me if I am wrong.
Cheers
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 19, 2009 - 09:50 AM
10k+ Postman


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

In the case of using the array name it doesn't matter if you use & or not.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
bianca22
PostPosted: Jul 08, 2009 - 03:14 PM
Newbie


Joined: Mar 25, 2009
Posts: 5


Hello!

I write in the EEPROM :
char tex1[20] = "12.05 BedTi 130";

when i read it i want to use only the 130 value to comapre it with another value if (something) then display a message on lcd. If anyone can tell me how to read only the 130?
I use AVR studio 4 , GCC compiler.


Thank you in advance,
 
 View user's profile Send private message  
Reply with quote Back to top
Koshchi
PostPosted: Jul 08, 2009 - 08:40 PM
10k+ Postman


Joined: Nov 17, 2004
Posts: 13814
Location: Vancouver, BC

If the string will always be the same length, then just read characters 12 through 14. If it might change, read the entire string and just look at the last three characters. To get it from a string to a number (if this is what you want), use atoi().

_________________
Regards,
Steve A.

The Board helps those that help themselves.
 
 View user's profile Send private message  
Reply with quote Back to top
asif164
PostPosted: Jul 17, 2009 - 12:19 PM
Rookie


Joined: Oct 30, 2008
Posts: 26


plz give me some example code to write data in EEPROM using "void eeprom_write_word (uint16_t *addr, uint16_t value)" Embarassed Embarassed
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jul 17, 2009 - 12:28 PM
10k+ Postman


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

Code:
#include <avr/io.h>
#include <avr/eeprom.h>

uint16_t EEMEM EEint;

int main(void) {
 uint16_t i;
 i = PINC | (PINB << 8); // set it to something useful
 eeprom_write_word(&EEint, i);
}

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
Snevzor
PostPosted: Aug 09, 2009 - 12:35 AM
Newbie


Joined: Sep 13, 2008
Posts: 13


Nice tutorial!
 
 View user's profile Send private message  
Reply with quote Back to top
BosByte
PostPosted: Aug 25, 2009 - 10:03 PM
Newbie


Joined: Aug 25, 2009
Posts: 1


Hi, i think you made an error in your code...

-edit- wh00ps, &array_name is the same as &array_name[0], so it's okay, sorry Smile


Last edited by BosByte on Aug 26, 2009 - 11:23 AM; edited 3 times in total
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Aug 26, 2009 - 11:06 AM
10k+ Postman


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

Suggest you take a look at the generated code both with and without the '&' - notice anything?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
kakarot
PostPosted: Sep 08, 2009 - 11:56 AM
Hangaround


Joined: Nov 01, 2003
Posts: 127
Location: Greece

What about negative numbers?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Sep 08, 2009 - 12:09 PM
10k+ Postman


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

Just store retrieve as uchar/uint then cast the signed interpretation onto them

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
kakarot
PostPosted: Sep 14, 2009 - 01:07 PM
Hangaround


Joined: Nov 01, 2003
Posts: 127
Location: Greece

sorry for this but I am stuck.
Here is my code
Code:

uint16_t EEMEM Eapp = 200;

char s[4]="";
uint16_t temp = 0;
temp = eeprom_read_word(&Eapp);
itoa((signed int)temp,s,4);


s is always "3020"
What is going wrong with my code?
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Sep 14, 2009 - 02:32 PM
10k+ Postman


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

Well did you realise that 4 in your itoa() is asking for the conversion to be made in base 4? So 3020 means 0 units, 2 lots of 4^1, 0 lots of 4^2 and 3 lots of 4^3. IOW 8 + (3 * 64). Which, if I got my sums right is 200 in decimal. You may want to try ",10" rather than ",4" Wink

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
kakarot
PostPosted: Sep 14, 2009 - 03:19 PM
Hangaround


Joined: Nov 01, 2003
Posts: 127
Location: Greece

Ok.
I am so embarrassed now!!!
Somehow I thought 4 was the size of s!
Thank you very much. I ate 6 hours trying to figure out what was going on!
Thanks again.
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Sep 14, 2009 - 07:07 PM
10k+ Postman


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

You are welcome to locate the thread "The Stupid Things We Do" in the OT Forum and contribute to it, just as we have. Everyone does Really Stupid Things [tm], so don't be too embarrassed. Very Happy
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
dahlia
PostPosted: Dec 04, 2009 - 02:33 AM
Newbie


Joined: Nov 26, 2009
Posts: 4


hi all
i'm working with STK500 and Atmega128 microcontroller.
i'm newbie in C programming.Currently i try to make a program that when a character is receive from a microcontroller, it will write the received character into the EEPROM. Start from address 0 and increment this address location for the next character.Upon finishing writing 1 character into the EEPROM, i also want to read previous written character and transmit to the PC for verifications that the characters written to the EEPROM is correct.Below is my coding:

Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#define BUFFER_SIZE 10
#define   BAUD_57600_BPS   15
#define TRUE 1
#define FALSE 0
volatile uint8_t rxBuffer [BUFFER_SIZE];
volatile uint8_t readPtr;
volatile uint8_t writePtr;
volatile uint8_t txComplete;
uint8_t addressWrite EEMEM  = 0;
uint8_t addressRead EEMEM = 0;

//Inizialize USART '0'
void USART_Init (uint16_t data)
{
   /* Set baud rate */
   UBRR0H = (uint8_t)(data>>8);
   UBRR0L = (uint8_t)data;

   //Double Speed Operation
   UCSR0A = 1 << U2X;
   
    //Enable Transmitter and receiver;enable RX interrupt;enable TX interupt
   UCSR0B |= (1<<RXEN)|(1<<TXEN)|(1<<RXCIE)|(1<< TXCIE);
   
   //Set frame format 8bit
   UCSR0C |= (1<<UCSZ1)|(1<<UCSZ0);
}

void transmitData (uint8_t data)
{   
   UDR0 = data;
}

uint8_t bufferEmpty (void)
{
    uint8_t emptyFlag;

   emptyFlag = FALSE;

   if(readPtr == writePtr)
   {    //buffer is empty
      emptyFlag = TRUE;
   }
   return(emptyFlag);      
}

void writeBuffer (uint8_t data)
{   
   rxBuffer [writePtr] = data;
   writePtr++;

   if (writePtr >= BUFFER_SIZE)
   {
      writePtr = 0;
   }
}

uint8_t readBuffer (void)
{
   uint8_t data;
   data = rxBuffer [readPtr];
   readPtr++;
   
   if (readPtr >= BUFFER_SIZE)
   {   
      readPtr = 0;
   }
   return (data);
}

uint8_t bufferFull (void)
{
   uint8_t fullFlag;
   uint8_t tempPtr;
   
   fullFlag = FALSE;
   tempPtr = writePtr;

   if (tempPtr++ >= BUFFER_SIZE)
   {   
      tempPtr = 0;
   }
   if (tempPtr == readPtr)
   {   
      fullFlag = TRUE;
   }

   return (fullFlag);
}

ISR(USART0_TX_vect)
{
   txComplete = TRUE;
}
   
ISR(USART0_RX_vect)

   if (bufferFull() == FALSE)
   {   
      writeBuffer (UDR0);
   }
}

int main(void)
{
   uint8_t readByte;

   // Initialize transmission complete flag.
   txComplete = TRUE;

   //initialize USART_Init
   USART_Init(BAUD_57600_BPS);
   
    //Enable Global Interupt
   sei();

   // loop forever
   while(1)
   {   
      if (bufferEmpty() == FALSE)
      {   
      //write byte to a EEPROM;location to place the byte;read byte from buffer.
           eeprom_write_byte(&addressWrite,readBuffer());
         
         //increament location to next character
         addressWrite++;
         
         if (txComplete == TRUE)
         {   
        //read previous written character      
            addressRead = addressWrite;
              
            addressRead--;   

            txComplete = FALSE;
            
           //read byte from EEPROM
            readByte = eeprom_read_byte(&addressWrite);
            
            //transmit to PC what EEPROM read
            transmitData(readByte);
         }
      }
   }
   return 0;   
}   


When i try this,it give me unknown data.i hope anybody can help me with this.Sorry for my bad english

Rgrd
Dahlia


Last edited by dahlia on Dec 07, 2009 - 03:03 AM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Dec 04, 2009 - 10:27 AM
10k+ Postman


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

Dahlia!

1) Rather than adding yet another subject to this thread, you would have been better off starting your own thread, and
2) I suspect no-one is going to bother to look at your code the way it iss presented right now. All indentation is lost as you have just pasted it as normal text. Do this: i)Click the edit button for your message, ii)mark the source code, iii) click the "Code" button, and finally iv) click the submit button. Your code should now be shown with all textual structure (as indentations etc) intact. Now people might gete interested in looking at your code.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
dahlia
PostPosted: Dec 07, 2009 - 02:37 AM
Newbie


Joined: Nov 26, 2009
Posts: 4


ok.i will do that.thanks..
 
 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