I'm trying to use an arduino duemilanove to ping an ultrasonic transmitter at 40kHz. I've searched through much of the information available on the boards and on the site about outputting various frequencies but I'm still a bit confused.
I've found that with the tone() function I can get up to around 30kHz It appears that there are methods to increase the frequency of PWM pins to get a desired frequency beyond the default. For 40kHz, do I need to change timer0 or can I achieve it by changing timer1/timer2?
Right now I'm trying to use the code:
#define F_CTC 40000
#define PRESCALER 1
#define T2DIV (F_CPU/PRESCALER/(F_CTC*2))
void setup()
{
OCR2A = T2DIV; // counter compare value
TCCR2B = _BV(CS20); // prescaler = 1 (F_CPU MHz)
TCCR2A = _BV(COM2A0) | _BV(WGM21); // toggle OC2A, CTC mode
Serial.begin(9600);
}
But it appears that I've either messed that code up or I'm using the wrong code to toggle pin 11 to a steady 40kHz. Should the above code give me the 40kHz I'm looking for? If so, should I be using digitalWrite, analogWrite, or something else?