Questions on interrupt

Discussion in 'Embedded Systems and Microcontrollers' started by Sonoma_Dog, Sep 18, 2008.

  1. Sonoma_Dog

    Thread Starter Active Member

    Jul 24, 2008
    99
    0
    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
     
  2. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    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.
     
  3. Sonoma_Dog

    Thread Starter Active Member

    Jul 24, 2008
    99
    0
    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: Sep 19, 2008
  4. Sonoma_Dog

    Thread Starter Active Member

    Jul 24, 2008
    99
    0
    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?

    Code ( (Unknown Language)):
    1.  
    2. // This code is for testing Interrupt.
    3. #include <avr/io.h>
    4. #include <avr/interrupt.h>
    5.  
    6. int pinone = 8;
    7.  
    8. void SETUP_IO(void)
    9. {
    10.   pinMode(pinone, OUTPUT);
    11. }
    12.  
    13. void INIT_TIMER0(void)
    14. {
    15.   TCCR0A = 0x00; //COM0A1:0 and COM0B1:0 set to zero, Waveform generation mode set to Normal
    16.   TCCR0B = 0x05; //Set prescaling to clkIO/1024
    17.   TIMSK0 = 0x01; //Set Overflow interrupt enable
    18. }
    19.  
    20. ISR(TIMER0_OVF_vect) // This interrupt happens 10 times per second...
    21. {
    22.  digitalWrite(pinone, LOW);
    23. }
    24.  
    25. int main(void)
    26. {
    27.     digitalWrite(pinone, HIGH);
    28.     SETUP_IO();
    29.     INIT_TIMER0();
    30.     sei();
    31.     while(1) {}
    32.     return(0);
    33. }
    34.  
     
  5. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    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.
     
  6. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    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
     
  7. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    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

    Code ( (Unknown Language)):
    1.  
    2. // This code is for testing Interrupt.
    3. #include <avr/io.h>
    4. #include <avr/interrupt.h>
    5.  
    6. int pinone = 8;
    7. [COLOR=red][B]int counter = 0;[/B][/COLOR]
    8.  
    9. void SETUP_IO(void)
    10. {
    11. pinMode(pinone, OUTPUT);
    12. }
    13.  
    14. void INIT_TIMER0(void)
    15. {
    16. TCCR0A = 0x00; //COM0A1:0 and COM0B1:0 set to zero, Waveform generation mode set to Normal
    17. TCCR0B = 0x05; //Set prescaling to clkIO/1024
    18. TIMSK0 = 0x01; //Set Overflow interrupt enable
    19. }
    20.  
    21. ISR(TIMER0_OVF_vect) // This interrupt happens 10 times per second...
    22. {
    23. [B]  [COLOR=red]counter = counter + 1;[/COLOR][/B]
    24. }
    25.  
    26. int main(void)
    27. {
    28. digitalWrite(pinone, HIGH);
    29. SETUP_IO();
    30. INIT_TIMER0();
    31.  
    32. sei();
    33.  
    34. while(1)
    35. {
    36. [COLOR=red][B]   if (counter == 10)[/B][/COLOR]
    37. [COLOR=red][B]   {[/B][/COLOR]
    38. [COLOR=red][B]        digitalWrite(pinone, HIGH);[/B][/COLOR]
    39. [COLOR=red][B]   }[/B][/COLOR]
    40.  
    41. [COLOR=red][B]   if (counter >= 20)[/B][/COLOR]
    42. [COLOR=red][B]   {[/B][/COLOR]
    43. [COLOR=red][B]        digitalWrite(pinone, LOW);[/B][/COLOR]
    44. [COLOR=red][B]        counter = 0;[/B][/COLOR]
    45. [COLOR=red][B]   }[/B][/COLOR]
    46.  
    47. }
    48. return(0);
    49. }
    50.  
     
    Last edited: Sep 20, 2008
Loading...