On the Arduino Uno timer 2 can be used to change the frequency while maintaining 50% duty-cycle, by utilising fast PWM mode with the double buffered OCR2A register acting as TOP. Double buffering prevents gitches in the PWM output when changing the frequency.
Here's an example of using the Arduino Uno fast mode to perform a PWM frquency sweep on pin D11 with 50% duty-cycle:
// Frequency sweep on timer 2 using output D11 with 50% duty-cycle
void setup() {
Serial.begin(115200); // Initialise the serial port
pinMode(11, OUTPUT); // Generate a square wave test frequency on D11
TCCR2A = _BV(COM2A0) | _BV(WGM21) | _BV(WGM20);// Set timer 2 for Fast PWM mode with OCR2A as TOP
OCR2A = 0; // Set the OCR2A to 0
TCCR2B = _BV(WGM22) | _BV(CS21); // Enable timer clock without prescaler divide by 8
}
void loop() {
for (int16_t i = 0; i < 255; i++) // Decrease the frequency
{
OCR2A = i;
delay(10);
}
for (int16_t i = 255; i >= 0; i--) // Increase the frequency
{
OCR2A = i;
delay(10);
}
}