We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
just as titleï¼ now i'am use this method: a=value%1000/100; b=value%100/10; c=value%10; but it's slow,any faster methods? thanksï¼
Don't see any BCD in your question. BCD would store the value 0..9 in four bits, which means that the conversion would be: a = (value >> 8) & 0x0f; b = (value >> 4) & 0x0f; c = value & 0x0f;
In your case, you seem to have a binary value 0..999 stored in a 16-bit integer and want to get out three decimal digits.
The alternative to using div instructions is to do a scan conversion. Basically (a non-optimized program to show the concept):
#include <stdint.h> #include <stdio.h> void convert(uint16_t value) { uint8_t a = 0,b = 0,c = 0; uint8_t a_tmp = 1,b_tmp = 0,c_tmp = 0; uint8_t overflow = 0; int bits_processed = 0; printf("value:%4u -> ",value); while (value) { // Find all non-zero bits in input value //printf("value %u %u %u %u\n",value,c_tmp,b_tmp,a_tmp); if (value & 1) { // We know the value of this bit, so add to our ouput bits. a += a_tmp; b += b_tmp; c += c_tmp; // Catch any decimal overflow to next digit. if (a >= 10) { a -= 10; b++; } if (b >= 10) { b -= 10; c++; } if (c >= 10) { c -= 10; overflow++; } } value >>= 1; if (!value) break; // Next bit to test is worth twice as much. a_tmp <<= 1; b_tmp <<= 1; c_tmp <<= 1; if (a_tmp >= 10) { a_tmp -= 10; b_tmp++; } if (b_tmp >= 10) { b_tmp -= 10; c_tmp++; } if (c_tmp >= 10) { c_tmp -= 10; overflow++; } if (++bits_processed >= 16) { printf("Internal error - hung algorithm\n"); return; } } printf(" %u %u %u",c,b,a); if (overflow) printf(" OVERFLOW!"); printf("\n"); } int main(void) { convert(1); convert(9); convert(11); convert(99); convert(117); convert(999); convert(4692); convert(9999); convert(54321u); convert(65535u); return 0; }
#define DIM(a) (sizeof(a) / sizeof(a[0])) void bin2bcd(unsigned long bin, unsigned char *bcd) { static const unsigned long pow_ten_tbl[] = { 1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1 }; unsigned long pow_ten; unsigned char digit; unsigned char i; for (i = 0; i != DIM(pow_ten_tbl); i++) { digit = 0; pow_ten = pow_ten_tbl[i]; while (bin >= pow_ten) { bin -= pow_ten; digit++; } *bcd++ = digit; } }
thanks,i well try it immediately。
i'am try you code,but it can't build success. Build target 'Target 1' compiling u32tobcd.c... U32TOBCD.C(23): warning C206: 'DIM': missing function-prototype U32TOBCD.C(23): error C267: 'DIM': requires ANSI-style prototype Target not created
my test code:
#include <reg52.h> #include <math.h> unsigned char bcdt[10]={0,0,0,0,0,0,0,0,0,0}; unsigned char *bcd; void bin2bcd(unsigned long bin, unsigned char *bcd) { static const unsigned long pow_ten_tbl[] = { 1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1 }; unsigned long pow_ten; unsigned char digit; unsigned char i;
for (i = 0; i != DIM(pow_ten_tbl); i++) { digit = 0; pow_ten = pow_ten_tbl[i];
while (bin >= pow_ten) { bin -= pow_ten; digit++; } *bcd++ = digit; } }
void main(void) { while(1) { bin2bcd(1234567,*bcd); } }
converter 4294967295 dec to BCD just cost 3770us @ 12MHz normal 8051 MCU.
"BCD would store the value 0..9 in four bits"
Some people would call that packed BCD
Yes, it would be packed when you have multiple BCD digits in the same word.
But a non-packed BCD digit would still have the values 0..9 and require four bits.
Now cross-posted at: www.8052.com/.../164580
I do not have malicious,I just want to know that no one have a faster way And willing to share. If I were to do so will lead to unpleasant I will stop this kind of behavior
Hi ou fuqiang,
I come from Taiwan. Nice to meet you.
If you don't mind, I have some suggestions for you.
When you post something in this KEIL forum, please: NOTE: When posting messages, use the following HTML tags to make your message easier to read and understand.
These HTML tags "b", "i", "pre" are described in the above of your message field, where you type your message.
And, when you press the "Preview" button, you shoule preview your post to make sure the format/style/indent is appropriate. This is more important when you post your source code. We need to make our posts be easy to read/understand.
And, if you post your questions twice or more in different forums, people will need to read/answer your questions twice or more, this is wasting people's precious time/resource. So, it is inappropriate.
"And, if you post your questions twice or more in different forums, people will need to read/answer your questions twice or more, this is wasting people's precious time/resource. So, it is inappropriate."
There is nothing wrong in asking the same question on two forums if you cross-link the forums so that users on one forum are aware of the responses people have already given on another forum.
Without the cross-posting links, many people will spend time giving similar answers, and that will waste not only bandwidth but also the time of the people who do spend time helping out.
Since time is a scarce resource, help is an even more valuable and limited resource.
This is my wrong,I have always thought that browse forum is only when they have free time. this is different between china.
I'am at 24'20'24.38"N,109'23'17.71"E.
Yes, people browse forums when they have free time.
But see it this way: If a student has a problem at school, he may ask the teacher for help. After he has received help, should he then find another teacher and once more ask for help with the same problem - just checking that the two teachers gave the same answer?
Wouldn't that waste the time for the teachers - time that they could have used to help someone else?
That is the reason why you should always add links between the forums if you post a question on multiple sites.
Since you had received two possible solutions to your question, you could have linked to this forum, and asked at 8052.com: Anyone know any better solution than these two, or how they can be rewritten to better take advantage of my specific processor?
Please do not angry, I just started browsing forums abroad. lot of Forum habits i don't know. O(-_-)O~ design of electronic products not my job. now I am DIY electric bicycle speed table. this is my hobbies. the hall sensor given the pulse so fast that i have to find a fast Conversion code.