Go Down

Topic: timer interupts (Read 733 times) previous topic - next topic

tateu

i have 2 questions about timers on atmega8. i have timer 2 and timer 1 running successfully in parallel to produce 2 square waves on OC1A and OC2. i need another timer to generate a 3rd frequency. 2 ideas: use timer 0 or put the 2 first waves both on timer 1 which is 16 bits and has OCR1A and OCR1B and use timer 2 for the 3rd.

1) timer0
i use the code belor but it seems to never even go into the interupt function (LED stays on, never goes off - note that i only set the led HIGH in setup and LOW in interupt just to see if I even go there). I have seen people using sei() - is that the problem?

ISR(TIM0_OVF) {
digitalWrite(ledPin, LOW);
};

void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
TIMSK = 0x00; // all interupts OFF
TCCR0 = 0x01; // 0000 0001 = prescaler = 1
TIMSK = 0x01; // TOIE0 ON
}


2) both waves on timer 1
i use the code below but only one output works. i am assuming OCR1A is used to compare but not OCR1B. the mega8 spec sheet talks about 0CR1B but when setting the mode (WGM13:10), the only CTC mode is 4 which uses OCR1A >> how can i use both?

TIMSK = 0x00; // all interupts OFF
TCCR1A = B01010000; // toggle for A & B, CTC mode
TCCR1B = B00001001; // prescaler = 1
OCR1A = 9661; //
OCR1B = 9999; //
TIMSK = B00011000; // 0CIE1A/B ON

thanks a bunch.

jims

Be aware, the overflow interrupts do not fire if you are resetting the timers with a compare match. There is a different interrupt vector for that. I don't think that is your problem, but you will probably bump into it.

Some functions use cli() and sei(), but they should only disable interrupts for short times.

The timer0 overflow interrupt is used inside the Arduino libraries to implement the millisecond timing functions. I wonder if that one is taking precedence over yours?

For timer1, although there are two different output compare registers, you won't get to make two different frequencies because only one will get to clear the timer. That one will set the frequency and the other will only be able to control duty cycle.

If any of your frequencies are multiples of each other you could make your fastest one be on timer2 and use the timer2 compare match interrupt to count and toggle the slower one.

tateu

hey jims, from looking at the various parts of the forum, i knew it would be you answering the question  :) thanks a  bunch.
so it looks like timer1 to handle 2 different freqs is out so i need to get timer0 to work. it might be what you are saying (used in ardu code already). if i were to disable the millis() function so that i can use timer0 myself, how would i do that?
also, got it for the interupt function not firing off when using CTC. i was aware of that, in the doc, i think those vectors are TIM1_COMPA/B, TIM2_COMP but i will not be using those at all. i will for timer0 though. when i adapted the code from gonium, i wanted to use the same notation as him for T0 (TIMER2_OVF_vect  transformed into TIMER0_OVF_vect) but the code did not compile at all any more. so i used the vector from the spec sheet (TIM0_OVF), then it compiled but does not work - i wonder if that has anything to do with it. regardless, thanks again for your feedback and i'll look into that timer0.

Go Up