Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.45 KB | None | 0 0
  1. //-----------------------------------------------------------------------------
  2. // currentToPower(u16 *power, u16 current)
  3. //-----------------------------------------------------------------------------
  4. // Function calculates the heater power corresponding to the current flowing
  5. // through it.
  6. //
  7. // Parameters:
  8. // *power - pointer to power output variable [mW]
  9. // current - heater current input variable [mA * 2^4]
  10. //-----------------------------------------------------------------------------
  11. void currentToPower(u16 *power, u16 current)
  12. {
  13.     static u32 temp;
  14.     // Current is in 10.4 fixed-point format.
  15.     // Square the current.
  16.     // One of the factors needs to be cast as u32 to yield a u32 result.
  17.     temp = (u32)current * current;
  18.  
  19.     // The result of the multiplication is 20.8 fixed-point.
  20.     // Clip the 8 LSB to get whole mA^2 value.
  21.     // This is temp = temp>>8, but Keil is stupid.
  22.     *(((u8 *) &temp)+3) = *(((u8 *) &temp)+2);
  23.     *(((u8 *) &temp)+2) = *(((u8 *) &temp)+1);
  24.     *(((u8 *) &temp)+1) = *((u8 *) &temp);
  25.     *((u8 *) &temp) = 0;
  26.  
  27.     // Multiply by heater resistance.
  28.     // Rheat is in 6.6 fixed-point format, scaled by 1.024 (!NOT 1024!).
  29.     temp *= RHEAT;
  30.  
  31.     // Result is in uW in 26.6 fixed-point format.
  32.     // Clip the bottom 6 bits to get a whole value plus
  33.     // 10 additional bits to convert directly to mW.
  34.     // 16 LSB total are clipped from the above result.
  35.     // This is the reason for having Rheat scaled by 1.024 earlier.
  36.     *power = *((u16 *) &temp);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement