Hi, I try to read value of timer of STM32L152VD and convert it to float type. The way I use is first define the below union:
typedef union { uint32_t timercount; float flowtime; }union_timercount; union_timercount flow;
then use function below:
uint32_t TIM_GetCounter(TIM_TypeDef* TIMx) { /* Check the parameters */ assert_param(IS_TIM_ALL_PERIPH(TIMx)); /* Get the Counter Register value */ return TIMx->CNT; }
I send it to serial port:
flow.timercount=TIM_GetCounter(TIM2); sprintf(str1,"\r\nu=%u f=%f",flow.timercount,flow.flowtime); uart_out_str(str1);
but what I read is: u=2248 f=0.000000 why float value is not being converted?
#include <stdio.h> #include <string.h> // unsigned long long data = 0x4030000000000000; unsigned long long data = 0x402699999999999A; double double_value; unsigned int dat = 2248; /* added for this case */ float float_val; /* added for this case */ int main(void) { printf( "double=%d, long long=%d\n", sizeof(double), sizeof(long long) ); memcpy( &double_value, &data, 8 ); printf( "double_value = %lf\n", double_value ); float_val = dat; /* added for this case */ printf( "float_val = %f\n", float_val ); /* added for this case */ }
#include <stdio.h> #include <string.h> // unsigned long long data = 0x4030000000000000; unsigned long long data = 0x402699999999999A; double double_value; int main(void) { printf( "double=%d, long long=%d\n", sizeof(double), sizeof(long long) ); memcpy( &double_value, &data, 8 ); printf( "double_value = %.22lf\n", double_value ); }
double=8, long long=8 double_value = 11.3000000000000010000000 Process returned 41 (0x29) execution time : 0.022 s Press any key to continue.