Timer1 CTC mode output problem with Mega2560

Hi All,

I am trying to generate a 1KHz square wave output via timer1 in CTC Mode but no matter what I try I am not generating any signal on Pin11 which according to the schematic is OC1A pin.

My code is as follows:

#include <avr/io.h> 
#include <avr/interrupt.h> 

void setup() 
{
	pinMode(11, OUTPUT);

	TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode 
	TCCR1A |= (1 << COM1A0); // Enable timer 1 Compare Output channel A in toggle mode 
	OCR1A   = 250; // Set CTC compare value to 1KHz at 16MHz AVR clock, with a prescaler of 64 
	TCCR1B |= ((1 << CS10) | (1 << CS11)); // Start timer at Fcpu/64 
}

void loop() 
{
}

Any suggestions?

Thanks
Chris

Hi,

TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode

There are seven more bits in the TCCR1B register. What are the values of those other bits?

Thanks Coding Badly,

I forgot that the Arduino had alraedy set up the timer for PWM.
adding the two lines:

        TCCR1B = 0;
        TCCR1A = 0;

To get the Timer into a known state works a charm :slight_smile:

Cheers
Chris

I also had an error in my Math so for the sake of anyone reading this thread if the future I include the corrected code to generate 1KHz signal on pin 11 of the Mega2560:

#include <avr/io.h> 
#include <avr/interrupt.h> 

void setup() 
{
	pinMode(11, OUTPUT);
	
	TCCR1B = (1 << WGM12); // Configure timer 1 for CTC mode 
	TCCR1A = (1 << COM1A0); // Enable timer 1 Compare Output channel A in toggle mode 
	OCR1A   = 124; // Set CTC compare value to 1KHz at 16MHz AVR clock, with a prescaler of 64 
	TCCR1B |= ((1 << CS10) | (1 << CS11)); // Start timer at Fcpu/64 

}

void loop() 
{
}

Cheers
Chris

Thank you for the follow-up. Glad to know you have it working.

Many thanks!!

OCR1A = 0 124,9kHz
OCR1A = 65535 > 1,907Hz Tested!!

I whant to work with an Stepper Motor Driver. But this is working only till 1000Hz. But inside the data sheeds means till 200KHz. Do we need an other Profil for such an Stepper Motor Driver??

Mike