Pages: [1]   Go Down
Author Topic: Timer interrupt question  (Read 528 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to figure out how to get a fairly simple timer interrupt set up, but the interrupt function gets called twice, and thats all..

I've been tinkering with this for a couple hours and Im stuck now so any help would be greatly appreciated!

Code:
/* Learn how to use interrupts on the ATMEGA32
Micro controller
*/

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


unsigned int counterT2 = 0; //Timer2 Counter
//int toggle = 0; //a Low/High toggle value


void setup(){
     Serial.begin(9600);       // use the serial port
     SetupInterruptTimer();
    
     Serial.println("setup?\n");

}

void loop(){
 // Serial.println("wtf");
 while(1);
}

void SetupInterruptTimer(){
  //Normal port operation. Prescaler = FCPU/256 ~= 244 interupts/seconf
   TCCR2A=(1<<CS22)|(1<<CS21);
  
  
 //Enable Overflow Interrupt Enable
   TIMSK2|=(1<<TOIE2);

   TCNT2=0;
  
  //Enable Global Interrupts
   sei();
}
/* Interrupt service routine for timer 2*/
ISR(TIMER2_OVF_vect){
  Serial.println("TIMER!!!!Q?\n");
  if (counterT2 >= 244){
    counterT2 =0;
    Serial.println("holy**** a second!");
  }
  else{
    counterT2++;
  }
}
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Rather then having to learn low level controls for simple timer applications, I have just used the MsTimer2 library in my projects that required timed interrupts. It is simple to understand and use.

http://www.arduino.cc/playground/Main/MsTimer2

Lefty
Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 7
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to figure out how to get a fairly simple timer interrupt set up, but the interrupt function gets called twice, and thats all..
.
.
.
Code:
/* Learn how to use interrupts on the ATMEGA32

Your code appears to be for the ATmega328, not ATmega32 as indicated in your comment.

Anyhow, for the '328:
Code:
void SetupInterruptTimer()
{
   // Normal port operation. Prescaler = FCPU/256 ~= 244 interupts/second
   //
   //
   // davekw7x:
   // From ATmega328 data sheet:
   //
   // We want WGM21, WGM21 and WGM20 to be zeros
   // We want CS22 and CS21 to be ones, and CS20 to be zero
   // We want COM2A1 and COM2A0 and COM2B1 and COM2B0 to be zeros
   //
   // TCCR2A contains bits: COM2A1, COM2A0, COM2B1, COM2B0, -, -, WGM21, WGM20
   //
   TCCR2A = 0;
  
   // TCCR2B contains bits: FOC2A, FOC2B, -, -, WGM22, CS22, CS21, CS20
   // For this application we want to prescale by 256, so set CS22 and set CS21 to ones
   TCCR2B = (1 << CS22) | (1 << CS21);
  
   // Enable Overflow Interrupt Enable
   TIMSK2 = (1 << TOIE2);

   TCNT2 = 0;
  
   // Enable Global Interrupts
   sei();
}


Regards,

Dave
« Last Edit: February 27, 2011, 12:24:19 pm by davekw7x » Logged

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

Rather then having to learn low level controls for simple timer applications, I have just used the MsTimer2 library in my projects that required timed interrupts. It is simple to understand and use.
Lefty

I did tinker with msTimer2, but I didn't like it. Plus I want to better myself with microcontrollers in general so I want to get to the nitty gritty.

Otherwise...

Ah, you are right davekw7x.. I have a 328 chip... I'm a dolt. I started this last night and didn't have a good light so I saw the atmega32 and assumed that was what it was... I will restart from scratch. Thanks for pointing it out!!
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Ah, you are right davekw7x.. I have a 328 chip... I'm a dolt.

Your not a dolt, your a wingding.  smiley-grin

Just kidding, good luck with your efforts.

Lefty

Logged

Pages: [1]   Go Up
Jump to: