// * In, shift, and out, repeat that forever * #include <avr/io.h> ; main program function starts here .GLOBAL main ; this is our main program .TEXT main: ; set pin 0 of port B to a push-pull output and output 0 ; other pins kept as inputs as they were originally ; note: the two highest bits of this port should be left alone - connected to quartz crystal in R16, _SFR_IO_ADDR(DDRB) ori R16, 0b00000001 ; note: the two most up bits must always remain zero! out _SFR_IO_ADDR(DDRB), R16 in R16, _SFR_IO_ADDR(PORTB) andi R16, 0b11111110 ; note: the two most up bits must always remain one! out _SFR_IO_ADDR(PORTB), R16 ; set pin 4 of port D to input with a pull-up resistor ; other pins kept as inputs as they were originally ; note: the two lowest bits of this port should be left alone - connected to UART in R16, _SFR_IO_ADDR(DDRD) andi R16, 0b11101111 ; note the two lowest bits must always remain one! out _SFR_IO_ADDR(DDRD), R16 in R16, _SFR_IO_ADDR(PORTD) ori R16, 0b00010000 ; note the two lowest bits must always remain zero! out _SFR_IO_ADDR(PORTD), R16 ; set up a constant to "negate" the push-button key status using ExclusiveOR ; push-button pushed yields input as 0 ; push-button not pushed yields input as 1 due to the pull-up resistor ldi R17, 0b00010000 ; mask to look only for the desired button(s) ; main program forever loop loop: in R16, _SFR_IO_ADDR(PIND) eor R16, R17 and R16, R17 lsr R16 lsr R16 lsr R16 lsr R16 in R18, _SFR_IO_ADDR(PORTB) andi R18, 0b11000000 or R18, R16 ; merge R16 into R18 using OR out _SFR_IO_ADDR(PORTB), R18 rjmp loop ; either expected by linker, equivalent to the "old style" .ORG 0 ; or alternatively generated by the C/C++ compiler on its own accord .TEXT _start: reset_vector: jmp start ; interrupt table would follow *** here *** if there are any interrupt services ; 32bits per entry, up to 34 entries ; this is the part of the loader program that initializes the stack etc. .TEXT start: ldi R16, 0xFF & (RAMEND>>8) out _SFR_IO_ADDR(SPH), R16 ldi R16, 0xFF & RAMEND out _SFR_IO_ADDR(SPL), R16 jmp main