Electronics > Microcontrollers

Routines to convert binary to BCD in C code

(1/6) > >>

Picuino:
Binary to decimal BCD conversion routines have many applications. The first application I give them in new systems is to send by UART binary numbers to the computer in decimal format.
The code is written in C for Arduino, but is easily translatable to another environment.


16 bits binary to 5 digits BCD:


--- Code: (C) ---#define BINARY_DIGITS   16
#define DECIMAL_DIGITS  5


void bin16_to_str(char *str, unsigned int bin) {
   char maxdig, digcnt, bitcnt;
   static char *p, carry;
 
   // Clear string
   p = str;
   digcnt = DECIMAL_DIGITS + 1;
   do { *p++ = 0; } while (--digcnt);
 
   // Transform binary to BCD
   bitcnt = BINARY_DIGITS;
   maxdig = (DECIMAL_DIGITS - 1);
   str += (DECIMAL_DIGITS - 1);
   do {
      // Shift left binary number with carry
      carry = 0;
      if (bin & (1L<<(BINARY_DIGITS-1)))
         carry |= 1;
      bin <<= 1;

      // Shift left decimal number with carry
      p = str;
      digcnt = DECIMAL_DIGITS - maxdig;
      do {
         carry = (*p<<1) + carry;
         if (carry >= 10) {
            *p-- = carry - 10;
            carry = 1;
            if (digcnt == 1) {
               maxdig--;
               digcnt++;
            }
         }
         else {
            *p-- = carry;
            carry = 0;
         }
      } while(--digcnt);
   } while(--bitcnt);

   // Transform BCD to ASCII
   digcnt = DECIMAL_DIGITS;
   do *str-- += '0'; while (--digcnt);
}


void setup() {
   Serial.begin(9600);
}


void loop() {
   unsigned long timeit;
   unsigned long bin;
   char strnum[DECIMAL_DIGITS + 1];
   strnum[DECIMAL_DIGITS] = 0; // End of char

   timeit = millis();
   for(bin=10000; bin<11000; bin++) {
      bin16_to_str(strnum, bin);
   }
   timeit = millis() - timeit;
   Serial.print("1 conversion in ");
   Serial.print(timeit);
   Serial.println(" microseconds");
   delay(5000);
   
   for(bin=0; bin<50000; bin++) {
      bin16_to_str(strnum, bin);
      Serial.println(strnum);
   }
}

--- End code ---

Output:

--- Code: ---1 Conversion in 61 microseconds
00000
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
--- End code ---

Size of flash memory (function bin16_to_str) = 2084 - 1916 = 168 Bytes

=======================================================

32 bits binary to 10 digits BCD:


--- Code: (C) ---#define BINARY_DIGITS   32
#define DECIMAL_DIGITS  10


void bin32_to_str(char *str, unsigned long bin) {
   char maxdig, digcnt, bitcnt;
   static char *p, carry;
 
   // Clear string
   p = str;
   digcnt = DECIMAL_DIGITS + 1;
   do { *p++ = 0; } while (--digcnt);
 
   // Transform binary to BCD
   bitcnt = BINARY_DIGITS;
   maxdig = (DECIMAL_DIGITS - 1);
   str += (DECIMAL_DIGITS - 1);
   do {
      // Shift left binary number with carry
      carry = 0;
      if (bin & (1L<<(BINARY_DIGITS-1)))
         carry |= 1;
      bin <<= 1;

      // Shift left decimal number with carry
      p = str;
      digcnt = DECIMAL_DIGITS - maxdig;
      do {
         carry = (*p<<1) + carry;
         if (carry >= 10) {
            *p-- = carry - 10;
            carry = 1;
            if (digcnt == 1) {
               maxdig--;
               digcnt++;
            }
         }
         else {
            *p-- = carry;
            carry = 0;
         }
      } while(--digcnt);
   } while(--bitcnt);

   // Transform BCD to ASCII
   digcnt = DECIMAL_DIGITS;
   do *str-- += '0'; while (--digcnt);
}


void setup() {
   Serial.begin(9600);
}


void loop() {
   unsigned long timeit;
   unsigned long bin;
   char strnum[DECIMAL_DIGITS + 1];
   strnum[DECIMAL_DIGITS] = 0; // End of char

   timeit = millis();
   for(bin=0x80000000; bin<0x80000000+1000; bin++) {
      bin32_to_str(strnum, bin);
   }
   timeit = millis() - timeit;
   Serial.print("1 Conversion in ");
   Serial.print(timeit);
   Serial.println(" microseconds");
   delay(5000);
   
   for(bin=0; bin<100000; bin++) {
      bin32_to_str(strnum, bin);
      Serial.println(strnum);
   }
}

--- End code ---

Output:

--- Code: ---1 Conversion in 234 microseconds
0000000000
0000000001
0000000002
0000000003
0000000004
0000000005
0000000006
0000000007
0000000008
0000000009
0000000010
0000000011
0000000012
0000000013
0000000014
0000000015
0000000016
0000000017
0000000018
0000000019
0000000020
0000000021
0000000022
0000000023
0000000024
0000000025
0000000026
0000000027
0000000028
0000000029
0000000030
0000000031
0000000032
0000000033
0000000034
0000000035
0000000036
0000000037
0000000038
0000000039
0000000040
0000000041
0000000042
0000000043
0000000044
0000000045
0000000046
0000000047
0000000048
0000000049
0000000050
0000000051
0000000052
0000000053
0000000054
0000000055
--- End code ---

Size of flash memory (function bin32_to_str)= 2154 - 1926 = 228 Bytes

Picuino:
8 bits binary to 3 digits BCD:


--- Code: ---#define BINARY_DIGITS   8
#define DECIMAL_DIGITS  3


void bin8_to_str(char *str, unsigned char bin) {
   char maxdig, digcnt, bitcnt;
   static char *p, carry;
 
   // Clear string
   p = str;
   digcnt = DECIMAL_DIGITS + 1;
   do { *p++ = 0; } while (--digcnt);
 
   // Transform binary to BCD
   bitcnt = BINARY_DIGITS;
   maxdig = (DECIMAL_DIGITS - 1);
   str += (DECIMAL_DIGITS - 1);
   do {
      // Shift left binary number with carry
      carry = 0;
      if (bin & (1L<<(BINARY_DIGITS-1)))
         carry |= 1;
      bin <<= 1;

      // Shift left decimal number with carry
      p = str;
      digcnt = DECIMAL_DIGITS - maxdig;
      do {
         carry = (*p<<1) + carry;
         if (carry >= 10) {
            *p-- = carry - 10;
            carry = 1;
            if (digcnt == 1) {
               maxdig--;
               digcnt++;
            }
         }
         else {
            *p-- = carry;
            carry = 0;
         }
      } while(--digcnt);
   } while(--bitcnt);

   // Transform BCD to ASCII
   digcnt = DECIMAL_DIGITS;
   do *str-- += '0'; while (--digcnt);
}


void setup() {
   Serial.begin(9600);
}


void loop() {
   unsigned long timeit;
   unsigned char bin;
   char strnum[DECIMAL_DIGITS + 1];
   strnum[DECIMAL_DIGITS] = 0; // End of char

   timeit = millis();
   for(bin=0; bin<255; bin++) {
      bin8_to_str(strnum, bin);
   }
   timeit = millis() - timeit;
   Serial.print("1 conversion in ");
   Serial.print(timeit/2.55);
   Serial.println(" microseconds");
   delay(5000);
   
   for(bin=0; bin<255; bin++) {
      bin8_to_str(strnum, bin);
      Serial.println(strnum);
   }
}

--- End code ---



--- Code: ---1 conversion in 2.75 microseconds
000
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

--- End code ---

tridac:
This is one of my standrd library functions. Edit as required...

/* Convert Binary to Bcd, Msd First */
/* -------------------------------- */
void vBin2Bcd (U32 u32Value, U8 *pu8BcdArray, U8 u8Count) {         /* BcdArray has msd at array[0] on return */

U8 u8Index;

if (u32Value > 0) {
   for (u8Index = 1; u8Index <= u8Count; u8Index++) {
      pu8BcdArray [u8Count - u8Index] = (U8) (u32Value % 10);       /* Buffer remainder */
      u32Value /= 10;                                               /* Next decade */
      }
   }

else {                                                              /* Don't convert zero */
   for (u8Index = 0; u8Index < u8Count; u8Index++) {
      pu8BcdArray [u8Index] = 0;                                    /* Clear output */
      }
   }


A printing version:

/* Convert and Display 32 Bit Value as Decimal */
/* ------------------------------------------- */
void vPutDecimal (U32 u32Value) {

U8 u8Count = BUF_SIZE - 1;

u8Buffer [u8Count--] = 0x00;                                            /* Terminate string */

do {                                                                    /* First, split number into individual digits */
   u8Buffer [u8Count--] = (U8) ((u32Value % 10) + 0x30);                /* Get remainder, asciify & buffer */
   } while ((u32Value /= 10) > 0 && u8Count > 0);                       /* Next decade, trap buffer overflow */

u8Count++;                                                              /* Correct count */
vPutString (&u8Buffer [u8Count]);                                       /* Display, left justified */
}

Picuino:

--- Quote from: tridac on March 27, 2024, 03:05:19 pm ---This is one of my standrd library functions. Edit as required...

/* Convert Binary to Bcd, Msd First */
/* -------------------------------- */
void vBin2Bcd (U32 u32Value, U8 *pu8BcdArray, U8 u8Count) {         /* BcdArray has msd at array[0] on return */

U8 u8Index;

if (u32Value > 0) {
   for (u8Index = 1; u8Index <= u8Count; u8Index++) {
      pu8BcdArray [u8Count - u8Index] = (U8) (u32Value % 10);       /* Buffer remainder */
      u32Value /= 10;                                               /* Next decade */
      }
   }

else {                                                              /* Don't convert zero */
   for (u8Index = 0; u8Index < u8Count; u8Index++) {
      pu8BcdArray [u8Index] = 0;                                    /* Clear output */
      }
   }


--- End quote ---

Good and very short and simple routine.

My rutine, on the other hand, it does not make use of integer arithmetic ( value%10 and value /= 10) it only makes use of binary number shifts.
This means that it can generate shorter and faster assembly code, especially on small micros that do not have large ALUs nor FPUs.

Picuino:
My routine of 32 bits use at Arduino: 228 bytes of flash,  234us/conversion

Other routine of 32 bits use at Arduino: 156 bytes of flash,  380us/conversion


It seems that in the end the routine with divisions by 10 is shorter, despite the integer arithmetic required.  :-//


Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod