Problem to read EEPROM memory

Thread Starter

ecaits

Joined Jan 6, 2014
56
Dear Friends,

I have used EEPROM memory to store the character by keypad. I have used EEPROM memory from byte 0 to byte 1 and I have store one digit to each location as character (by 4x4 keypad). I am working on PIC16F877 using Hi-Tech C compiler.

Say,

Byte 0 = 1,
Byte 1 = 4,
Byte 2 = 6,
Byte 3 = 3,
Byte 4 = 1.

Now I want to make it integer, say total = 14631.

I have used following logic to do the task.

int Value, total=0, p=10000;

for(i=0; i<5; i++)
{
Value= eeprom_read(i); // To read the digit
Value= Value-48; // to make it integer
total = total+(Value*p); // to make total
p=p/10;
}

But it is not giving proper output.

Can anybody suggest other logic or where is the problem in above logic.

Thank you in advance,

Nirav
 

THE_RB

Joined Feb 11, 2008
5,438
The logic looks fine.

One problem i can see is that you only set total=0 and p=10000 once, so if the for() loop is called multiple times it will only work the first time.

You could add this;
Rich (BB code):
total=0;  // setup the vars before loop!
p=10000;
for(i=0; i<5; i++)
{
  Value= eeprom_read(i); // To read the digit
  Value= Value-48; // to make it integer
  total = total+(Value*p); // to make total
  p=p/10;
}
Then the other most obvious thing would be that you are not writing the data into the correct place in the eeprom. You did not show your code for that?

Try just reading ONE value from eeprom and display it with no conversion.

You might find the problem is that your programmer is over-writing the eeprom each time you program the PIC!
:)
 

t06afre

Joined May 11, 2009
5,934
I would also suspect that the problem is the writing process. You can use the software simulator in MPLAB to track down this error very easy
 

ErnieM

Joined Apr 24, 2011
8,377
But it is not giving proper output.
What output would it be giving?

Have you tried several inputs to observe what the outputs are?

Not saying what the output is restricts answers to the most obvious. Letting us know (for example) I put in 1,4,6,3,1 and got out 1463 lets us aim a little closer.
 

embpic

Joined May 29, 2013
189
yes you are making interger size but in decimal format right???
so simply you can do like
Rich (BB code):
Byte 0 = 1, 
Byte 1 = 4,
Byte 2 = 6,
Byte 3 = 3,
Byte 4 = 1.

val1 = Byte0 *10000+ Byte1 *1000+Byte 2*100+Byte 2*100+Byte 3*10+Byte 4;
you will get you are ans.
 
Top