avr-chat
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-chat] mega32 timer 2 confused


From: Klaus Rudolph
Subject: [avr-chat] mega32 timer 2 confused
Date: Wed, 07 Mar 2012 19:37:23 +0100
User-agent: Thunderbird 2.0.0.23 (X11/20090817)

Hi all,

i run into a very simple problem:

I want to use timer2 on atmega 32 but I see no interrupts and also no
overflow or compare flags. That makes me a bit crazy :-)

In the following source each irq will set PORTB:1 to high but I couldn't
see any interrupt and also the flags (OVF2 TOV2) are not set.

What is missing?

Thanks!
 Klaus



#include <avr/io.h>
#include <avr/interrupt.h>

ISR( TIMER2_COMP_vect )
{
    PORTB=0x02;
}

ISR( TIMER2_OVF_vect )
{
   PORTB=0x02;
}

void SetupTimer2()
{
    ASSR &= ~(1 << AS2);      // run on system clock

    TCCR2 =
        //( 1 << CS22 ) | ( 1 << CS21 ) | ( 1 << CS20 ) | // prescaler
-> 1024 Page 127
        ( 1 << CS22 ) | ( 1 << CS21 ) | ( 0 << CS20 ) | // prescaler ->
256 Page 127
        // run in CTC Mode ( Clear Timer on compare ) -> Mode 2 WGM21 ->
1, WGM20 -> 0
        ( 1 << WGM21 ); //CTC Mode

    // compare value
    OCR2= 250;  // results in freq.: 8 000 000 : 256 : 250 -> 125 Hz

    // enable Output Compare interrupt
    TIMSK |= (1 << OCIE2 );

    // enable Overflow 2 interrupt
    TIMSK |= (1 << TOIE2);
}

int main()
{
    DDRB=0xff;
    SetupTimer2();
    sei();              // enable irq

    while (1)
    {
       uint8_t val= TCNT2;
       uint8_t old= PORTB;
       old &= ~0x01;
       old |= val & 0x01;
       PORTB = old;


       if ( TIFR & ( 1  << OCF2 ))
       {
          PORTB |= 0x02;
       }

       if ( TIFR & ( 1 << TOV2 ))
       {
          PORTB |= 0x02;
       }
    }

    while(1){};
}








reply via email to

[Prev in Thread] Current Thread [Next in Thread]