Problem to read EEPROM memory

Discussion in 'Embedded Systems and Microcontrollers' started by ecaits, May 19, 2014.

  1. ecaits

    Thread Starter Member

    Jan 6, 2014
    52
    0
    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
     
  2. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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;
    Code ( (Unknown Language)):
    1.  
    2. total=0;  // setup the vars before loop!
    3. p=10000;
    4. for(i=0; i<5; i++)
    5. {
    6.   Value= eeprom_read(i); // To read the digit
    7.   Value= Value-48; // to make it integer
    8.   total = total+(Value*p); // to make total
    9.   p=p/10;
    10. }
    11.  
    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!
    :)
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    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.
     
  5. embpic

    Member

    May 29, 2013
    187
    3
    yes you are making interger size but in decimal format right???
    so simply you can do like
    Code ( (Unknown Language)):
    1.  
    2. Byte 0 = 1,
    3. Byte 1 = 4,
    4. Byte 2 = 6,
    5. Byte 3 = 3,
    6. Byte 4 = 1.
    7.  
    8. val1 = Byte0 *10000+ Byte1 *1000+Byte 2*100+Byte 2*100+Byte 3*10+Byte 4;
    9.  
    you will get you are ans.
     
Loading...