I just started using Timer1 to generate clock signals, 1KHz, using the output compare register and OCR1A driven pin for the output. I realized that you have to set the pin to OUTPUT first or it would not output anything. Code snippet:
digitalWrite(8, LOW);
pinMode(8, OUTPUT);
TCCR1B = _BV(WGM12)|_BV(CS10); // // CTC mode - 1 prescale
TCCR1A = _BV(COM1A0); // or use TCCR1A = 0x40; // Toggle mode 0C1A
OCR1A = 16000; // set the counter 8000 for 1KHz square wave or 16000 for 500Hz
I'm using 1284P with Bobuino definition so the output pin maps to digital 8.
Is there any reason why I have to set to OUTPUT? Maybe there are registers I didn't set but pinMode() did it for me?
I am using this clock signal to synchronize a few modules, including the generator of the module itself. So the module needs to read in the clock and act when it senses a transition. So I just used digitalRead(8). Lo and behold, it works! I thought that when Timer1 takes over the output on the pin, the GPIO is disconnected. I guess not. According to the diagram in the spec sheet, the digital (and analog) input is always connected to the pin. Am I correct?
Thanks.