ashugtiwari
Full Member level 4
- Joined
- Dec 12, 2008
- Messages
- 218
- Helped
- 45
- Reputation
- 90
- Reaction score
- 44
- Trophy points
- 1,308
- Activity points
- 3,310
Hello All,
Im working on BLDC motor speed control project using MCU. So, i have declared some of #defines as follows:
#define RATED_SPEED 8500L
#define Vmax 9L
#define Vmotor 24L
#define Q12_MAX 4095L
#define SPEED_CONV 5000000L
#define SPEED_Vmax (int)((RATED_SPEED*Vmax/Vmotor)+100L)
#define COUNT_TO_SPEED (long)((SPEED_CONV*Q12_MAX)/SPEED_Vmax)
As, at compile time, all other formulae's are evaluated to their integer value but above two constants are resulting into float value. The first formula is giving an output of 3287.5 and next one is 6229084.2713720717979920900517189 when SPEED_Vmax is truncated to 3287.
So, when keeping these values directly into #define, my system and feedback control works just fine but when i use the formula's directly, the system feedback controls does not work correctly. Why is it happening? And is there a way i can directly use the formula and round these values, so that i can also use these values into other #defines? As far as i know compiler only does integer math. Im using MPLABX with XC8 compiler.
Any ideas? The project is confidential so i cannot share any more info, i hope this explains my question well.
Thanks for replies
Ashutosh
Im working on BLDC motor speed control project using MCU. So, i have declared some of #defines as follows:
#define RATED_SPEED 8500L
#define Vmax 9L
#define Vmotor 24L
#define Q12_MAX 4095L
#define SPEED_CONV 5000000L
#define SPEED_Vmax (int)((RATED_SPEED*Vmax/Vmotor)+100L)
#define COUNT_TO_SPEED (long)((SPEED_CONV*Q12_MAX)/SPEED_Vmax)
As, at compile time, all other formulae's are evaluated to their integer value but above two constants are resulting into float value. The first formula is giving an output of 3287.5 and next one is 6229084.2713720717979920900517189 when SPEED_Vmax is truncated to 3287.
So, when keeping these values directly into #define, my system and feedback control works just fine but when i use the formula's directly, the system feedback controls does not work correctly. Why is it happening? And is there a way i can directly use the formula and round these values, so that i can also use these values into other #defines? As far as i know compiler only does integer math. Im using MPLABX with XC8 compiler.
Any ideas? The project is confidential so i cannot share any more info, i hope this explains my question well.
Thanks for replies
Ashutosh