From ca67510e704853fbec5874095b6ae1da1764a015 Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Wed, 4 Feb 2015 11:52:01 +1100 Subject: [PATCH] Minor cleanup of pulsewidth calc. --- math.h | 17 +++++++++++++++-- speeduino.ino | 2 +- utils.h | 7 ++++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/math.h b/math.h index d0591c2..fe25096 100644 --- a/math.h +++ b/math.h @@ -11,8 +11,8 @@ Ref: http://www.hackersdelight.org/divcMore.pdf */ //Unsigned divide by 10 -unsigned divu10(unsigned n) { - unsigned q, r; +unsigned int divu10(unsigned int n) { + unsigned int q, r; q = (n >> 1) + (n >> 2); q = q + (q >> 4); q = q + (q >> 8); @@ -36,3 +36,16 @@ int divs10(int n) { return q + ((r + 6) >> 4); // return q + (r > 9); } + +//Signed divide by 100 +int divs100(int n) { + int q, r; + n = n + (n>>31 & 99); + q = (n >> 1) + (n >> 3) + (n >> 6) - (n >> 10) + + (n >> 12) + (n >> 13) - (n >> 16); + q = q + (q >> 20); + q = q >> 6; + r = n - q*100; + return q + ((r + 28) >> 7); +// return q + (r > 99); +} diff --git a/speeduino.ino b/speeduino.ino index 6940cb3..e84eec2 100644 --- a/speeduino.ino +++ b/speeduino.ino @@ -359,7 +359,7 @@ void loop() //BEGIN INJECTION TIMING //Determine next firing angles //1 - int PWdivTimerPerDegree = div(currentStatus.PW, timePerDegree).quot; //This variable is used multiple times, so only do the division once. + int PWdivTimerPerDegree = div(currentStatus.PW, timePerDegree).quot; //How many crank degrees the calculated PW will take at the current speed injector1StartAngle = 355 - ( PWdivTimerPerDegree ); //This is a little primitive, but is based on the idea that all fuel needs to be delivered before the inlet valve opens. I am using 355 as the point at which the injector MUST be closed by. See http://www.extraefi.co.uk/sequential_fuel.html for more detail //Repeat the above for each cylinder //2 diff --git a/utils.h b/utils.h index fded4e8..dc60ba9 100644 --- a/utils.h +++ b/utils.h @@ -50,9 +50,10 @@ unsigned int PW(int REQ_FUEL, byte VE, byte MAP, int corrections, int injOpen, b intermediate = (intermediate * iMAP) >> 7; intermediate = (intermediate * iCorrections) >> 7; intermediate = (intermediate * iTPS) >> 7; - unsigned int final = (unsigned int)(intermediate + injOpen); - //if (final > 65535) { final = 65535; } //Make sure this won't overflow. This means the maximum pulsewidth possible is 65.535mS - return final; + intermediate += injOpen; //Add the injector opening time + if ( intermediate > 65535) { intermediate = 65535; } //Make sure this won't overflow when we convert to uInt. This means the maximum pulsewidth possible is 65.535mS + //unsigned int final = (unsigned int)(intermediate); + return (unsigned int)(intermediate); }