This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Integer to Floating conversion error

I get strange behaviour with integer conversion; in the following example T4Frozen never reaches the value 790 because it turn back to 700 when it step over 767. This happen both on the target and on Dscope.

 T4Frozen=700;
 while(TRUE){
   PeriodoEncoder=(float)T4Frozen;
   PeriodoEncoder/=2.5;
   T4Frozen++;
   if(T4Frozen>790){
     Dummy=700;
   }
 }
Thanks in advance for your answer

Parents
  • Have you declared it volatile?

    "Is it possible that a 32 bit float is affected by ISR during main loop reading in the way that low 16 bit are 'old' and the high 16 bit are 'new'?"

    Yes: of course that is entirely possible - due to the very nature of the way interrupts work!

    And, of course, it's even worse with floats because the processor has to do a lot of work to handle them - so even more chance for an interrupt to happen in the middle of it!

    "If so, which treatments can I adopt to avoid this problem?"

    Err - disable the interrupt...?

    BTW: do you really need to use floating point?
    Have you considered fixed-point?
    (wouldn't make any difference to this particular problem, though).

Reply
  • Have you declared it volatile?

    "Is it possible that a 32 bit float is affected by ISR during main loop reading in the way that low 16 bit are 'old' and the high 16 bit are 'new'?"

    Yes: of course that is entirely possible - due to the very nature of the way interrupts work!

    And, of course, it's even worse with floats because the processor has to do a lot of work to handle them - so even more chance for an interrupt to happen in the middle of it!

    "If so, which treatments can I adopt to avoid this problem?"

    Err - disable the interrupt...?

    BTW: do you really need to use floating point?
    Have you considered fixed-point?
    (wouldn't make any difference to this particular problem, though).

Children
No data