Questions on interrupt

Thread Starter

Sonoma_Dog

Joined Jul 24, 2008
99
ok, I finally got my Arduino board!! yeah!

i was trying to set up a interrupt but I didnt get anywhere after hours of programming. Does any of you have a good article on how to set up a interrupt?
I am trying to execute some function whenever a timer/counter is overflowed.

also, on the Arduino board, i only see one crystal oscillator on the board, does that mean i only have one timer/counter to use?

And finally, this seems like a dumb question, but i am going to ask anyways.:) what control the speed of executing code(HEX code?) ? IS it the oscillator? if so, can i replace a faster oscillator and make it go faster?.

Thanks
 

mik3

Joined Feb 4, 2008
4,843
ok, I finally got my Arduino board!! yeah!

i was trying to set up a interrupt but I didnt get anywhere after hours of programming. Does any of you have a good article on how to set up a interrupt?
I am trying to execute some function whenever a timer/counter is overflowed.

also, on the Arduino board, i only see one crystal oscillator on the board, does that mean i only have one timer/counter to use?

And finally, this seems like a dumb question, but i am going to ask anyways.:) what control the speed of executing code(HEX code?) ? IS it the oscillator? if so, can i replace a faster oscillator and make it go faster?.

Thanks
What uC is on the board?

No, the oscillator is used to pulse the uC and does not mean that you have only one timer/counter. You have as many as the uC has built in.

If the uC can work with a higher frequency then you can replace the crystal, see the datasheet of the uC for its maximum operating frequency.
 

Thread Starter

Sonoma_Dog

Joined Jul 24, 2008
99
I am going to be using a ATMega168, I think the MAX freq it can handle is 20Mhz, So i should be using a 20Mhz if i want maximum frequency?

and also, i only see a 8bit timer/counter0, 8bit timer/counter2 and 16bit timer/counter1 on the diagram. So where do i get the external interrupt and how should i connect it?


Thanks
 
Last edited:

Thread Starter

Sonoma_Dog

Joined Jul 24, 2008
99
All right here is my Testing interrupt code.
How do i know how often dose the interrupt occur? I have set the prescaler to 1024,
so does it mean it overflow every 16Mhz/1024 =15625hz=64usec?

Rich (BB code):
// This code is for testing Interrupt. 
#include <avr/io.h>
#include <avr/interrupt.h>

int pinone = 8;

void SETUP_IO(void)
{
  pinMode(pinone, OUTPUT);
}

void INIT_TIMER0(void)
{
  TCCR0A = 0x00; //COM0A1:0 and COM0B1:0 set to zero, Waveform generation mode set to Normal
  TCCR0B = 0x05; //Set prescaling to clkIO/1024
  TIMSK0 = 0x01; //Set Overflow interrupt enable
}

ISR(TIMER0_OVF_vect) // This interrupt happens 10 times per second...
{
 digitalWrite(pinone, LOW);
}

int main(void)
{
    digitalWrite(pinone, HIGH);
    SETUP_IO();
    INIT_TIMER0();
    sei();
    while(1) {}
    return(0);
}
 

mik3

Joined Feb 4, 2008
4,843
I am going to be using a ATMega168, I think the MAX freq it can handle is 20Mhz, So i should be using a 20Mhz if i want maximum frequency?

and also, i only see a 8bit timer/counter0, 8bit timer/counter2 and 16bit timer/counter1 on the diagram. So where do i get the external interrupt and how should i connect it?


Thanks
Yes you have to use a 20MHZ crystal for maximum performance.

If you want an interrupt to be activated by an external signal then you dont need to use the timers. You use the timer's interrupt if you want that interrupt to be activated every X seconds.
 

hgmjr

Joined Jan 28, 2005
9,027
All right here is my Testing interrupt code.
How do i know how often dose the interrupt occur? I have set the prescaler to 1024,
so does it mean it overflow every 16Mhz/1024 =15625hz=64usec?

Rich (BB code):
// This code is for testing Interrupt. 
#include <avr/io.h>
#include <avr/interrupt.h>
 
int pinone = 8;
 
void SETUP_IO(void)
{
  pinMode(pinone, OUTPUT);
}
 
void INIT_TIMER0(void)
{
  TCCR0A = 0x00; //COM0A1:0 and COM0B1:0 set to zero, Waveform generation mode set to Normal
  TCCR0B = 0x05; //Set prescaling to clkIO/1024
  TIMSK0 = 0x01; //Set Overflow interrupt enable
}
 
ISR(TIMER0_OVF_vect) // This interrupt happens 10 times per second...
{
 digitalWrite(pinone, LOW);
}
 
int main(void)
{
    digitalWrite(pinone, HIGH);
    SETUP_IO();
    INIT_TIMER0();
    sei();
    while(1) {}
    return(0);
}
I see that you set the pin high with the command digitalWrite(pinone, HIGH);

Then you end up executing the tight While(1) loop relying on the interrupt service routine to toggle the signal at the output pin. However, all the interrupt service routine does is set the output pin low. There is nothing to toggle the output pin.

What is needed is a command that reads the state of the target output pin and inverts it and then writes it back out to the pin.

Ten times per second is a bit fast but I assume that is what you are wanting to do.

hgmjr
 

hgmjr

Joined Jan 28, 2005
9,027
By the way, congratulation on your acquisition of your first microcontroller software/hardware development board. You are in for a lot of fun.

One thing to remember is that there is no single method to flash and LED using a microcontroller. That is the flexibility and the beauty of beast.

Since you are still in the early stages of familiarizing yourself with writing the program I hope you will not mind if I offer up a slight alteration to the basic program you have put forward.

hgmjr

Rich (BB code):
// This code is for testing Interrupt. 
#include <avr/io.h>
#include <avr/interrupt.h>
 
int pinone = 8;
int counter = 0;
 
void SETUP_IO(void)
{
pinMode(pinone, OUTPUT);
}
 
void INIT_TIMER0(void)
{
TCCR0A = 0x00; //COM0A1:0 and COM0B1:0 set to zero, Waveform generation mode set to Normal
TCCR0B = 0x05; //Set prescaling to clkIO/1024
TIMSK0 = 0x01; //Set Overflow interrupt enable
}
 
ISR(TIMER0_OVF_vect) // This interrupt happens 10 times per second...
{
  counter = counter + 1;
}
 
int main(void)
{
digitalWrite(pinone, HIGH);
SETUP_IO();
INIT_TIMER0();
 
sei();
 
while(1) 
{
   if (counter == 10)
   {
        digitalWrite(pinone, HIGH);
   }
 
   if (counter >= 20)
   {
        digitalWrite(pinone, LOW);
        counter = 0;
   }
 
}
return(0);
}
 
Last edited:
Top