actually i wanted to generate 40kHz square wave to be given...
If I thought I could explain it better than the awesome Mr. Sharriff, I might try. Alas, I don't think I can accomplish that. Maybe someone else here feels like giving it a shot.
If you are interested in learning, you can check his code, as I suggested. In particular:
void IRsend::enableIROut(int khz) {
// Enables IR output. The khz value controls the modulation frequency in kilohertz.
// The IR output will be on pin 3 (OC2B).
// This routine is designed for 36-40KHz; if you use it for other values, it's up to you
// to make sure it gives reasonable results. (Watch out for overflow / underflow / rounding.)
// TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B
// controlling the duty cycle.
// There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A)
// To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin.
// A few hours staring at the ATmega documentation and this will all make sense.
// See my Secrets of Arduino PWM at http://arcfn.com/2009/07/secrets-of-arduino-pwm.html for details.
.
.
.
The "IR output" to which he is referring is, simply, a square wave on Arduino pin 3. Use it for whatever you want to. He also shows how to turn the output signal on and off with a single instruction (without starting and stopping the timer each time), so it can be useful for lots of stuff. Maybe even your application.
If you have
specific questions, I'm betting that someone here is willing and able to try to help...
Regards,
Dave