Hi guys i am having a bit of trouble changing the pwm frequencies on the pins on my uno.
I have succesfully changed the pwm frequency to 167Hz on pin D9 and 32kHz on D3. When i try to change D11 to 32kHz instead of D3 it does not work(verified by oscilloscope).
Also to verify which pins are on which timer this is the info i found:
Timer 0: D5 & D6
Timer 1: D9 & D10
Timer 2: D3 & D11
I do not wish to change timer 0 as it controls the timing of the chip (millis, etc) correct?
I have attached my current code.
#include <serLCD.h>
#include <SoftwareSerial.h>
#include <PID_v1.h>
#include <PWM.h>
//Exhaust Pressure Sensor Input
float ebp_reading = 0;
float ebp_oldaverage = 0;
float ebp_newaverage = 0;
const int ebp = A0;
//Exhaust Pressure Sensor Output
const int ebpOut = 3;
int ebpOutput = 0;
int ebpOutput_brake = 0;
//Throttle Position Sensor
float tps_reading = 0;
float tps_oldaverage = 0;
float tps_newaverage = 0;
const int tps = A2;
unsigned long tps_counter = 0;
//Vehicle Speed Sensor
unsigned long vss_current_time;
unsigned long vss_previous_time = 0;
volatile unsigned long vss_counter = 0;
unsigned long vss_freq = 0;
unsigned long previous_vss_freq = 0;
const int vssIn = 2;
//VGT Duty Cycle in measurement
const int pwmIn = 4;
float totaltime = 6000.0;
float dutycycleIn;
float dutycycleOut;
float dutycycleOutD;
float dutycycle_reading = 0;
unsigned long durationL;
float durationL_oldaverage = 0;
float durationL_newaverage = 0;
//VGT Output pin
const int pwmOut = 9;
int vgtOut = 0;
//Exhaust Brake Status, 0=OFF, 1=ON
int brake_status = 0;
// Exhaust Brake On/Off Toggle Switch
const int eb_switch = 7;
int eb_switch_value = 0;
//PID Information
double Setpoint, Input, pidOutput;
PID myPID(&Input, &pidOutput, &Setpoint,1,2,0, DIRECT);
// Initialize LCD
const int RXpin = 8;
serLCD lcd(RXpin);
// LCD timer
unsigned long current_time1;
unsigned long previous_time1 = 0;
void setup() {
pinMode(pwmIn, INPUT);
pinMode(tps, INPUT);
pinMode(ebp, INPUT);
pinMode(vssIn, INPUT);
digitalWrite(vssIn, HIGH);
pinMode(eb_switch, INPUT);
pinMode(RXpin, OUTPUT);
//vehicle speed sensor
attachInterrupt(0, vspeed, FALLING);
//initialize all timers except for 0, to save time keeping functions
InitTimersSafe();
//sets the frequency for pwmOut pin 9
bool success1 = SetPinFrequencySafe(pwmOut, 167);
//if the pin frequency was set successfully, turn pin 9 on
if(success1) {
pinMode(pwmOut, OUTPUT);
}
//sets the frequency for ebpOut pin 11
bool success2 = SetPinFrequencySafe(ebpOut, 32000);
//if the pin frequency was set successfully, turn pin 3 on
if(success2) {
pinMode(ebpOut, OUTPUT);
}
//initialize PID variables
Input = ebp_newaverage;
Setpoint = 431; //test value = 30 psig
myPID.SetOutputLimits(38, 128);
//turn the PID on
myPID.SetMode(AUTOMATIC);
// lcd setup
lcd.setBrightness(20);
lcd.clear();
lcd.print("VGT%:");
lcd.setCursor(2,1);
lcd.print("EP:");
} //End of Setup
// Vehicle Speed Sensor Interrupt
void vspeed(){
vss_counter++;
}
void loop(){
//Read Duty Cycle In Measurement
dutycycle_reading = pulseIn(pwmIn, LOW);
durationL_newaverage = ((dutycycle_reading * 0.4) + ((1 - 0.4) * (durationL_oldaverage)));
durationL_oldaverage = durationL_newaverage;
dutycycleIn = ((durationL_oldaverage / 6000) * 100);
dutycycleOut = map(dutycycleIn, 0, 100, 0, 255);
//Read Exhaust Pressure Sensor
ebp_reading = analogRead(ebp);
ebp_newaverage = ((ebp_reading * 0.1) + ((1 - 0.1) * (ebp_oldaverage)));
ebp_oldaverage = ebp_newaverage;
//Read Throttle Position Sensor
tps_reading = analogRead(tps);
tps_newaverage = ((tps_reading * 0.1) + ((1 - 0.1) * (tps_oldaverage)));
tps_oldaverage = tps_newaverage;
//TPS Counter
if(tps_oldaverage <= 70){
tps_counter++;
}
else{
tps_counter = 0;
}
//Vehicle Speed Sensor
vss_current_time = millis();
if(vss_current_time - vss_previous_time >=1000){
vss_freq = vss_counter;
vss_previous_time = vss_current_time;
vss_counter = 0;
}
//Read Exhaust Brake On/Off Switch
eb_switch_value = digitalRead(eb_switch);
//Output
if(tps_counter >= 150 && vss_freq >= 30 && eb_switch_value == HIGH){
Input = ebp_oldaverage;
myPID.Compute();
pwmWrite(pwmOut, pidOutput);
vgtOut = pidOutput;
//Send Fake Exhaust Pressure Signal To FORD PCM
ebpOutput_brake = ebp_oldaverage * 0.7;
ebpOutput = map(ebpOutput_brake, 0, 1023, 0, 255);
pwmWrite(ebpOut, ebpOutput);
brake_status = 1; //EXHAUST BRAKE ON
}
else{
pwmWrite(pwmOut, dutycycleOut);
vgtOut = dutycycleOut;
//Send Real Exhaust Pressure Sensor Reading To FORD PCM
ebpOutput = map(ebp_oldaverage, 0, 1023, 0, 255);
pwmWrite(ebpOut, ebpOutput);
brake_status = 0; //EXHAUST BRAKE OFF
}
current_time1 = millis();
// LCD Display
if(current_time1 - previous_time1 > 800){
previous_time1 = current_time1;
//print vgt output
lcd.setCursor(1, 6);
lcd.print(" ");
lcd.setCursor(1, 6);
lcd.print(vgtOut * 0.3921, 0); // MULTIPLY BY 1.8 TO GET FROM 0 TO 100 FOR DISPLAY PURPOSE ONLY AFTER TESTING IS COMPLETE
//Print Exhaust Pressure Sensor Measurement
lcd.setCursor(2, 4);
lcd.print(" ");
lcd.setCursor(2, 4);
lcd.print(((ebp_oldaverage * 0.00488) *19) - 11, 0);
lcd.setCursor(2, 7);
lcd.print(" ");
lcd.setCursor(2, 7);
lcd.print(((ebpOutput * 0.0196) *19) - 11, 0);
lcd.setCursor(2, 10);
lcd.print(" ");
lcd.setCursor(2, 10);
lcd.print(tps_oldaverage, 0);
lcd.setCursor(1, 10);
lcd.print(" ");
lcd.setCursor(1, 10);
lcd.print(vss_freq);
//Print if Switch Is On or Off
lcd.setCursor(1, 15);
lcd.print(" ");
lcd.setCursor(1, 15);
if(eb_switch_value == HIGH){
lcd.print("EN");
}
else{
lcd.print("DD");
}
// Print Exhaust Brake Status
lcd.setCursor(2, 14);
lcd.print(" ");
lcd.setCursor(2, 14);
if(brake_status == 1){
lcd.print("ON");
}
else{
lcd.print("OFF");
}
}
} //End of Loop
Thanks so much guys!