If this question should be in StackExchange instead of here I can delete here and post there.
Using an Arduino Duemilanove (Atmega328) I am trying to generate a square wave (50% duty cycle) of frequency somewhere between 500 Hz and 200 KHz, as entered by a user. I do not need any variable PWM duty cycle, just a simple square wave. Also I can not use Timer0 since modification of that affects delay() and other time related functions of the Arduino.
The timer calculations for prescaler and top counter have me very confused, can someone point me to an algorithm to identify which combination of prescaler and top values will give the closest output frequency to the specified one? In many cases, two or more combinations of prescaler and top give similar output frequency results but neither is exact.
For example user enters value of 98350 Hz. How would my Arduino code find out what prescaler and top counter value to set the Timer 2 registers to, for the closest match to that frequency?