How to convert the decimal value to hexadecimal value which needed to be stored in the timer? for example: Timer Low and Timer High only need 2 values such as the 0xFB. How can we extract it if we able to get the hexa from decimal? Have any ideas abt this ? thanx
Think more carefully - there is no conversion required here. A 16-bit number is the same number whether you choose to display it in binary, decimal, hexadecimal, or any other radix (number base)! You could even display it using Roman numerals, if you want! So the question is just how to separate the individual bytes - this is a standard 'C' problem, and nothing specifically to do with Keil. To be completely portable & compiler independent, use the standard 'C' shift and mask (bitwise-AND) operators; To be possibly quicker and use less code, use a union or pointers. It's all been discussed many times before - try a search...
well, i think i can provide what actually i want here. i'm actually writing a servo motor code. for -1 it would be FFFF in hex. So, i'm thinking of to write a function that when i'm key in -1 it will return a hexadecimal FFFF to me. the i can extract this FFF into FF and FF so that FF can be stored in TH0 and FF can be stored in TL0. That's my problems. Thank u for replying me Neil..i'm appreciate ur help!
Take a look at the input functions in the standard C library. strtoul(), strtol(), scanf(), atoi(). The bit pattern for -1 in a 16-bit twos-complement integer is 1111111111111111 The bit pattern for 0xFFFF is 1111111111111111 The value is a matter of interpretation by the programmer; the processor does not care. Human-readable (and -writable) format is a matter for the I/O routines.
can you provide me the coding of how to convert hexa value to decimal. thanks, Regards, Kian Ching
"can you provide me the coding of how to convert hexa value to decimal." I'm guessing that 'hexa' means hexadecimal and that what you really mean is binary. I'm also guessing that when you say 'decimal' that you mean the ASCII representation of the binary value. If I have guessed correctly the function you want is probably sprintf().
If you want to "roll your own" you may find this: http://www.programmersheaven.com/zone5/cat27/34429.htm and this: http://www.programmersheaven.com/zone5/cat27/32816.htm useful. Enjoy.
"I'm guessing that 'hexa' means hexadecimal and that what you really mean is binary. I'm also guessing that when you say 'decimal' that you mean the ASCII representation of the binary value." Most likely! For an explanation of how numbers can be represented in various notations, and how ASCII-coded characters can be used to implement those representations, see: http://www.8052.com/forum/read.phtml?id=96256
try this, unsigned int motor_var=0x0102; TH0 = (motor_var >> 8); TL0 = (motor_var & 0xFF);
"try this, unsigned int motor_var=0x0102; TH0 = (motor_var >> 8); TL0 = (motor_var & 0xFF);" You really didn't read the thread, did you?
Oops. How's this? unsigned int motor_var= 1234; TH0 = (motor_var >> 8); TL0 = (motor_var & 0xFF); If one is entering the data via keyboard/monitor/host PC and communicating to the embedded controller over a serial port the ASCII string to integer conversion seems best done by the host application.
"... communicating to the embedded controller over a serial port the ASCII string to integer conversion seems best done by the host application." Maybe, maybe not. There are plenty of Gotchas! with sending pure binary data over serial links...
I've got some serial port code I tend to reuse or adapt as needed but it always includes some level of error detection and a retransmission request. Having the "What did you say?" command available forgives a lot of errors, of course the host application has to support this so again I usually write my own. The ASCII code wastes( or uses, depending on your viewpoint) a lot of bits to represent digits.