Help with assembly program that uses two interrupt pins (ATmega328P)

Hello to everyone :slight_smile: I have written the following code using assembly language in Atmel studio.

.ORG 0
JMP MAIN
.ORG 0x02
JMP i1
.ORG 0x04
JMP i2
MAIN:
** **
** **
** SEI**
HERE: JMP HERE
i1:
** RETI**
i2:
** RETI**

I have built the circuit for this code in a breadboard. I have connected two switches. What it does is, when the first button is pressed it does i1 routine and so on. Everything works well except one thing.

I am experiencing bouncing in switch. Sometimes it takes two inputs at once. I don't want to use a capacitor to fix this. Can someone help me to write a delay program in the main code?

Thank you very much for your time and attention.

How much delay do you want? The typical de-bouncing time of a mechanical switch is about 20 ms within which the bouncing of the switch is expected to have decayed.

I am not sure about the magnitude of the delay. I am a beginner tbh. I was thinking about 20/50ms (either one you see fit) so the user won't feel any difference. I don't have the capability to write one myself. Thanks for any help kind sir:)

Please, post/attach as file the full ASM codes of the skeleton of your original post.

What is r1?

GolamMostafa:
Please, post/attach as file the full ASM codes of the skeleton of your original post.

.ORG 0
JMP MAIN
.ORG 0x02
JMP i1
.ORG 0x04
JMP i2

MAIN: 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:
LDI DDRB,0x01
OUT DDRB,R21
OUT PORTB,R21
RETI

i2: LDI DDRB,0x02
OUT DDRB,R21
OUT PORTB,R21
RETI

TheMemberFormerlyKnownAsAWOL:
What is r1?

I corrected my mistake. I apologize.

From your code, I have understood that you are triggering INTO interrupt at the falling edge of the incoming interrupting signal. If it would be my project, I would think to employ a hardware non-retriggerable one shot circuit instead of time delay to over come the bouncing times of the interrupting device.
oneshot.png
Figure-1: Hardware debounce circuit

Functional Description: When K1 is closed at time t1, the signal-A goes LOW and then after a while the bouncing of the button begins which lasts for about 25 ms before the button makes the final sit. At the very first low-going edge of signal-A (at time t1), the signal-B assumes HIGH state and remains HIGH during the whole bouncing times of K1 (because 74LS221 is a non-retriggering one shot, it does not respond to the bouncing transitions of K1). At time t2, the signal-B makes a transition from HIGH to LOW; as a result, the MCU is interrupted only once. The pulse width of signal-B (tw) is about: 0.7R1C1 = 0.7*4.7x1031010-6 ~= 33 ms (>25 ms).

Reference: (extracted from here)
debounce.png
Figure-2: Bouncing phenomenon

oneshot.png

debounce.png

Thank you for the reply and the effort good sir. But the hardware setup is fixed and my objective is to write a code to achieve the the objective mentioned. I want to write a code to create a delay hence prevent bouncing. Would it be possible for you to help me write a delay code?

Thank you very much for your time :slight_smile:

esavrperson:
Thank you for the reply and the effort good sir. But the hardware setup is fixed and my objective is to write a code to achieve the the objective mentioned. I want to write a code to create a delay hence prevent bouncing. Would it be possible for you to help me write a delay code?

This is the 33 ms TimeDelay subroutine using TC1 of ATmega328P MCU. There is no assembly errors.

TimeDelay:
ldi	         r16, 0x00		;normal mode operation of TC1
sts		TCCR1A, r16
sts		TCCR1B, r16	;TC1 is OFF; to be run at 15625 Hz
ldi		r17, 0xFD		;preset value for 33 ms at clkTC1 = 16MHz/1024
sts		TCNT1H, r17	;load upper part od TC1 first
sts		TCNT1L, r17	;preset count oxFDFD for TCNT1 for 33 ms time delay
sbi		TIFR1, 0		; clear TOV1 bit
ldi		r17, 0x07		;
sts		TCCR1B, r17	;run Tc1 at 16 MHz/1024 clock rate
;-----check if TOV1 has occured at 33 ms elapse
AGN:	
in		r17, TIFR1		;read TIFR1 register to check TOV1 bit
ror		r17			;place TOV1 bit into carry bit of SREG
brcc	        AGN			;TOV1 is not yet HIGH; 33 ms has not elapsed
ret					;33 ms has elapsed; return to Main Line Program