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

Unknown problem

Hi folks,
I have tried the following source code and it seemed not behaving what I was expected

 
#define Const1 (10)
#define Const2 (Const1/4096)
#define Base (2850)

unsigned int g_wData;
float g_fA = Base;
float g_fB = Base;

u8 IsChanged(void)
{ 
  g_fA = Base + (Const2 *g_wData);
   /*if the two value are the same,just return 0*/
   /*else return 1 and assign the new value to...*/
   if(g_fA == g_fB )
   {
	return (0);
   }
   g_fB =  g_fA ;
   return (1);
}


Algorithm:
Two float point global variables to hold some sort of decimal values.
The content of unsigned integer Data changes some where in the program dynamically.
This function always return (0) even though the g_wData value changes.
Do anybody have any suggestion(s)?

P.S. I have make the g_wData variable volatile, and it didn't help.

  • The values you are using in the macro definitions are integers and thus, when used, Const2 yields a subexpression value of zero and g_fA will always equal g_fB.

    Change:

    #define Const1 (10)
    To:
    #define Const1 (10.0)

    Or change:
    #define Const2 (Const1/4096)
    To:
    #define Const2 (Const1/4096.0)

    Or change both.

    --Dan Henry

  • Open the option of the target and go to the "c51" page, try to select a bigger number than 3 in the "bits to round for float" column.
    ps. The Const1 and Const2 should be defined with float numeric.
    A floating-point compare is not a good idea for a mcu program.
    Try to use the "long int" type instead of it.