B_f = (freq_khz * freq_khz)/10;
HC08LB8 arithmetic operation
B_f = (freq_khz * freq_khz)/10;
Hello rceit,
I have now actually tried out your equations in the simulator, and obtained the expected results for both A_f and B_f variables, with freq_khz firstly set for 40, and then for a value of 120. I declared all three variables as unsigned int.
The following results were obtained:
freq_khz A_f B_f
40 8768 160
120 10016 1440
Regards,
Mac
Message Edited by rceit on 2006-06-11 05:44 AM
/***************************************************************************** Intrinsic functions definition*****************************************************************************/SByte Add_8(SByte x, SByte y); /* source sdkmath.asm */SByte Lim_8(SByte x, SByte limit); /* source sdkmath.c */SByte Neg_8(SByte x); /* source sdkmath.asm */SByte Shl_8(SByte x, UByte n); /* source sdkmath.asm */SByte Sub_8(SByte x, SByte y); /* source sdkmath.asm */SWord16 Add(SWord16 x, SWord16 y); /* source sdkmath.asm */SWord16 Lim(SWord16 x, SWord16 limit); /* source sdkmath.c */SWord16 Neg(SWord16 x); /* source sdkmath.asm */SWord16 Shl(SWord16 x, UByte n); /* source sdkmath.asm */SWord16 Sub(SWord16 x, SWord16 y); /* source sdkmath.asm */UWord16 Umul_16x8 (UWord16 x, UByte y); /* source sdkmath.c */SWord16 Smul_16x8 (SWord16 x, UByte y); /* source sdkmath.c */SWord16 Smul_8(SByte x, UByte y); /* source sdkmath.c */SByte Sdiv_8(SWord16 x, UByte y); /* source sdkmath.asm */UByte Udiv_16to8(UWord16 x, UWord16 y); /* source sdkmath.asm */
Message Edited by Alban on 2006-06-12 10:18 AM
Hello Mac,
The problem of overflow is solved for B_f, but it continues with :
volt_pk_T1_bin = (A_f * volt_T1 + B_f)/100;
and any change done an inaceptable loss of resolution. The problem will be the mathematical functions described in 2006-06-1202:59 AMreply?
Hello rceit,
rceit wrote:
The problem of overflow is solved for B_f, but it continues with :
volt_pk_T1_bin = (A_f * volt_T1 + B_f)/100;
and any change done an inaceptable loss of resolution.
Why do you not use the CW library for integer mathematical functions where I assume long variables can be handled?
You do not mention the range of values expected for volt_T1 variable. You imply that this is equal to an address - or should this be the contents of an address - but this is not clear. According to my calculations the range of values for A_f is 8768 to 10016, and for B_f the range is 1430 to 2598. You may want to divide each of these by 100 prior to multiplying the first term by the volt_T1 value, with a maximum allowed value of about 655 when A_f and B_f have their maximum values. So the formula might be re-arranged as follows:
volt_pk_T1_bin = ((A_f + 50)/100 * volt_T1 + (B_f + 50)/100;
Note the rounding correction for the integer division process. This is probably the best result achievable with integer arithmetic on 16-bit values. The rounding correction might also be applied to the calculation of A_f and B_f (assuming the formulae do not already do this).
Regards,
Mac
How is freq_khz defined? is it an 8-bit or 16-bit value?
What do you get as an incorrect result, and for what value of freq_khz?
Can you post the generated assembly code?