Go Down

Topic: help with getting CTC Timer1 clock out of chip (Read 747 times) previous topic - next topic


My program is working, but I want to check on a clock with an oscilloscope.  Unfortunately, I have had no luck getting it out of the chip.  The basic code for the clock setup is
Code: [Select]
  OCR1A = 100;

  // Set CS10 and CS12 bits for 64 prescaler:
  TCCR1B |= (1 << CS10);
  TCCR1B |= (1 << CS11);
  //TCCR1B  |= (1 << CS12);

  // turn on CTC mode:
  TCCR1B |= (1 << WGM12);

  // enable timer compare interrupt:
  TIMSK1 |= (1 << OCIE1A);

I have tried setting DDRD to B11111111 to no avail.  Any clues on what I should set?

I am using an Arduino Uno btw
Thank you for your time


Make a complete sketch please, that demonstrates what you are trying to do. That won't compile.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Jack Christensen

Or tell us what you're trying to do.

But I'll make a guess: Are you trying to divide the system clock frequency by 6400 and output the result on a pin so it is observable with a scope? Not sure why the interrupt would need to be enabled.

If so, try using compare match by setting TCCR1A appropriately. The resulting signal will then be output on the OC1A pin (Arduino pin 9) or IC1B (Arduino pin 10).

Don't assume register values are the initial values as documented in the datasheet, as the Arduino core does some initialization for PWM, etc. So if I want to use the timer for my own purposes I usually start with

Code: [Select]
TCCR1B = 0;    //stop the timer
TCCR1A = 0;

Jack Christensen

Feb 15, 2013, 05:52 am Last Edit: Feb 15, 2013, 05:54 am by Jack Christensen Reason: 1
Here's an example. A couple of good intros to AVR timers:

Code: [Select]
//Output a 1250Hz signal on Arduino pin 9
//Toggles the pin based on dividing the 16MHz system clock
//frequency by 6400. Toggling the pin effectively adds another
//factor of two, so the resultant frequency is 16MHz / 100 / 64 / 2 = 1250Hz

void setup(void)
   pinMode(9, OUTPUT);
   TCCR1B = 0;                                     //stop the timer
   TCCR1A = _BV(COM1A0);                           //toggle OC1A on compare match
   OCR1A = 99;                                     //divide by 100 (timer counts from 0 to 99)
   TCCR1B = _BV(WGM12) | _BV(CS11) | _BV(CS10);    //start timer, CTC mode, prescale factor 64

void loop(void)

Go Up