;
; Use Timer 1 to flash LED1 when switch SW1 is not pressed
; and flash LED2 when switch SW1 is pressed
;
;==============================================================================
..equ __30F6014, 1
;.include "p30f6014.inc"
;------------------------------------------------------------------------------
;Global Declarations
.global __reset ;The label for the first line of code
.global __OscillatorFail ;Declare Oscillator Fail trap routine label
.global __AddressError ;Declare Address Error trap routine label
.global __StackError ;Declare Stack Error trap routine label
.global __MathError ;Declare Math Error trap routine label
;------------------------------------------------------------------------------
;Configuration bits
config __FOSC, CSW_FSCM_OFF & XT_PLL4
config __FWDT, WDT_OFF
config __FBORPOR, PBOR_OFF & BORV_27 & PWRT_16 & MCLR_EN
config __FGS, CODE_PROT_OFF
;------------------------------------------------------------------------------
.equ FCY, #7372800 ;es una directiva-->asignamos a FCY un valor(Osc x PLL / 4)
;==============================================================================
;Start of code
.text ;Start of Code section
;------------------------------------------------------------------------------
;Initialize stack pointer and limit register
__reset: mov #__SP_init, W15 ;Initalize the Stack Pointer register
mov #__SPLIM_init, W0 ;Get address at the end of stack space
mov W0, SPLIM ;Load the Stack Pointer Limit register
nop ;Add NOP to follow SPLIM initialization
;------------------------------------------------------------------------------
;Initialize LED outputs on PORTD bits 0-3
mov #0xffff, W0 ;Initialize LED pin data to off state
mov W0, LATD
mov #0xfff0, W0 ;Set LED pins as outputs
mov W0, TRISD
bclr LATD, #0 ;Turn LED1 on
;------------------------------------------------------------------------------
;Initialize Timer1 for 1/5 second period
clr T1CON ;Turn off Timer1 by clearing control register
clr TMR1 ;Start Timer1 at zero
mov #FCY/256/5, W0 ;Get period register value for 1/5 second
mov W0, PR1 ;Load Timer1 period register
mov #0x8030, W0 ;Get Timer1 settings (1:256 prescaler)
mov W0, T1CON ;Load Timer1 settings into control register
;------------------------------------------------------------------------------
;Loop while waiting for a Timer1 match and toggle LED1 or LED2 when it happens
MainLoop: btss IFS0, #T1IF ;Check if Timer1 interrupt flag is set
bra MainLoop ;Loop back until set
bclr IFS0, #T1IF ;Clear Timer1 interrupt flag
btss PORTA, #12 ;Test switch SW1 (low when pressed)

btg LATD, #0 ;Toggle LED1 when SW1 is not pressed
bset LATD, #1 ;Turn off LED2
bra MainLoop ;Loop back
SwitchPressed:bset LATD, #0 ;Turn off LED1
btg LATD, #1 ;Toggle LED2 when SW1 is pressed
bra MainLoop ;Loop back
;==============================================================================
;Error traps
;------------------------------------------------------------------------------
;Oscillator Fail Error trap routine
.text ;Start of Code section
__OscillatorFail:
bclr LATD, #3 ;Turn LED4 on
bra __OscillatorFail ;Loop forever when oscillator failure occurs
;------------------------------------------------------------------------------
;Address Error trap routine
__AddressError:
bclr LATD, #3 ;Turn LED4 on
bra __AddressError ;Loop forever when address error occurs
;------------------------------------------------------------------------------
;Stack Error trap routine
__StackError:
bclr LATD, #3 ;Turn LED4 on
bra __StackError ;Loop forever when stack error occurs
;------------------------------------------------------------------------------
;Math (Arithmetic) Error trap routine
__MathError:
bclr LATD, #3 ;Turn LED4 on
bra __MathError ;Loop forever when math error occurs
;==============================================================================
.end ;End of code in this file
