I'm using an analog to digital converter to read a voltage value from 0 to 3.3V. The ADC gives an output from 0x00
to 0x0FFF
(0 to 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
pui32ADC0Value
prints without any problems and gives the expected values, so I know that it is working.
What does this error mean?
NOTE: UART0printf
is a pre-defined UART function on the TIVA TM4C microcontroller I am using.
UPDATE: I wrongly assumed UART0printf
is identical to printf
, as I have used it before with %u
, %s
, and %x
. However for numbers it only supports %c
, %i
, and %u
. I am re-working my code and will update if I don't get anywhere.
UPDATE 2: Could I do something like this? 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]);