| ??? 10/02/02 14:04 Read: times |
#30028 - RE: 16 bit hex to bcd conversion |
Here under is what I've done to convert a 32 bits value contained in a variable to ASCII in order to display on an LCD or to send to an UART.
The following code is compiled with Keil and return the ASCII value to the UART. It will be easily modified. Just call the function, with the variable value to convert, the number of digits before the dot and the number of digits after the dot. When this last value is set to 0, the dot isn't displayed. I've wrote it to display the frequency of an HF receiver. It can display the non significant zeros.... Regards Stephane typedef signed char S8; /* 8-Bit Signed. */ typedef unsigned char U8; /* 8-Bit Unsigned. */ typedef signed short S16; /* 16-Bit Signed. */ typedef unsigned short U16; /* 16-Bit Unsigned. */ typedef signed long S32; /* 32-Bit Signed. */ typedef unsigned long U32; /* 32-Bit Unsigned. */ /*------------------------------------------------------------------- Convert a digital value into an ASCII string UART_DisplayValue (U32 u32Value, U8 u8NumberOfIntegerDigits, U8 u8NumberOfDecimalDigits) u32Value = 32 bits Value to display u8NumberOfIntegerDigits = Number of interger digits. If this number is higher than the number of integer digits, it will display the non significant '0' u8NumberOfDecimalDigits = Number of decimal digits --------------------------------------------------------------------*/ void UART_DisplayValue (U32 u32Value, U8 u8NumberOfIntegerDigits, U8 u8NumberOfDecimalDigits) { U8 u8Size; /* Size of the table */ U8 au8ValueToDisplay[10]; /* String where is converted the value */ S8 s8CharNumber; /* Character number in the string */ /* Check if the dot has to be displayed and change the Size value */ if (u8NumberOfDecimalDigits == 0) { u8Size = u8NumberOfIntegerDigits + u8NumberOfDecimalDigits; } else { u8Size = u8NumberOfIntegerDigits + u8NumberOfDecimalDigits + 1; } /* Last value of the table = 0 */ au8ValueToDisplay[u8Size] = 0; /* Convert the value to an ASCII string */ for(s8CharNumber=u8Size - 1;s8CharNumber>=0;s8CharNumber--) { if ((s8CharNumber == u8Size - 1 - u8NumberOfDecimalDigits) && u8NumberOfDecimalDigits !=0) { au8ValueToDisplay[s8CharNumber] = '.'; } else { au8ValueToDisplay[s8CharNumber] = u32Value % 10 + '0'; u32Value /= 10; } } /* Display the string */ UART_Printf(au8ValueToDisplay); } |
| Topic | Author | Date |
| 16 bit hex to bcd conversion | 01/01/70 00:00 | |
| RE: 16 bit hex to bcd conversion | 01/01/70 00:00 | |
| RE: 16 bit hex to bcd conversion | 01/01/70 00:00 | |
| RE: 16 bit hex to bcd conversion | 01/01/70 00:00 | |
| RE: 16 bit hex to bcd conversion | 01/01/70 00:00 | |
RE: 16 bit hex to bcd conversion | 01/01/70 00:00 |



