I'm trying to output a square wave on an Arduino pin using a hardware timer. I'm using eight bit timer (timer 0) but I can't seem to get it to work like in the datasheet. I want the highest clock rate possible from the chip.
Here's my timer setup code:
// Timer 0 -> Output on OC0A
TCCR0A = _BV(WGM01) // CTC mode
|_BV(COM0A0); // Toggle OC0A output on every match
OCR0A = 4; // Reset when it reaches this value
TCCR0B = _BV(CS00); // Start timer, no prescale
I think that should toggle the ouput pin OC0A every 4 clock cycles, giving a nice square wave.
What I actually get is something like a PWM wave with a very short low pulse, like this (the purple line):
If I increase the value in OCR0A it makes the low part of the pulse wider, but a value of 3 or less makes it disappear (at least on my 'scope...)
a) If I'm toggling a pin high/low based on a regular event (anything at all!), how can I get anything but a nice square wave (50% duty cycle)?
b) The datasheet says I can put zero in OCR0A when I want maximum frequency output. Why does the minimum useful value seem to be 4?