/*
 * Test_DAQ_ASM.asm
 *  Created: 01/11/2016 3:07:13 PM
 *   Author: Arroyo
 */ 
/************************************************************************
 *																		*
 * Title:		Test_DAQ_Asm.asm										*
 * Programmer:	A. Antonio Arroyo										*
 * Date:		January 11, 2016											*
 * Version:		1.0														*
 *																		*
 * Description:															*
 *	Test the Atmel ATxMEGA32A4U in Tim's DAQ Board using the Assembler  *
 *  Blink Internal Debug LED on PC0 and External LEDs in PD0 & PD1      *
 ************************************************************************/

;.include "m128def.inc"				;The regular Atmega128 definitions file
;.include "ATxmega128A1Udef.inc"	;The DIY AtxMega128A1U definitions file
;.include "ATxmega32A4Udef.inc"		;The DAQ AtxMega32A4U definitions file
.org 0x0000             ;Places the following code from address 0x0000
   rjmp  RESET          ;Take a Relative Jump to the RESET Label
	
RESET:                  ;Reset Label (start of Main)
   ldi R16, 0x0A		;Store 12  in R16 (Repeat Count)

   ldi R17, 0x5			;Store 5  in R17 (i, Outer  Loop)
   ldi R18, 0xFF		;Store 255 in R18 (j, Middle Loop)
   ldi R19, 0xFF		;Store 255 in R19 (k, Inner  Loop)
   LDI ZH, HIGH(PORTD_OUT)
   LDI ZL, LOW(PORTD_OUT)	; Z --> PORTD_OUT
   LDI YH, HIGH(PORTC_OUT)
   LDI YL, LOW(PORTC_OUT)	; Y --> PORTC_OUT
   ldi R20, 0x03		;Set PD0 & PD1 direction as Output
   STS PORTD_DIR,R20
   ldi R20, 0x01		;Set PC0 direction as Output
   STS PORTC_DIR,R20

; Set PD0 pin High & PD1 Low
   ldi R20, 0x01		;Set PD0 High & PD1 Low
   ST  Z,	R20
   ldi R20, 0x00		;Set PC0 High
   ST  Y,   R20			;Turn on Debug LED (Active Low)
Loop:					;Delay Loop 1
   dec R19				;Decrement R19
   brne Loop			;If not zero jump to the Loop label
   ldi R19, 0xFF		;Restore 255 in R19
   dec R18				;Decrement R18
   brne Loop			;If not zero jump to the Loop label
   ldi R18, 0xFF		;Restore 255 in R18
   ldi R19, 0xFF		;Restore 255 in R19
   dec R17				;Decrement R17
   brne Loop			;If not zero jump to the Loop label

;Set Set PD0 pin Low & PD1 Hign
   ldi R17, 0x05		;Store 05 in R17
   ldi R18, 0xFF		;Store 0xFF in R18
   ldi R19, 0xFF		;Store 0xFF in R19
   ldi R20, 0x02		;Set PD0 Low & PD1 High (R20) 
   ST  Z,	R20
   ldi R20, 0x01		;Turn off Debug LED (R20) 
   ST  Y,	R20			;Turn off Debug LED (Active Low)

Loop2:					;Delay Loop 2
   dec R19				;Decrement R19
   brne Loop2			;If not zero jump to the Loop label
   ldi R19, 0xFF		;Restore 255 in R19
   dec R18				;Decrement R18
   brne Loop2			;If not zero jump to the Loop label
   ldi R18, 0xFF		;Restore 255 in R18
   ldi R19, 0xFF		;Restore 255 in R19
   dec R17				;Decrement R17
   brne Loop2			;If not zero jump to the Loop label

;Repeat Count times  
   ldi R17, 0x05		;Restore 05  to R17
   ldi R18, 0xFF		;Restore 255 to R18
   ldi R19, 0xFF		;Restore 255 to R19
   ldi R20, 0x01		;Set PD0 High & PD1 Low
   ST  Z,	R20
   ldi R20, 0x00		;Set PC0 High
   ST  Y,   R20			;Set PC0 High
   dec R16				;Decrement COUNT
   brne Loop			;If not zero jump to the Loop label

   ldi R20, 0x03		;Set PD0 High & PD1 High to end
   ST  Z,	R20
   ldi R20, 0x00		;Turn on Debug LED (Active Low)
   ST  Y,   R20
Here: rjmp Here