Go Down

Topic: timers and carrier signals (Read 1 time) previous topic - next topic

KidCrazy

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: [Select]

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

madworm

#1
Feb 22, 2009, 07:31 am Last Edit: Feb 22, 2009, 07:34 am by madworm Reason: 1
The way you set the timer registers seems inconsistent to me.

For example:
Code: [Select]

TCCR1A &= ~((1<<COM1A1) | (1<<COM1A0) | (1<<WGM11) | (1<<WGM10));


sets 4 bits of TCCR1A to zero, whereas

Code: [Select]
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);
• 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!

KidCrazy

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

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

Code: [Select]

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

Go Up