Skip to content

Instantly share code, notes, and snippets.

@BobBurns
Created November 20, 2014 00:27
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BobBurns/c96ecad1b6ab0921389d to your computer and use it in GitHub Desktop.
Save BobBurns/c96ecad1b6ab0921389d to your computer and use it in GitHub Desktop.
AVR Assembly simple debouncer
;example program using debouncing
;from AVR Programming by Elliot Williams p.115
;
;compile with gavrasm debouncer.asm
;flash with avrdude -c avrisp -p m168 -P /dev/tty.usbmodem1411 -b 19200 -U flash:w:debouncer.hex
;
.def temp = r16
.device atmega168
;--- inits ---
.def b_pressed = r17
.def eoReg = r18
sbi PORTD,PD2 ;set pullup bit on button
sbi DDRB,PB0 ;set ddr output on led pin
loop:
rcall debounce
brcc else
tst b_pressed
brne loop
in temp,PORTB
ldi eoReg,(1 << PB0)
eor temp,eoReg
out PORTB,temp
ldi b_pressed,1
rjmp loop
else:
ldi b_pressed,0
rjmp loop
;--- suboutines ---
debounce:
.equ d_time = 1000
sbic PIND,PD2 ;bit is clear=button is pressed
rjmp bitSet
ldi r25,high(d_time)
ldi r24,low(d_time)
delay: sbiw r25:r24,1
brne delay
sbic PIND,PD2 ;button still pressed?
rjmp bitSet
sec
ret
bitSet:
clc
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment