[英]How to convert uint32_t to double or float and print?
I'm using an analog to digital converter to read a voltage value from 0 to 3.3V. 我正在使用模数转换器读取0至3.3V的电压值。 The ADC gives an output from 0x00
to 0x0FFF
(0 to 4095). ADC提供从0x00
到0x0FFF
(0到4095)的输出。 I want to print the voltage reading with two decimal places. 我想用两位小数来打印电压读数。 I have read a number of answers but nothing I've tried has worked. 我已经阅读了许多答案,但没有尝试过。 Here's my code: 这是我的代码:
uint32_t pui32ADC0Value[1]; // ADC0 data value
double D0v; // Decimal value
// Read value from ADC
ADCSequenceDataGet(ADC0_BASE, 3, pui32ADC0Value);
// Convert to decimal
D0v = (double)pui32ADC0Value[0] / 4095 * 3.3;
// Print to console
UART0printf("\n\r> D0 = %.2d V", D0v);
The output I get is: D0 = ERROR2d V
我得到的输出是: D0 = ERROR2d V
pui32ADC0Value
prints without any problems and gives the expected values, so I know that it is working. pui32ADC0Value
可以pui32ADC0Value
打印并给出期望的值,所以我知道它正在工作。
What does this error mean? 这个错误是什么意思?
NOTE: UART0printf
is a pre-defined UART function on the TIVA TM4C microcontroller I am using. 注意: UART0printf
是我正在使用的TIVA TM4C微控制器上的预定义UART功能。
UPDATE: I wrongly assumed UART0printf
is identical to printf
, as I have used it before with %u
, %s
, and %x
. 更新:我错误地认为UART0printf
与printf
相同,因为我之前在%u
, %s
和%x
使用过它。 However for numbers it only supports %c
, %i
, and %u
. 但是,对于数字,它仅支持%c
, %i
和%u
。 I am re-working my code and will update if I don't get anywhere. 我正在重新编写我的代码,如果我什么都没收到,它将进行更新。
UPDATE 2: Could I do something like this? 更新2:我可以这样做吗? I know it will round incorrectly... 我知道它将四舍五入...
uint32_t pui32ADC0Value[1]; // ADC0 data value
uint32_t D0v[2]; // Decimal value
// Read value from ADC
ADCSequenceDataGet(ADC0_BASE, 3, pui32ADC0Value);
// Convert to decimal
for ( int i = 0; i <= 2; i++){
D0v[i] = (pui32ADC0Value[0]/10^i) / 4096 * 3.3;
}
// Display the AIN0 (PE0) digital value on the console.
UART0printf("\n\r> D0 = %d.%d%d V", D0v[0],D0v[1],D0v[2]);
If this function works printf
like: 如果此功能可以像printf
一样工作:
UART0printf("\n\r> D0 = %.2f V\n", D0v);
Also your code may work a little faster if you use: 如果您使用以下代码,您的代码也可能会更快地工作:
D0v = (double)pui32ADC0Value[0] * (3.3 / 4095);
The compiler will precompute 3.3 / 4095
and will do a single floating point multiply operation. 编译器将预先计算3.3 / 4095
并将执行单个浮点乘法运算。 Otherwise it's possible that the division is done first (which usually takes much more CPU cycles than a multiply) and then the multiply * 3.3
will be done. 否则,有可能首先完成除法(通常比乘除需要更多的CPU周期),然后再乘以* 3.3
。
Also since you seems to need only 2 digit precision, you could use float
(single precision) instead of double
. 另外,由于您似乎只需要2位精度,因此可以使用float
(单精度)而不是double
。 It may execute faster (but it's possible to be even slower), this depends on the platform. 它的执行速度可能更快(但可能会更慢),这取决于平台。
Update for UPDATE2 in the question: 问题中的UPDATE2更新:
Try this: 尝试这个:
uint32_t pui32ADC0Value[1]; // ADC0 data value
uint32_t D0v; // Decimal value
// Read value from ADC
ADCSequenceDataGet(ADC0_BASE, 3, pui32ADC0Value);
// Convert to millivolts
D0v = pui32ADC0Value[0] * (3300.0 / 4095);
// Display the AIN0 (PE0) digital value on the console.
UART0printf("\n\r> D0 = %d mV", D0v); // If it's OK for you to display in mV
UART0printf("\n\r> D0 = %d.%03d V", D0v / 1000, D0v % 1000); // Print in V
There is no such printf
format string as 没有这样的printf
格式字符串
%.2d
The formatter %d
means integer , so there are no decimal-precision options for it. 格式化程序%d
表示integer ,因此没有十进制精度选项。 The %.
%.
is being parsed as an error (as it does not exist), and the 2d
is making it verbatim into your output. 被解析为错误(因为它不存在),并且2d
将其逐字记录到您的输出中。
The formatter for floating-point values is %f
, so: 浮点值的格式为%f
,因此:
%.2f
However, all this is based on the assumption that UART0printf
, which you do not identify in your question whatsoever, is identical to printf
. 但是,所有这些都是基于这样的假设: UART0printf
您在问题中没有发现的UART0printf
与printf
相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.