All you need to do is setup a timer as a...timer.
In this example I used an ATMega32 running the internal 8MHz RC oscillator.
An ATmega32 has a timer 2, you can use any timer you like.
The square wave was generated at the OC2 pin.
Here is the code:
'Timer 2 is the square wave oscillator
Config Timer2 = Timer , Prescale = 64 , Compare = Toggle , Clear Timer = 1
Now to generate a tone you simply load the compare register with a byte, for example:
compare2 = 88 'approximately 700Hz
compare2 = 61 'approximately 1000Hz
compare2 = 40 'approximately 1500Hz
So how does a value of 61 generate around 1000Hz? Well....
The 8MHz CPU clock is divided by 64 by the prescaler, which gives a base frequency of 125kHz. The timer starts counting from 0, we have configured it to count up to the number in the compare register, toggle the pin and then start counting again from 0. In this example a value of 61 will cause the pin to toggle around two thousand times per second(1 toggle every 125kHz/61), which is a square wave of around 1kHz.
↧