So I found another example, so now I'm trying to generate a frequency on pin 9, and then measure it on pin 3 because, the alternator doesn't seem to be getting the correct value, because it's not adjusting it's output.
Is it possile to generate on one pin and measure on another? The frequecy being reported on the serial doesn't match 179, but I'm guessing that has something to do with the fact that this code changes the timer to adjust the frequency? Would that be true? If so how would I adjust to confirm that it is in fact generating 179hz.
I'm just trying to confirm if this issue is in my code or in the alternator at this point.
THANKS!
#define DEFAULT_PWM 179
int16_t currentPWM = DEFAULT_PWM;
byte PWM_PIN = 3;
int pwm_value;
// Function Prototypes
void setPwmFrequency(int pin, int divisor);
void setup() {
pinMode(PWM_PIN, INPUT);
pinMode(DR44_PWN_PIN, OUTPUT);
setPwmFrequency(DR44_PWN_PIN, 256);
analogWrite(DR44_PWN_PIN, currentPWM ); // Start off with 60% duty cycle
Serial.begin(9600);
delay(3000); //wait for engine to start
}
void loop()
{
analogWrite(DR44_PWN_PIN, currentPWM );
delay(100);
pwm_value = readMagmeter();
Serial.println(pwm_value);
}
float readMagmeter (){
//let's try reading just one pulse :-P
int durationLow = (float)pulseIn(PWM_PIN, LOW); //returns period in MICROseconds between low and high pulses, i.e. half of square wave
float durationMillis = durationLow/1000.000; //converts to milliseconds
float freQuency = 1000.000/(durationMillis *2.000); //frequency in Hz
return freQuency;
} //end magMeter
/**
* Divides a given PWM pin frequency by a divisor.
*
* The resulting frequency is equal to the base frequency divided by
* the given divisor:
* - Base frequencies:
* o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
* o The base frequency for pins 5 and 6 is 62500 Hz.
* - Divisors:
* o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,
* 256, and 1024.
* o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,
* 128, 256, and 1024.
*
* PWM frequencies are tied together in pairs of pins. If one in a
* pair is changed, the other is also changed to match:
* - Pins 5 and 6 are paired on timer0
* - Pins 9 and 10 are paired on timer1
* - Pins 3 and 11 are paired on timer2
*
* Note that this function will have side effects on anything else
* that uses timers:
* - Changes on pins 3, 5, 6, or 11 may cause the delay() and
* millis() functions to stop working. Other timing-related
* functions may also be affected.
* - Changes on pins 9 or 10 will cause the Servo library to function
* incorrectly.
*
* Thanks to macegr of the Arduino forums for his documentation of the
* PWM frequency divisors. His post can be viewed at:
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/0#4
Pins 5 and 6: controlled by Timer 0 in fast PWM mode (cycle length = 256)
Setting Divisor Frequency
0x01 1 62500
0x02 8 7812.5
0x03 64 976.5625 <--DEFAULT
0x04 256 244.140625
0x05 1024 61.03515625
TCCR0B = (TCCR0B & 0b11111000) | <setting>;
Pins 9 and 10: controlled by timer 1 in phase-correct PWM mode (cycle length = 510)
Setting Divisor Frequency
0x01 1 31372.55
0x02 8 3921.16
0x03 64 490.20 <--DEFAULT
0x04 256 122.55
0x05 1024 30.64
TCCR1B = (TCCR1B & 0b11111000) | <setting>;
Pins 11 and 3: controlled by timer 2 in phase-correct PWM mode (cycle length = 510)
Setting Divisor Frequency
0x01 1 31372.55
0x02 8 3921.16
0x03 32 980.39
0x04 64 490.20 <--DEFAULT
0x05 128 245.10
0x06 256 122.55
0x07 1024 30.64
TCCR2B = (TCCR2B & 0b11111000) | <setting>;
All frequencies are in Hz and assume a 16000000 Hz system clock.
*/
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
} else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
} else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x7; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}
Outputing: 206 207 206 207