From 7f67ac3b16374822776eb37b926c79a7471ea605 Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Sun, 23 Aug 2015 18:04:53 +1000 Subject: [PATCH] Fix for pwLimit overflow --- math.h | 6 +++--- speeduino.ino | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/math.h b/math.h index 15dc6321..8a8be524 100644 --- a/math.h +++ b/math.h @@ -61,8 +61,8 @@ int divs100(int n) { } //Unsigned divide by 100 -unsigned int divu100(unsigned n) { - unsigned q, r; +unsigned long divu100(unsigned long n) { + unsigned long q, r; q = (n >> 1) + (n >> 3) + (n >> 6) - (n >> 10) + (n >> 12) + (n >> 13) - (n >> 16); q = q + (q >> 20); @@ -74,7 +74,7 @@ unsigned int divu100(unsigned n) { //Return x percent of y //This is a relatively fast approximation of a percentage value. -unsigned int percentage(byte x, unsigned int y) +unsigned long percentage(byte x, unsigned long y) { return divu100(y) * x; } diff --git a/speeduino.ino b/speeduino.ino index 134398b6..1f6af229 100644 --- a/speeduino.ino +++ b/speeduino.ino @@ -564,7 +564,7 @@ void loop() //Check that the duty cycle of the chosen pulsewidth isn't too high. This is disabled at cranking if( !BIT_CHECK(currentStatus.engine, BIT_ENGINE_CRANK) ) { - unsigned int pwLimit = percentage(configPage1.dutyLim, revolutionTime); //The pulsewidth limit is determined to be the duty cycle limit (Eg 85%) by the total time it takes to perform 1 revolution + unsigned long pwLimit = percentage(configPage1.dutyLim, revolutionTime); //The pulsewidth limit is determined to be the duty cycle limit (Eg 85%) by the total time it takes to perform 1 revolution if (currentStatus.PW > pwLimit) { currentStatus.PW = pwLimit; } } @@ -777,7 +777,8 @@ void loop() //Check for hard cut rev limit (If we're above the hardcut limit, we simply don't set a spark schedule) if(ignitionOn && (currentStatus.RPM < ((unsigned int)(configPage2.HardRevLim) * 100) )) { - if ( (ignition1StartAngle > crankAngle) ) + //if ( (ignition1StartAngle > crankAngle) && ign1LastRev != startRevolutions) + if ( (ignition1StartAngle > crankAngle) && ign1LastRev != startRevolutions) { setIgnitionSchedule1(beginCoil1Charge, ((unsigned long)(ignition1StartAngle - crankAngle) * (unsigned long)timePerDegree),