/****
 * GPIO_Output.asm
 *
 *  Modified: 09 Sept 2021
 *  Author: Dr. Schwartz
 
 This program shows how to initialize a GPIO port on the Atmel 
 (Port D for this example) and demonstrates various ways to write to 
 a GPIO port.  The output will blink LEDs at the bottom left of the 
 uPAD, labeled D4. PortD4, PortD5, and PortD6 are the red, green, 
 and blue LEDs, respectively.  Note that these LEDs are active-low.
****/

;Definitions for all the registers in the processor. ALWAYS REQUIRED.
;View the contents of this file in the Processor "Solution Explorer" 
;   window under "Dependencies"
.include "ATxmega128A1Udef.inc"

.equ	BIT4 = 0b00010000
.equ	INV4 = 0b11101111
.equ	RED = INV4
.equ	BIT5 = 0b00100000
.equ	INV5 = ~BIT5
.equ	GREEN = ~(BIT5)
.equ	BIT6 = 0x40
.equ	BLUE = ~(BIT6)
.equ	BIT456 = 0x70
.equ	WHITE = ~(BIT456)
.equ	BIT64 = 0x50
.equ	PINK = ~(BIT64)
.equ	BLACK = 0xFF

; Any necessary initializations for SRAM in data space?

.ORG 0
;Skip address through 0x00FD (interrupt vectors)
	rjmp MAIN

;Start program at 0x0100 so we don't overwrite vectors (0-0x00FD)
.ORG 0x0100				
MAIN:
; initialize the data direction of the three LEDs as outputs (PD4-6)
	ldi R16, BIT456
	sts PORTD_DIRSET, R16
; Notice that the 3 LEDs (RED, GREEN, and BLUE) are all now on, creating white

; The following code shows different ways to write to the GPIO pins.

; Turn on each of the primary colored LEDs in turn, then use some combinations
	; These instructions sends the value in R16 to the PORTD pins. 
	; Since the LEDs are wired as active-low, 
	;    R16 = RED = 0xEF = 0b1110 1111 will turn the RED LED on.
	ldi	R16, RED
	sts PORTD_OUT, R16 		

; Turn on the green LED (only)
	ldi	R16, GREEN
	sts PORTD_OUT, R16

; Turn on the blue LED (only)
	ldi	R16, BLUE
	sts PORTD_OUT, R16

; Turn on all of the colored LEDs to make white
	ldi	R16, WHITE
	sts PORTD_OUT, R16

; Turn on blue and red to make a pink (magenta)
	ldi	R16, PINK
	sts PORTD_OUT, R16

; Turn off all of the LEDs (i.e., make it black)
	ldi	R16, BLACK
	sts PORTD_OUT, R16

; Turn on the blue LED (only)
; Notice that the OUTCLR makes the (blue) LED go on, 
;   since the LED is active-low
	ldi	R16, BIT6			; BIT6 = ~BLUE
	sts PORTD_OUTCLR, R16

; Turn off the blue LED (i.e., make BIT6 a 1, using OUTSET)
; Notice that the OUTSET makes the (blue) LED go off, 
;  since the LED is active-low
	ldi	R16, BIT6
	sts PORTD_OUTSET, R16

; Notice that the OUTTGL toggles the value of a PORT pin 
;   (in this case the BLUE LED)
LOOP:
	sts PORTD_OUTTGL, R16
	rjmp LOOP				;repeat forever!
