Well, a few years back, when I was a beginner in robotics, I was stubborn to use Timer 2 for the 38kHz frequency, because Timer 1 would controll the motors. Using Timer 1 is fine, because it's a 16 bit timer, but Timer 2 is a 8 bit timer, so for the 38kHz frequency, I needed a 9.728 MHz cristal (PWM frequency is cristal/256). But here's a thought. You need to turn on the PWM, wait like 10 ms, read the sensor and so on. While waiting, the robot wasn't doing anything anyway. So, why bother using the timer when I can generate the signal in software during the wait time? Now I'm using the software method and it works perfectly.
Here is the code in Arduino:
int Obstacle;
int DoProxy(){
int LeftVal=0;
int RightVal=0;
Obstacle = 0;
IR38Write();
LeftVal = digitalRead(LeftProxyPin);
IR38Write();
RightVal = digitalRead(RightProxyPin);
if (LeftVal == LOW){
Obstacle = Obstacle + 1;
}
if (RightVal == LOW){
Obstacle = Obstacle + 2;
}
Serial.println ("LeftVal RightVal"); // debug - remember to comment out
Serial.print (LeftVal, DEC); // debug - remember to comment out
Serial.print (" "); // debug - remember to comment out
Serial.println (RightVal, DEC); // debug - remember to comment out
Serial.println (Obstacle, DEC); // debug - remember to comment out
return Obstacle;
}
void IR38Write() { //this function will generate 38.461 kHz frequency, 50% duty cycle, for 10 ms
for(int i = 0; i <= 384; i++) { //repeat 384 times 26 us = 10.010 ms for a IR burst
digitalWrite(IRpin, HIGH); //turn IR LED pin High
delayMicroseconds(12); //for 12 us
digitalWrite(IRpin, LOW); //turn IR LED pin Low
delayMicroseconds(12); //for 12 us
}
}
Now you will need to translate the Arduino functions into regular C statements, but I think the ideea is clear enough.