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.

Problem to read EEPROM memory

Status
Not open for further replies.

ecaits

Member level 4
Joined
Jan 16, 2014
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
579
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
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
15,270
Helped
4,970
Reputation
9,962
Reaction score
4,798
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
129,629
Try:
Code:
total = (eeprom_read(0) & 0x0f) * 10000;
total += (eeprom_read(1) & 0x0f) * 1000;
total += (eeprom_read(2) & 0x0f) * 100;
total += (eeprom_read(3) & 0x0f) * 10;
total += eeprom_read(4) & 0x0f;

I used ' & 0x0f' rather than '-48' to make ASCII into binary because it should use less compiler code and it also limits the range of values to 0 - F. Be careful with the range of numbers you use, you can't fit 99999 into an integer, even an unsigned one!

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top