cancel
Showing results for 
Search instead for 
Did you mean: 

MCU stuck when convert point to double

nicekwell
Associate II

There is a array:

uint8_t dat[8] = {0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xdc, 0x5e, 0x40};

It means a double variable data in memery. The value is 123.45.

This code can be run in my computer:

int main(int argc, char *argv[])

{

  uint8_t dat[] = {0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xdc, 0x5e, 0x40};

  double test = *((double*)dat);

  printf("%.3f\r\n", test);

  return 0;

}

It will put: 123.450

But on stm32f103rct6. It will stuck when I run this:

 double test = *((double*)dat);

But use float will not stuck:

 float test = *((float*)dat);

So, how can I convert memery to double rightly on stm32f103? (I use stm32cubeide)

Thanks!

11 REPLIES 11

Thank you! I have solved this problem by making a buffer.

0693W000008xmTpQAI.pngBut I still don't understand why.

'dat' is given by other progress. I'v printed and checked the content is right. But I'll get hardfault.

'buf' is copied from 'dat'. And it works fine.

Copying one uint8_t array to another array of the same type will not solve anything. You will have to learn about alignment and unions. 🙂 But you can start with an example from there:

https://stackoverflow.com/questions/8072238/decomposition-of-double-precision-float-into-bytes-using-union-in-c