On Nov 18, 7:19 pm, "John Speth" <
[email protected]> wrote:
> Hi folks-
>
> I'm in the beginning stages of crafting a high-speed measurement device
> which needs to output a floating point value in ASCII string form that
> represents the measurement at a rate of 10 kHz. I'm using an Altera FPGA
> that runs Nios2. Convenient standard library functions like sprintf() or
> ftoa() running as code will be too time-consuming to meet my high throughput
> requirements. What I need is an ultrafast float to ASCII conversion
> function that can run in code *or* a strategy for implementing a conversion
> function in HDL. Altera has a nice tool called C to HDL compiler which I'm
> looking at now.
>
> It seems to me that a fast float to ASCII conversion function would be a
> common function of many embedded systems. Rather than me reinventing the
> wheel, can anyone point me to a resource (example on the web or a product
> for sale) that I can use to achieve my goal?
>
> Thanks, John Speth.
It seems your task not just doesn't require floating-point to ASCII
conversion, it needs no floating point at all. I'd go as far as to say
that even if there was fast floating point hardware I'd still prefer
to not use it for that type of task.
Use fix point!
Suppose, you have 14-bit ADC with 3V unipolar scale and you want to
present the result in mV with three digits after decimal point. All
you need to do is something like:
const int ADC_CODE_TO_uV_NUM = int(3E6);
const int ADC_CODE_TO_uV_DEN = 1<<14;
unsigned result_uV = (unsigned)(((long long)result_code *
ADC_CODE_TO_uV_NUM*2 + ADC_CODE_TO_uV_DEN)/(ADC_CODE_TO_uV_DEN*2));
char result_uV_str[32];
int len1 = siprintf(result_uV_str, "%04u", result_uV);
char result_mV_str[32];
memcpy(result_mV_str, result_uV_str, len1 - 3);
result_mV_str[len1 - 3] = '.';
memcpy(&result_mV_str[len1 - 3 + 1], &result_uV_str[len1 - 3], 3);
result_mV_str[len1 + 1] = 0;