Creating a toggle switch in AVR assembly
<p>I want to create a program in AVR assembly that will poll the state of a momentary pushbutton switch and toggle the state of an LED whenever the switch is pressed. I am using an Arduino Duemilanove with an ATMega328P chip. I have a pushbutton switch connected between digital pin 0 and ground, and an LED with 330 ohm resistor connected between digital pin 8 and +5V. Here is my code so far:</p>

<pre><code>;==============
; Declarations:

.def temp = r16
.org 0x0000
rjmp Reset

;==============

Reset:
        ser temp
        out DDRB, temp         ; set all pins on Port B to OUTPUT mode
        ldi temp, 0b11111110   ; set pin 0 on Port D to INPUT mode
        out DDRD, temp
        clr temp
        out PORTB, temp        ; clear temp and set all pins on Port B to LOW state
        ldi temp, 0b00000001   ; set pin 0 on Port D to HIGH state
        out PORTD, temp

;==============
; Main Program:

switch:
        in temp, PIND          ; get state of pins on Port D
        cpi temp, 0            ; compare result to 0 (pushbutton is pressed)
        brne PC+3              ; if != 0, go check again
        ldi temp, (1&lt;&lt;PB0)     ; otherwise, write logic 1 to pin 0 of Port B
        out PINB, temp         ; which toggles the state of the pin
        rjmp switch
</code></pre>

<p>Unfortunately all this does is light the LED and keep it on no matter how many times the button is pushed. I am basing this code off of a program found <a href="http://www.instructables.com/id/Command-Line-Assembly-Language-Programming-for-Ard-1/step2/Writing-the-assembly-code/" rel="nofollow">here</a> that turns the LED on <em>as long as</em> the button is pushed. I simply want to extend that to hold the LED in its current state until the button is pushed again. Any suggestions?</p>