Main Menu
Home
AVR News
Atmel AVR
AVR Development Tools
Valuable Tools
WinAVR toolset
Makefile for WinAVR
AVR Virtual Simulators
Hardware for prototyping
GCC and AVR-GCC
Short introduction to C
AVR-GCC Tutorial
Example AVR Projects
AVR-GCC articles
WinAVR Site Map
Scienceprog FORUM
ScienceProg BLOG
Disclaimer
WinARM Tutorial
Login Form





Lost Password?
No account yet? Register
Latest comments
Measuring motor speed and...
Dear Administrator The code above...
More...

Programming AVR ADC module...
hi please send me example usart with...
More...

Measuring motor speed and...
main.c: In function...
More...

Running TX433 and RX433 RF...
(Hopefully this is not a re-post). I am...
More...

Running TX433 and RX433 RF...
I just bnought a pair of the RX/TX 433...
More...

Syndicate
Friendly sites

Recommended sites


Latest News
Tip on storing initial values in EEPROM PDF Print E-mail
Written by Administrator   
Monday, 10 September 2007

AVR microcontroller have EEPROM memory built in which adds a lots of abilities storing constant values, calibration data, and other data that may be read and changed at any time during program flow. Ion couple projects I have used EEPROM for storing last device configuration, eg. in TDA7313 volume, BASS, TREBLE input and output channel information is updated each time when it has been changed. After device si turned ON it reads last saved settings from EEPROM memory and restores last device state. But what happens when you turn device for the first time. It has to read initial values that has to be stored or it wil read 'FF' what may lead to crash or something else. One way is to prepare separate *.eep file with initial EEPROM data and then burn it separately to microcontrollers EEPROM. But this may not be always handy.

So this is simple trick how to store initial values to EEPROM at very first microcontroller run and skip this part later without additional files need to be flashed in. Probably other are doing a bit differently, this is how I do.


#include <avr/eeprom.h>

//Define EEPROM locations for restoring state

#define E_VOLUME 0

#define E_LF 1

#define E_RF 2

#define E_LR 3

#define E_RR 4

#define E_ch 5

#define E_BASS 6

#define E_TREBLE 7

#define E_INIT 8


After main() I add following code


//------EEPROM initial values-------------

if (eeprom_read_byte((uint8_t*)E_INIT)!='T')

{

eeprom_write_byte((uint8_t*)E_VOLUME,0x0F);//0dB

eeprom_write_byte((uint8_t*)E_LF,0x80);//0dB

eeprom_write_byte((uint8_t*)E_RF,0xA0);//0dB

eeprom_write_byte((uint8_t*)E_LR,0xC0);//0dB

eeprom_write_byte((uint8_t*)E_RR,0xE0);//0dB

eeprom_write_byte((uint8_t*)E_ch,0x58);//ch1 0dB, Loudness OFF

eeprom_write_byte((uint8_t*)E_BASS,0x6F);//0dB

eeprom_write_byte((uint8_t*)E_TREBLE,0x7f);//0dB

eeprom_write_byte((uint8_t*)E_INIT,'T');//marks once that eeprom init is done

//once this procedure is held, no more initialization is performed

}


//--------Restore values from EEPROM------

ST.value[0]=eeprom_read_byte((uint8_t*)E_VOLUME);

ST.value[1]=eeprom_read_byte((uint8_t*)E_BASS);

ST.value[2]=eeprom_read_byte((uint8_t*)E_TREBLE);

ST.value[3]=eeprom_read_byte((uint8_t*)E_ch);

ST.value[4]= ST.value[3]&0b00011000;//restore gain

ST.LD=ST.value[3]&0b00000100;//restore Laudness

ST.MT=0;//default Mute OFF

ST.value[5]=eeprom_read_byte((uint8_t*)E_LF);

ST.value[6]=eeprom_read_byte((uint8_t*)E_RF);

ST.value[7]=eeprom_read_byte((uint8_t*)E_LR);

ST.value[8]=eeprom_read_byte((uint8_t*)E_RR);


-EEPROM initial values- block is executed only once. It initializes first values for the first time and sets 'T' flag at E_INIT EEPROM address. Second block - Restore values from EEPROM- now can read values initialized and continue normal program flow. So next time program is started it skips EEPROM initialization block as there si already E_INIT flag set to 'T'. This way EEPROM data can be initialized for any situation from a single source file if needed.


   

Users' Comments  
 

Average user rating

 


Add your comment
Only registered users can comment an article. Please login or register.

No comment posted



mXcomment 1.0.6 © 2007-2008 - visualclinic.fr
License Creative Commons - Some rights reserved
Next >

© 2008 WinAVR AVR-GCC Tutorial
Joomla! is Free Software released under the GNU/GPL License.