Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

I have error in reading from internal EEprom (2byte)as word and show this value.....

Status
Not open for further replies.

mahm150

Full Member level 1
Joined
Dec 14, 2010
Messages
98
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
Cairo, Egypt, Egypt
Activity points
2,001
I have error in reading from internal EEprom (2byte)as word and show this value in LCD,this error not
occur all the time but the word appear in lcd some times with the correct value and sometimes with un correct value ??, i test the function in simulation and simulator it work ok but in real word it work sometimes ... and sometimes.
my functions

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
////////////////////////////////////////////////////////////////////////////////////
unsigned char string[25];
unsigned int EEprom_read_word(unsigned int address)
{
  unsigned char x=0,data;
  unsigned  int long_data;
  unsigned int local_address;
  local_address=address; 
  local_address+=1;
   while(x<9)
   {
      data=EEprom_READ(local_address);//
      long_data = (long_data << x)|data ;//(unsigned int)
      x+=8;
     local_address-=1;
   } 
   return long_data ;
}
//////////////////////////////////////////
unsigned char EEprom_READ(unsigned int address)
{   
    unsigned char data ;
    while(EECR & 1<<EEWE);//(0<<EEMWE|1<<EEWE |0<<EERE));
    EEAR=address; // 16 bit 
    EECR|=(1<<EERE);
    data=EEDR;
    return data;
}
////////////////////////////////////////////////  
count = EEprom_read_word(count_address);
          ltoa(count,string);//
          lcd_puts (string);
/////////////////////////////////////////////////


if intialize count =0 it appear sometimes 4096 ?? and sometimes 0
and if count1 ================>4097

---------- Post added at 19:12 ---------- Previous post was at 19:10 ----------

unsigned int count;
 

I think you are using AVR, why are you trying to make custom functions to write/read the eeprom?
You haven't defined your compiler either but for avr-gcc check my post Write/read the internal EEPROM in AVR using avrgcc (winAVR) - Blogs - Forum for Electronics

It is as simple as

Code C - [expand]
1
2
3
4
5
6
#include <avr/eeprom.h>
#define read_eeprom_word(address) eeprom_read_word ((const uint16_t*)address)
#define write_eeprom_word(address,value) eeprom_write_word ((uint16_t*)address,(uint16_t)value)
 
write_eeprom_word(3,0x0AAA);          // write in eeprom position 3 the value 0x0AAA
my_word= read_eeprom_word(3);         // read from eeprom position 3 and store the value to my_word variable


Alex
 

you can declare eeprom variables, for example

eeprom char my_char;
eeprom char my_char_array[10];
eeprom unsigned int my_word_array[10];

Then when by writing/reading that values you are directly writing/reading the eeprom without the need to use addresses.

Alex
 

you say that the function i written is not work ok for some error , OR it easy to use the function created by compiler

---------- Post added at 16:33 ---------- Previous post was at 16:23 ----------

if i use array eeprom unsigned int my_word_array[10]; how i can read one word (2bytes) as one variable
 

I haven't used custom functions like this to write the eeprom since there are the eeprom variables that simplify the job.

the unsigned integer is already a two byte variable

eeprom unsigned int my_integer;
eeprom unsigned int my_word_array[10];

my_integer=0xabcd;
my_word_array[0]=0x1234;

Alex
 
in this way i am not care about address of my variable

---------- Post added at 17:04 ---------- Previous post was at 17:01 ----------

but compiler need intialize EEprom
 

Do you have a specific reason for wanting to know the exact address of each variable?
If so then you have to use a different way that the one I have described, depends on the application.

Alex

---------- Post added at 18:06 ---------- Previous post was at 18:05 ----------

but compiler need intialize EEprom

what do you mean?
 

the compilre give me warning message need to inialize eeprom

---------- Post added at 17:22 ---------- Previous post was at 17:14 ----------

it work ok but how i intialize eeprom

---------- Post added at 17:29 ---------- Previous post was at 17:22 ----------

about the address of eeprom because my function i use i choose the address i afraid if it make overwrite of other variables
 

You can't overwrite other variables, the eeprom variable will be pointing always to a specific position , it doesn't change each time you run the program on the mcu.
You mean you want to assign an initial value and save it to the eeprom when you program the mcu?
I think eeprom unsigned int my_integer=0x1234; does it but I'm not sure

Alex
 

i means that in my program i use own function which i choose the address and i use the eeprom unsigned int my_integer as you said the problem i talk about it ,is that is there two method make problem in overwriten or not

the second thing you give me the solve of my problem but about my code i written what is the problem that make it not work ok at all the time that the question is there any problem in casting variable or what ? need to know what is the problem
 

If you want to control the address where the data is written then maybe the way I have describe doesn't fit you but you can easily declare a big array of integers (even the complete eeprom) and instead of writing to different addresses write to different array positions.

I have no idea what is wrong with your code, I have never used this way.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top