From 0b82b120c5eeb0d83d75ee4effd043bd96ff7304 Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Fri, 27 May 2016 22:44:40 +1000 Subject: [PATCH] Fix for reverse PWM IAC valve setting not working --- idle.ino | 34 ++++++++++++++++++++++++++-------- math.h | 2 +- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/idle.ino b/idle.ino index 0baf584c..887bf144 100644 --- a/idle.ino +++ b/idle.ino @@ -243,20 +243,38 @@ ISR(TIMER4_COMPC_vect) { if (idle_pwm_state) { - *idle_pin_port &= ~(idle_pin_mask); // Switch pin to low (1 pin mode) - if(configPage4.iacChannels) { *idle2_pin_port |= (idle2_pin_mask); } //If 2 idle channels are in use, flip idle2 to be the opposite of idle1 + if (configPage4.iacPWMdir == 0) + { + //Normal direction + *idle_pin_port &= ~(idle_pin_mask); // Switch pin to low (1 pin mode) + if(configPage4.iacChannels) { *idle2_pin_port |= (idle2_pin_mask); } //If 2 idle channels are in use, flip idle2 to be the opposite of idle1 + } + else + { + //Reversed direction + *idle_pin_port |= (idle_pin_mask); // Switch pin high + if(configPage4.iacChannels) { *idle2_pin_port &= ~(idle2_pin_mask); } //If 2 idle channels are in use, flip idle2 to be the opposite of idle1 + } OCR4C = TCNT4 + (idle_pwm_max_count - idle_pwm_cur_value); - if (configPage4.iacPWMdir == 0) { idle_pwm_state = false; } - else { idle_pwm_state = true; } + idle_pwm_state = false; } else { - *idle_pin_port |= (idle_pin_mask); // Switch pin high - if(configPage4.iacChannels) { *idle2_pin_port &= ~(idle2_pin_mask); } //If 2 idle channels are in use, flip idle2 to be the opposite of idle1 + if (configPage4.iacPWMdir == 0) + { + //Normal direction + *idle_pin_port |= (idle_pin_mask); // Switch pin high + if(configPage4.iacChannels) { *idle2_pin_port &= ~(idle2_pin_mask); } //If 2 idle channels are in use, flip idle2 to be the opposite of idle1 + } + else + { + //Reversed direction + *idle_pin_port &= ~(idle_pin_mask); // Switch pin to low (1 pin mode) + if(configPage4.iacChannels) { *idle2_pin_port |= (idle2_pin_mask); } //If 2 idle channels are in use, flip idle2 to be the opposite of idle1 + } OCR4C = TCNT4 + idle_pwm_target_value; idle_pwm_cur_value = idle_pwm_target_value; - if (configPage4.iacPWMdir == 0) { idle_pwm_state = true; } - else { idle_pwm_state = false; } + idle_pwm_state = true; } } diff --git a/math.h b/math.h index 8577a84b..81af51a9 100644 --- a/math.h +++ b/math.h @@ -76,7 +76,7 @@ unsigned long divu100(unsigned long n) { //This is a relatively fast approximation of a percentage value. unsigned long percentage(byte x, unsigned long y) { - return divu100(y) * x; + return divu100(y * x); } #endif // MATH_H