Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----------------------------------------------------------------------------
- // currentToPower(u16 *power, u16 current)
- //-----------------------------------------------------------------------------
- // Function calculates the heater power corresponding to the current flowing
- // through it.
- //
- // Parameters:
- // *power - pointer to power output variable [mW]
- // current - heater current input variable [mA * 2^4]
- //-----------------------------------------------------------------------------
- void currentToPower(u16 *power, u16 current)
- {
- static u32 temp;
- // Current is in 10.4 fixed-point format.
- // Square the current.
- // One of the factors needs to be cast as u32 to yield a u32 result.
- temp = (u32)current * current;
- // The result of the multiplication is 20.8 fixed-point.
- // Clip the 8 LSB to get whole mA^2 value.
- // This is temp = temp>>8, but Keil is stupid.
- *(((u8 *) &temp)+3) = *(((u8 *) &temp)+2);
- *(((u8 *) &temp)+2) = *(((u8 *) &temp)+1);
- *(((u8 *) &temp)+1) = *((u8 *) &temp);
- *((u8 *) &temp) = 0;
- // Multiply by heater resistance.
- // Rheat is in 6.6 fixed-point format, scaled by 1.024 (!NOT 1024!).
- temp *= RHEAT;
- // Result is in uW in 26.6 fixed-point format.
- // Clip the bottom 6 bits to get a whole value plus
- // 10 additional bits to convert directly to mW.
- // 16 LSB total are clipped from the above result.
- // This is the reason for having Rheat scaled by 1.024 earlier.
- *power = *((u16 *) &temp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement