|
|
View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
Write Hex and read Decimal |
Posted: Mon Dec 20, 2010 4:57 pm |
|
|
Hi, anyone can advise me, I have an array of hex data which are write in the EEPROM internal memory of pic, then read them, but I need to read this data in decimal format, as I can do this?
Code: | #include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20MHz)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)// RS232 Estándar
#include <string.h>
#include <STDLIB.H>
#include <MATH.H>
int Test[] = {0x10, 0x16, 0x10, 0x01, 0x04, 0xae, 0x00, 0x01, 0x02, 0x02, 0x00, 0x00, 0x00, 0x17, 0x85 0x00};
int j;
int Buffer[3];
int16 Pag;
void main(){
for (j = 0; j<2; j++)
write_eeprom( j, Test[13+j]);
for (j = 0; j< 2; j++)
Buffer[j] = read_eeprom(j);
Pag = atol(Buffer);
while (TRUE);
} |
|
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Dec 20, 2010 5:05 pm |
|
|
Remember,
computers work in binary. So the number is ultimately stored in groups of 8 binary digits.
Decimal vs. Hex is more of a matter of how we THINK (and display) the numbers for ourselves -- the human race. And we do this for reasons.
the decimal number 15 = 1111 in binary (nibble size)
the hex number 15 also = 1111 in binary.
No matter how you look at it in decimal or hex or octal.. the computer still sees it as a string of 1's or 0's. (or 1111 in this case).
So think about your question and tell us why you need to "write" something as hex and "read" it back as decimal. Those are HUMAN methods of interpretation.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Mon Dec 20, 2010 5:05 pm |
|
|
If you are referring to reading byte data in "decimal" that is simply in how you print it using the printf() statement for example. 0x10 is the same as 16 decimal if you look at the bit pattern (0001 0000) or are you trying to do something different ? If you are simply asking to display the data in decimal instead of hex, see the arguments to the printf() statement.
[edit] - I see Ben types faster than I do :-)
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Dec 20, 2010 5:07 pm |
|
|
Hahhaha
Sometimes.
;) _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Tue Dec 21, 2010 2:02 pm |
|
|
In this case I've written and read elements 13 and 14 of the array in the EEPROM memory, these elements (0x17, 0x85) are part of the number (0x1785) and its decimal equivalent is 6021, I need to have the decimal value in a variable to perform mathematical operations, I need to do this conversion for that the other elements with which I want to do the operation are in decimal also, as I can do this? |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Tue Dec 21, 2010 2:11 pm |
|
|
pilar wrote: | In this case I've written and read elements 13 and 14 of the array in the EEPROM memory, these elements (0x17, 0x85) are part of the number (0x1785) and its decimal equivalent is 6021, I need to have the decimal value in a variable to perform mathematical operations, I need to do this conversion for that the other elements with which I want to do the operation are in decimal also, as I can do this? |
I don't think you're understanding.
To a computer
10 and 0xA are the same thing.
If you multiply 10 (or 0xA) by 4 you still get the same thing.
YOU (or me) might like to look at it as 40 or 0x28, but the computer
still thinks of it 1 way.
b1010 * b0100
and it equals b0010_1000
What I think you are talking about is the fact that you've written a 16bit (word) value that has to be broken down into 8bit (BYTE) chunks because that's the storage medium. When you read them back, you need to recover the alignment of those BYTES back into the WORD so you can do some math on them.
For this, you should know the endianess of the system at hand... although if you store the values out in the same order you read them back in, you should be ok.
One way to make this easier is to store your values in a structure and then store the STRUCTURE (not the value within discretely. They will be taken care of when storing the struct).
So if you declare
Code: | struct {
unsigned int16 value1;
} stuff_I_want_to_store;
|
when then roll through stuff_I_want_to_store into EEPROM, when you read it back from EEPROM into stuff_I_want_to_store, stuff_I_want_to_store.value1 will be in the order and size that you want.
Then you can do anything you want as you would normally.
-Ben
p.s. at least, this is the way I've done it so far and life with this method has been pretty good to date. _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|