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ï¼
#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; } }
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.