NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » EEPROM confusion

July 27, 2011
by missle3944
missle3944's Avatar

Hi guys,

Im using mjswan's code for eeprom storage in the library. I already read through the avr tutorial and I understand it alittle bit. But I still dont get how to initiallize the eeprom and how to store variables and stuff like that. Anyways heres my code but it wont even let me put it on the MCU b/c of sooo many errors. It says that false is undeclared??? I'm confused!!

volatile    struct savedvars sv;

/*
 *  readsavedvars()
 *      - read variables that need to be saved when the base station is powered off
 */
void
readsavedvars()
{
    eeprom_read_block((void *)&sv, 0, sizeof(sv));
    // do some checking to make sure the data is valid
    if (sv.preamble != 0x4D53) {
        // it doesn't appear to be valid so let's fill in some sensible values
        sv.preamble = 0x4D53;
        sv.TDS = 1500;          // 1500 PPM
        sv.CYA = 50;            // 50 PPM
        sv.backlight = false;   // LCD backlight is off
    }
}

/*
 *  writesavedvars()
 *      - write variables that need to be saved when the base station is powered off
 *      - warning: the ATMega168 has only 512 bytes of EEPROM for this purpose!
 */
void
writesavedvars()
{
    eeprom_write_block(0, (void *)&sv, sizeof(sv));
}
July 28, 2011
by Ralphxyz
Ralphxyz's Avatar

missle3944, you might want to work through this EEPROM Tutorial from Dean Camera over on AVRfreaks.Net.

He explains it so well that even I "should" be able to use the 168 EEPROM.

Dean has other excellant tutorials also.

Ralph

July 28, 2011
by missle3944
missle3944's Avatar

Hi ralph,

Thanks for the siggestion but I already read through that one too. But I don't get how you initialize the EEPROM and how to write values to it?

-missle3944

July 28, 2011
by bretm
bretm's Avatar

Can you post the complete code? That snippet is missing a lot of things such as the #include directives that define the eeprom functions, "false", etc.

Once you have it correctly compiling, the way you use it is to call readsavedvars() to load the saved variables. It initializes the EEPROM for you if the variables haven't been saved before. After you change the values in savedvars you call writesavedvars() to update the EEPROM.

July 28, 2011
by missle3944
missle3944's Avatar

Heres the code for the HEADER:

struct  savedvars {
    uint16_t    preamble;   // used to verify we've previously written data
    uint16_t    TDS;        // total disolved solids, user input
    uint16_t    CYA;        // cyanuric acid, user input
    uint16_t    backlight;
    bool        backlight;  // is the LCD backlight on or off?
};

extern  volatile    struct savedvars sv;

void    readsavedvars(); 
void    writesavedvars();

For the main c code-

#define F_CPU 14745600

#include <stdio.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>
##include <avr/eeprom.h>
#include "persistent.h"

volatile    struct savedvars sv;

/*
 *  readsavedvars()
 *      - read variables that need to be saved when the base station is powered off
 */
void
readsavedvars()
{
    eeprom_read_block((void *)&sv, 0, sizeof(sv));
    // do some checking to make sure the data is valid
    if (sv.preamble != 0x4D53) {
        // it doesn't appear to be valid so let's fill in some sensible values
        sv.preamble = 0x4D53;
        sv.TDS = 1500;          // 1500 PPM
        sv.CYA = 50;            // 50 PPM
        sv.backlight = false;   // LCD backlight is off
    }
}

/*
 *  writesavedvars()
 *      - write variables that need to be saved when the base station is powered off
 *      - warning: the ATMega168 has only 512 bytes of EEPROM for this purpose!
 */
void
writesavedvars()
{
    eeprom_write_block(0, (void *)&sv, sizeof(sv));
}
July 28, 2011
by missle3944
missle3944's Avatar

All right Guys,

I erased that confusing code and started out with a new slate. Heres my barecode to read from location 46 of the eeprom. The code compiles fine in the compiler. But I dont think it does anything b/c theres nothing to read. Can you guys guide me in the steps of reading and writing to the eeprom possibly? heres my code

#define F_CPU 14745600

#include <stdio.h>

#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>

void main(void) 
{
   uint8_t ByteOfData;
   ByteOfData = eeprom_read_byte((uint8_t*)46);
   }

Thanks a bunch :D -missle3944

July 28, 2011
by Noter
Noter's Avatar

Here's all the eeprom functions - eeprom_write_byte is used to store a single byte of data in eeprom. http://www.nongnu.org/avr-libc/user-manual/groupavreeprom.html

July 28, 2011
by missle3944
missle3944's Avatar

Hi guys,

Finally got it to work. But how do I display an 8 bit integer on the lcd? Thanks for the link noter. Now I'm working with floating point numbers in the eeprom.

-missle3944

July 28, 2011
by Noter
Noter's Avatar

You could format using printf() or just use the lcd_write_int16 routine in libnerdkit with a typecast to a 16bit integer in the call:

int8_t my_8bit_int;
lcd_clear_and_home();
lcd_write_int16((int16_t)my_8bit_int);
July 28, 2011
by missle3944
missle3944's Avatar

WOW thanks Noter. I dont think i would have found this :D. How can you put a 8 bit int into a 16bit spot?

-missle3944

July 28, 2011
by Noter
Noter's Avatar

The (int16_t) in front of the variable name is called a typecast and it temporarily converts from one data type to another. In this case the int8 becomes int16 and then the function is called with the temporary int16. You can find many explainations and examples on the web, just search "typecast c".

July 28, 2011
by missle3944
missle3944's Avatar

Hi noter,

Reached another wall... For some reason the compiler wants me to put quote "expected ";",";" or ")" unquote. heres my code snippet:

if (temp_avg < 65){

      void eeprom_write_float (float *2, temp_avg);

        }

I'm using the site you gave to me on all of the syntax of avr/eeprom.h. There examples are hard to follow becuase Im not sure if I add parenthisis around anything b/c my code for 1 byte into eeprom had quotes around unsigned8bit here:

eeprom_read_byte((uint8_t*)46);

were as theirs doesnt:

eeprom_read_byte (const uint8_t *__p) __ATTR_PURE__

just a question.

-missle3944

July 29, 2011
by Noter
Noter's Avatar

A function doesn't need the return type when called, only when defined. Remove the void from the front of the line where you call eeprom_write_float.

In the second example the (uint8_t*) is a typecast. It makes the value 46 into a pointer to an int8 for the purpose of the function call.

Function definitions are a little different than calls. In the definition you specify the data types expected and returned where in the call you just supply/receive those data types. Your 3rd line is from the function definition although the return type of uint8_t is not present in your example.

Here is a good tutorial on c functions as well as the rest of the c language. http://www.crasseux.com/books/ctutorial/Functions.html#Functions

Post a Reply

Please log in to post a reply.

Did you know that you can build an analog amplifier with one transistor and a few resistors? Learn more...