Help with writing delay code for swicth debouncing.

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

Hello to everyone , I the the following code using assembly language in Atmel studio.

 

.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

 

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? I am expecting to make a 50ms delay.

Thank you very much for your time and attention.
 

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

Before this turns into another LOOOOOONG thread on switch debouncing I suggest you do a few searches of the forums on switch debouncing.  There are a LOT of threads here on the subject.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

I'm not real big into assembly, but to debounce you basically have to sample a PIN again and again and look for it to be the same value over a period of time.  So, if you want to debounce a switch for say 30ms, you could sample 6 times at 5ms intervals and say that if the last 6 values are 0, then you switch to 0, and if the last 6 values are 1, then you switch to 1.  What I do usually is keep a counter and if the expected value is different than the last value, I reset the counter to 0.  Then I remember the last value.  If the counter ever gets to its threshold, then the actual state changes because we have sampled that many times in the same state.  I've got some debouncing code in projects here that is old from years ago, but it is in C.

 

To figure out your delay, use the __builtin_avr_delay_cycles(x) function in C where x is the number of cycles you want.  Then compile it.  Then look at the LSS file to see what assembly code it made.

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

jgmdesign wrote:
I suggest you do a few searches of the forums on switch debouncing. 
I agree.

 

A summary of all that you will read: "whatever the language, whether it be Asm or C or something else, read bouncing buttons at a regular period from a timer interrupt, do not use pin change/external interrupt with delays"

 

PS a second thing you will learn is that your most important Google in this is "bounce ganssle"