Using Timer1 to generate a square wave on the Mega

/*
This sketch developed and tested on a Mega with version 0018

For the purposes of square wave generation Timer1 is controlled by 3 registers of interest.
There are more registers but they are not needed for waveform generation and will not be discussed.

These are TCCR1A, TCCR1B and the 16 bit output compare registers OCR1A OCR1B and OCR1C (generically OCR1x)
of which only OCR1A is used in this example.

TCCR1A contains the COM bits which control how the output pins behave:
COM1A1 and COM1A0 control pin 11 - OC1A or PB5 - used in this example
COM1B1 and COM1B0 control pin 12 - OC1B or PB6 - not used in this example
COM1C1 and COM1C0 control pin 13 - OC1C or PB7 - Note that pin 13 is the led pin and is shared with OC0A from Timer0. Take care

For square wave generation the mode where the output toggles is required: COM1x1 = 0 and COM1x0 = 1
For toggling pin 11 use COM1A1 = 0 and COM1A0 = 1.

TCCR1A also contains Waveform Generation Mode (WGM) Bits not of interest here.

Clock control bits in register TCCR1B
CS12 CS11 CS10 meaning
0 0 0 No clock source - timer stops
0 0 1 No prescaler clkI/O/1
0 1 0 prescaler 8 - clkI/O/8
0 1 1 prescaler 64 - clkI/O/64
1 0 0 prescaler 256 - clkI/O/256
1 0 1 prescaler 1024 - clkI/O/1024

Also in TCCR1B:
Waveform Generation Mode (WGM) Bits
The only Waveform bit of interest is WGM12 which sets CTC mode

Generating the waveform is done by clocking the counter register (TCNT1) from the clock source (16MHz on a MEGA)
through the prescaler (which may be set at 1 = no prescaler) until the value in the Output Compare Register 1A (OCR1A)
is reached. (Note that there are two other OCR (OCR1B and OCR1C not used in this example. However thay
may be used independent of OCR1A to generate another freuquecy or other Timer Counter Functions.
When that value is reached the output toggles provided that the corresponding output pin is set to
output mode.

Generating a specific frequency is then a combination of the prescaler and the value in the OCR.

For 16MHz input here are the corresponding prescal frequencies
Prescaler out for each mode
1 8 64 256 1024
16,000,000 2,000,000 250,000 62,500 15,625 Hz

For a 16MHz clock here is the range of frequencies to be expected for a range of example OCR and each presacler case:
OCR OCR(Hex) Output Pin Frequency corresponding to Prescaler division
1 8 64 256 1024
1 1 8,000,000.00 1,000,000.00 125,000.00 31,250.00 7,812.50
2 2 4,000,000.00 500,000.00 62,500.00 15,625.00 3,906.25
4 4 2,000,000.00 250,000.00 31,250.00 7,812.50 1,953.13
8 8 1,000,000.00 125,000.00 15,625.00 3,906.25 976.56
16 10 500,000.00 62,500.00 7,812.50 1,953.13 488.28
32 20 250,000.00 31,250.00 3,906.25 976.56 244.14
64 40 125,000.00 15,625.00 1,953.13 488.28 122.07
128 80 62,500.00 7,812.50 976.56 244.14 61.04
256 100 31,250.00 3,906.25 488.28 122.07 30.52
512 200 15,625.00 1,953.13 244.14 61.04 15.26

1,024 400 7,812.50 976.56 122.07 30.52 7.63
2,048 800 3,906.25 488.28 61.04 15.26 3.81
4,096 1000 1,953.13 244.14 30.52 7.63 1.91
8,192 2000 976.56 122.07 15.26 3.81 0.95
16,384 4000 488.28 61.04 7.63 1.91 0.48
32,768 8000 244.14 30.52 3.81 0.95 0.24
65,535 FFFF 122.07 15.26 1.91 0.48 0.12

Choice of prescale and OCR is dependant on the frrequency (or frequencies if other OCR are used) and on the rest of the functions that are required.

However generally a prescale that gives a larger OCR results in less error in the frequency.
For example, in the table above a frequency of 244.14 Hz is avaiable at the following settings:
Prescale OCR (Hex)
1 8000
8 1000
64 200
256 80
1024 20

The formula is OCR value = Prescaler out /(2*f) where f is the desired frequency rounded to a 16 bit unsigned integer.

So if a .5Hz fquency is required with a prescale of 1024 the OCR is

OCR = 15,625/(2* 0.5) = 15625

Note that it might not be possible to generate any frequency due to limitations of OCR,
the staring frequency and the prescaler division. Some frequencies will exhibit some level of inaccuracy.

*/

/* The following sketch replicates the classic blink program that most users try as their first example but also blinks led 11 using Timer/Counter 1. The Timer/Counter led uses no processor resources except for the setup however the standard blink code oocupies the processor completely
*/

int ledPin = 13;
int blinkPin = 11;

void TimerOne_setPeriod(long OCRValue)
// It would be possible to develop a program to take the required frequency, assign an apporopriate prescale option
// and then determine the OCR frequency. This has not been done in this case as the process usually done once
// and the thought process to determine the optimum prescaler option is of value

{
TCCR1B = _BV(WGM12)|_BV(CS12)|_BV(CS10); // or use TCCR1B = 0x0d; // CTC mode - 1024 prescale
TCCR1A = _BV(COM1A0); // or use TCCR1A = 0x40; // Toggle mode 0C1A
OCR1A = OCRValue; // set the counter
}

void setup()
{
pinMode(ledPin, OUTPUT); // ledPin is output
pinMode(blinkPin, OUTPUT); // alternately use DDRB = 0xa0; // turn on the timer ouput pin;
TimerOne_setPeriod(15625); // set up and start Timer1 to blink at the same rate as the blink sketch
}

void loop()
{
digitalWrite(ledPin, LOW); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, HIGH); // set the LED off
delay(1000); // wait for a second
}

I don't understand why this is posted under 'Troubleshooting'

I Think this description is very helpfull.

Mellis recently said he knows of no complete tutorial on the timers.

Might I suggest that this could be included in such a tutorial.

Just a few points the ATMega 168/328 used in the Arduino 2009
board connects OC1A to PB1 (D9 not D11) and
OC1B to PB2 (D10 not D12).

With those changes it works fine.