generating specific frequencies using pwm

I'm looking to generate a specific frequency in this case 40khz using just a pwm pin on an arduino instead of adding extra components to generate a pulse such as 555 timers or extra crystals. As far as I know pic's can do this so it shouldn't be a problem for arduino. Does anyone know how to do this?

There is a Tone function, though may be not documented well, I'm not sure about upper freq. limit http://arduino.cc/en/Reference/Tone

Yes you can do this by altering the value in the timer 2 registers. I assume it is for an IR emitter. I haven't got access to my code at the moment but I have posted it here several times. Search for PWM frequency.