Pages: [1]   Go Down
Author Topic: timers and carrier signals  (Read 1232 times)
0 Members and 1 Guest are viewing this topic.
Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there,

I'm currently doing tests on creating carrier signals with timers. For this, i initialy want to create a rectangular signal which changes state of OC1A pin (digital pin 9 on arduino) once every second. I read the chapter timers in datasheet of atmega168 but it's a little bit... confusing to read, so i'm not sure where my error is.

I'm working on a arduino duemilanove, 16 MHz with atmega168.

Code so far:
Code:
void setup(){
  // reset settings of Timer/Counter register 1
  TCCR1A &= ~((1<<COM1A1) | (1<<COM1A0) | (1<<WGM11) | (1<<WGM10));
  TCCR1B &= ~((0<<WGM13) | (1<<WGM12) | (1<<CS12) | (0<<CS11) | (1<<CS10));

  // set compare match output A to toogle
  TCCR1A |= (0<<COM1A1) | (1<<COM1A0);
  // set waveform generation mode to CTC
  TCCR1A |= (0<<WGM11) | (0<<WGM10);
  TCCR1B |= (0<<WGM13) | (1<<WGM12);
  // set clock select to clock/1024 (from prescaler)
  TCCR1B |= (1<<CS12) | (0<<CS11) | (1<<CS10);
  // set output compare register A to 15624
  OCR1A = 15624;
}
  
void loop(){
}

What should happen is that Timer/Counter 1 (TCNT1) should be increased every clock/1024 ticks. TCNT should be cleared when it reaches 15624 (= OCR1A, CTC mode) and the output pin OC1A should be toggled. So it should be toggled 16.000.000/1024/15624=1 times per second if i calculated right.

However, OC1A seems never to change to HIGH. Where is my error in thinking? And is there some real good tutorial on timers somewhere?

Thanks in advance for answers,
  Kc
Logged

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The way you set the timer registers seems inconsistent to me.

For example:
Code:
TCCR1A &= ~((1<<COM1A1) | (1<<COM1A0) | (1<<WGM11) | (1<<WGM10));

sets 4 bits of TCCR1A to zero, whereas

Code:
TCCR1B &= ~((0<<WGM13) | (1<<WGM12) | (1<<CS12) | (0<<CS11) | (1<<CS10));

sets only 3 bits to zero (WGM12,CS12,CS10) and the bits WGM13,CS11 are left UNCHANGED!

Clearing bits:

TCCR1A &= ~(1<<WGM13);

Setting bits:

TCCR1A |= (1<<WGM13);
« Last Edit: February 22, 2009, 01:34:09 am by madworm » Logged

• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ay, you're completely right. typical case of copy&paste error, i forgot to exchange the zeros by ones to reset the bits i wanted. seems it was a little bit too late last night smiley-razz

i also forgot to set pin 9 as output pin. new code:

Code:
void setup(){
  // set pin 9 as output
  pinMode(9,OUTPUT);
  
  // reset settings of Timer/Counter register 1
  TCCR1A &= ~((1<<COM1A1) | (1<<COM1A0) | (1<<WGM11) | (1<<WGM10));
  TCCR1B &= ~((1<<WGM13) | (1<<WGM12) | (1<<CS12) | (1<<CS11) | (1<<CS10));

  // set compare match output A to toogle
  TCCR1A |= (0<<COM1A1) | (1<<COM1A0);
  // set waveform generation mode to CTC
  TCCR1A |= (0<<WGM11) | (0<<WGM10);
  TCCR1B |= (0<<WGM13) | (1<<WGM12);
  // set clock select to clock/1024 (from prescaler)
  TCCR1B |= (1<<CS12) | (0<<CS11) | (1<<CS10);
  // set output compare register A to 15624
  OCR1A = 15624;
}
  
void loop(){
}

now it's working. thanks smiley
Logged

Pages: [1]   Go Up
Jump to: