Skip to content

Instantly share code, notes, and snippets.

@cpjobling
Last active November 9, 2023 17:25
Show Gist options
  • Save cpjobling/e72f37ff363353b6ed10ecdbeaa970af to your computer and use it in GitHub Desktop.
Save cpjobling/e72f37ff363353b6ed10ecdbeaa970af to your computer and use it in GitHub Desktop.
Switches and LEDs - An Atmel ATmega328 Program
;Equate easy to read names to the I/O memory addresses
.EQU DDRB = 0x04
.EQU PORTB = 0x05
.EQU PIND = 0x09
.EQU DDRD = 0x0A
.EQU PORTD = 0x0B
.CSEG
.ORG 0x0200
;setup bits 2 and 3 of port D as inputs
IN R16, DDRD
ANDI R16, 0b11110011
OUT DDRD, R16
;setup bits 0 and 1 of port B as outputs
IN R16, DDRB
ORI R16, 0b00000011
OUT DDRB, R16
;both pins B0 (D8) and B1 (D9) start low
IN R16, PORTB
ANDI R16, 0b11111100
OUT PORTB, R16
;Enable the pull up resistor for bits 2 and 3 of port D
IN R16, PORTD
ORI R16, 0b00001100
OUT PORTD, R16
LOOP:
IN R16, PIND
ANDI R16, 0b00000100
LDI R17, 0x00
CP R16, R17
BREQ LED1
IN R16, PIND
ANDI R16, 0b00001000
LDI R17, 0x00
CP R16, R17
BREQ LED2
IN R16, PORTB
ANDI R16, 0b11111100
OUT PORTB, R16
RJMP LOOP
LED1:
IN R16, PORTB
ORI R16, 0b00000001
OUT PORTB, R16
RJMP LOOP
LED2:
IN R16, PORTB
ORI R16, 0b00000010
OUT PORTB, R16
RJMP LOOP
.EXIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment