0

I have this code but i need to alter it to be for the rising edge of pin B2, ive had a look at the datasheet and does the INT0, get changed to INT2? Im just a little unsure and im very new to the ATMEGA324A and avr

/* Set up interrupt to occur on rising edge of pin D2 (start/stop button) */
  EICRA = (1<<ISC01)|(1<<ISC00);
  EIMSK = (1<<INT0);
  EIFR = (1<<INTF0);

This is the current code block I want to add it to: clock

Ticks = 0L;

    /* Clear the timer */
    TCNT0 = 0;

    /* Set the output compare value to be 124 */
    OCR0A = 124;

    /* Set the timer to clear on compare match (CTC mode)
     * and to divide the clock by 64. This starts the timer
     * running.
     */
    TCCR0A = (1<<WGM01);
    TCCR0B = (1<<CS01)|(1<<CS00);

    /* Enable an interrupt on output compare match. 
     * Note that interrupts have to be enabled globally
     * before the interrupts will fire.
     */
    TIMSK0 |= (1<<OCIE0A);

    /* Make sure the interrupt flag is cleared by writing a 
     * 1 to it.
     */
    TIFR0 |= (1<<OCF0A);
1
  • Could you explain what do you want to do with timers? Commented Jun 29, 2020 at 10:46

1 Answer 1

0

At first You should try example with LED. So tasks to do:

  1. Enable external interrupt on PB2, check Pinout Configurations, you should take pin with description which contains INTx - [x - 0/1/2].
  2. Prepare pin to use it as source of external interrupt by setting it DDRx // DDRB if PINB2 as input.
  3. Prepare MCU registers to "catch" your interrupt, EICRA and EIMSK registers as you tried but for pin that you choose (INT2 on PB2).
  4. Write ISR (interrupt subroutines) according to your desired task (I would like to recommend you to start with simple toggle LED when interrupt occurs). You should use macro from avr/interrupt.h named ISR(INT2_vect){ // isr body}.
  5. Enable executing ISRs with sei(); function.

When I used external interrupts at first time I was enabling LED in ISR and disabling external interrupt to prevent multi-execution.

This is short "code" about above instructions.

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

int main(void)
{
  // set your pin as input here
  // set your MCU to "catch" external interrupt here
  // enable interrupts with sei(); instruction here


  while(1);

  return 0;
}

ISR(INT2_vect)
{
  // your action when interrupt occurs here
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.