I am trying to use Timer1 of Atmel AVR microcontroller, either AtMega328 as used in the Arduino, or the ATTiny85, to output two clock signals which are mirror images of each other. The frequency I am trying to generate is a variable 1 MHz to 2 MHz or more which are too high to do this using code to toggle the output pins unless I want to do almost nothing else in the controller. So I want to use the timer output directly on the associated pins. I am using GCC toolchain so not limited by arduino libraries or language.
Timer1 in the Atmega328 has two pins associated with it and I can get two identical 1MHz to 2MHz signals out of them. Though the datasheet seems to say I can get an inverted waveform, it is confusing me. I am also able to get two signals which are different duty cycles at 1 MHz, using the PWM settings with Timer1, but both signals go high at the same time, the shorter one goes low earlier. This does not serve my project. I do not even need the PWM pulse width variation, I just need two identical "clock" type signals of opposite phase, that's all.
I am not asking for anyone to write code for me to do this, I just need someone to tell me which mode / flags of the timer should give me a simple inverted waveform on one of the two pins associated with the timer. If possible I want to avoid using an external inverting circuit for one of the outputs unless that is only option.
If this is possible at all in the ATTiny, that will be even better. The ATTiny also has 2 pins associated with one timer, but I am not sure it has the same options as the ATMega.
I already have a 20 MHz crystal and capacitors connected on the PCB and 20 MHz clock is working reliably on the ATMega328. On ATTiny85 PCB I have a 8 MHz crystal and that is also working reliably.
Please help. Thank you.
UPDATE: There are some invalid assumptions in the answers and comments so far so maybe I should clarify: Note that in my original post I have stated that I am using a 20 MHz clock, not 8 MHz, and also that I do not need PWM.
The only mode that gives a high enough output frequency seems to be CTC mode because PWM modes are not working for 2 MHz output. Is there a way to invert either Timer 1 output A, or output B, in CTC mode?
I have now switched to a standard Arduino Uno (ATMega328, 16 MHz) instead of my own 20 MHz board to check my code, and this is my code for a nice steady 2 MHz clock in CTC mode from pins 9 and 10, the Timer 1 output pins:
#define tick 9
#define tock 10
void setup() {
pinMode(tick, OUTPUT);
pinMode(tock, OUTPUT);
TCCR1A = _BV(COM1A0) | _BV(COM1B0) ; // activate both output pins
TCCR1B = _BV(WGM12)| 1; // set CTC mode, prescaler mode 1
// various frustrating attempts to invert OC1B failed. What do I put here?
OCR1A = 3; // set the counter max for 2 MHz
}
void loop() {
}
The oscilloscope traces for both pins are identical and in sync, how can I get either of the two signals inverted? The invert mode in the datasheet appears to do nothing in CTC mode. Am I reading the datasheet wrong, or will I be forced to use a lower frequency and PWM mode after all?
To add a specific "bounty" question to my original query:
So what changes do I need to make to my code above, to make it give perfectly inverted signals at pin 9 and 11 at the highest possible frequency for a 16 MHz clock, whether that is 2 MHz or not?
I will stick with a standard Arduino Uno for now, so that there is no error mode being introduced by my homespun board, and so that anyone with an arduino can try my code above and confirm that it works as I have mentioned and not as I need!