Go Down

Topic: Timer interrupt question (Read 550 times) previous topic - next topic

wingding

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

/* 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++;
 }
}

retrolefty

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

davekw7x

#2
Feb 27, 2011, 05:50 pm Last Edit: Feb 27, 2011, 06:24 pm by davekw7x Reason: 1

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

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

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

wingding


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!!

retrolefty

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


Your not a dolt, your a wingding.  :D

Just kidding, good luck with your efforts.

Lefty


Go Up