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
  • I would strongly recommend not using floats inside an ISR at all. Aside from the fact that any operation on a float is fairly time consuming it involves calls to library functions which I would guess are not reentrant. If this is the case your ISR could corrupt any floating point calculations anywhere in your program, not just ones involving the FP variable modified in the ISR.

    Whenever you need to share any multibyte variable between an ISR and the rest of your program you must declare it as volatile and also disable interrupts while accessing it outwith the ISR. I like to use a function similar to this:

    long ReadSharedLong(long *shared)
    {
    long temp;

    EA=0;
    temp=*shared;
    EA=1;

    return(temp);
    }

    Stefan

Reply
  • I would strongly recommend not using floats inside an ISR at all. Aside from the fact that any operation on a float is fairly time consuming it involves calls to library functions which I would guess are not reentrant. If this is the case your ISR could corrupt any floating point calculations anywhere in your program, not just ones involving the FP variable modified in the ISR.

    Whenever you need to share any multibyte variable between an ISR and the rest of your program you must declare it as volatile and also disable interrupts while accessing it outwith the ISR. I like to use a function similar to this:

    long ReadSharedLong(long *shared)
    {
    long temp;

    EA=0;
    temp=*shared;
    EA=1;

    return(temp);
    }

    Stefan

Children