Pages: [1]   Go Down
Author Topic: help with getting CTC Timer1 clock out of chip  (Read 332 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 138
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Make a complete sketch please, that demonstrates what you are trying to do. That won't compile.
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3511
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
TCCR1B = 0;    //stop the timer
TCCR1A = 0;
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3511
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's an example. A couple of good intros to AVR timers:
http://fourwalledcubicle.com/AVRArticles.php
http://www.gammon.com.au/forum/?id=11504

Code:
//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)
{
}
« Last Edit: February 14, 2013, 11:54:50 pm by Jack Christensen » Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Full Member
***
Karma: 0
Posts: 138
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks everyone!
Logged

Pages: [1]   Go Up
Jump to: