hi, I'm working on the compass sensor hmc5883l with at89s52 controller. In the process of reading the data from the compass sensor, i am supposed to collect data from 2 registers and join these 2 datas to make a single data (in short concatenation 8 bit +8 bit =16 bit). the data present in the registers is in the form of 2's complement hexadecimal number. can any1 suggest me how to convert this 2's complement data to a decimal number. also i need to display this number onto the 16*2 char lcd. for this i again need to convert the decimal data to character data.
thanks in advance.
Just about any processor can do this. And most can do it trivially for 8, 16 and 32-bit integers, even if they do not have native register sizes for 8, 16 and 32-bit integers.
Convert from two-complement? What format do most processors use internally for storing signed integers?
What do a normal program usually do when a value in an integer should be emitted on a console, or on a display or a printer? Would the conversion from binary to individual ASCII characters actually care about what actual device that will be used to present these ASCII characters?
what freq is the crystal? it should be very fast.
"what freq is the crystal? it should be very fast."
Stop posting random noise. You are either a troll or do not know what you are talking about. Either case, it's non-productive.
crystal frequency is 11.0592MHz
pls suggest me some documents or links from which i can enhance my knowledge on how to decide crystal frequency for any device. Also regarding the data reading and conversion. thanks in advance.
Get some books on programming in C, micro-processor architectures and read them. Some math skills would probably also help.
www.cplusplus.com/.../
int16_t value; uint8_t lowbyte, highbyte; value = (int16_t)((uint16_t)lowbyte | ((uint16_t)highbyte << 8));
static char *itoa_i16_10(int i) { static char str[8]; char *s = str + sizeof(str); int sign = 0; *--s = 0; if (i < 0) { sign = 1; i = -i; } do { *--s = '0' + (char)(i % 10); i /= 10; } while(i); if (sign) *--s = '-'; return(s); }
The code is poorly documented, coder made undocumented assumptions, and code is not portable.
Some Qestions/Answers
Q1) What if sizeof(int) is 4? A1) function can write beyond array str[] limits
Q2) What if sizeof(int) is 2 and i is -32768? A2) It will fail inverting the sign as there is no +32768
Q3) Is the code safe? Do I want to see this in an embedded application. A3) Definitly not.
:-)