Help with using two interrupt pins in ATmega328p

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone, I wrote a program that works with a single interrupt. I want use two interrupt ports in the same time. The following is my code. Please tell me what I am doing wrong since this does only work for I2.

 

.ORG 0
    JMP MAIN
.ORG 0x02
    JMP I1
.ORG 0x04
    JMP I2

MAIN:      

//INT0
        LDI R20,HIGH(RAMEND)
        OUT SPH,R20
        LDI R20,LOW(RAMEND)
        OUT SPL,R20
        LDI R20,0x2
        STS EICRA,R20
        SBI PORTD,2
        LDI R20,1<<INT0
        OUT EIMSK,R20

//INT1 
        LDI R22,HIGH(RAMEND)
        OUT SPH,R22
        LDI R22,LOW(RAMEND)
        OUT SPL,R22
        LDI R20,0x4
        STS EICRA,R22
        SBI PORTD,3
        LDI R22,1<<INT1
        OUT EIMSK,R20

        SEI
        
HERE:    JMP HERE

I1:
        //Do routine 1
        RETI

I2:  //Do routine 2
        RETI

 

Thank you very much for your time and attention.

 

What I am trying to accomplish here is, when PD2(int0) goes low - perform I1

                                                          when PD3(int1) goes low - perform I2

 

This topic has a solution.
Last Edited: Sat. Sep 7, 2019 - 10:40 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
.ORG 0
    JMP MAIN
.ORG 0x02
    JMP I1
.ORG 0x04
    JMP I2
MAIN:      
//INT0
        LDI R20,HIGH(RAMEND)
        OUT SPH,R20
        LDI R20,LOW(RAMEND)
        OUT SPL,R20
        LDI R20,0x6
        STS EICRA,R20
        SBI PORTD,2
        SBI PORTD,3
        LDI R20,(1<<INT0) | (1<<INT1)
        OUT EIMSK,R20
        SEI
        
HERE:    JMP HERE
I1:
        //Do routine 1
        RETI
I2:  //Do routine 2
        RETI

No use setting up the stack pointer twice and you were overwriting the EICRA and EIMSK registers so that only the last setting actually worked.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you very much kind sir. I wrote the code I posted with no sleep :(. Your solution is actually working. Another problem I am facing is is switch bouncing. I was going to use a capacitor. Would you mind helping with adding a delay inside the main program?

 

Thank you very much again for the help kind sir:)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you're stretching the friendship here. It's your homework - it's for your benefit. Why would you use external interrupts for switch inputs? Set up a timer to read the switches at a regular interval - say 10ms. No delays needed - just count highs or lows.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:

I think you're stretching the friendship here. It's your homework - it's for your benefit. Why would you use external interrupts for switch inputs? Set up a timer to read the switches at a regular interval - say 10ms. No delays needed - just count highs or lows.

 

Sorry If I overstepped my boundaries. I am a student aye. But no I don't get this for homework. I am 16 and doing this as a hobby. I am trying to build circuits using interrupts.  I am following this slide series I found from the internet. I haven't studied timers yet. I see you have given a hint. Which is very helpful to me. Thank you sir, I'll stop this here.