;******************************************************************************
;   This file is a basic template for assembly code for a PIC18F4550. Copy    *
;   this file into your project directory and modify or add to it as needed.  *
;                                                                             *
;   The PIC18FXXXX architecture allows two interrupt configurations. This     *
;   template code is written for priority interrupt levels and the IPEN bit   *
;   in the RCON register must be set to enable priority levels. If IPEN is    *
;   left in its default zero state, only the interrupt vector at 0x008 will   *
;   be used and the WREG_TEMP, BSR_TEMP and STATUS_TEMP variables will not    *
;   be needed.                                                                *
;                                                                             *
;   Refer to the MPASM User's Guide for additional information on the         *
;   features of the assembler.                                                *
;                                                                             *
;   Refer to the PIC18FXX50/XX55 Data Sheet for additional                    *
;   information on the architecture and instruction set.                      *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Filename:    PlantillaASM                                                *
;    Date:        12/01/11                                                    *
;    File Version: 1.0                                                        *
;                                                                             *
;    Author:   Ing. Alejandro Vicente Lugo Silva                              *
;    Company:   Acad. Computación ICE - ESIME Zac.                            *
;                                                                             * 
;******************************************************************************
;                                                                             *
;    Files required: P18F4550.INC                                             *
;                                                                             *
;******************************************************************************

	LIST P=18F4550, F=INHX32	;directive to define processor
	#include <P18F4550.INC>		;processor specific variable definitions

;******************************************************************************
;Configuration bits

	CONFIG PLLDIV   = 5         ;(20 MHz crystal on PICDEM FS USB board)
    CONFIG CPUDIV   = OSC1_PLL2	
    CONFIG USBDIV   = 2         ;Clock source from 96MHz PLL/2
    CONFIG FOSC     = HSPLL_HS
    CONFIG FCMEN    = OFF
    CONFIG IESO     = OFF
    CONFIG PWRT     = OFF
    CONFIG BOR      = ON
    CONFIG BORV     = 3
    CONFIG VREGEN   = ON		;USB Voltage Regulator
    config WDT      = OFF
    config WDTPS    = 32768
    config MCLRE    = ON
    config LPT1OSC  = OFF
    config PBADEN   = OFF		;NOTE: modifying this value here won't have an effect
        							  ;on the application.  See the top of the main() function.
        							  ;By default the RB4 I/O pin is used to detect if the
        							  ;firmware should enter the bootloader or the main application
        							  ;firmware after a reset.  In order to do this, it needs to
        							  ;configure RB4 as a digital input, thereby changing it from
        							  ;the reset value according to this configuration bit.
    config CCP2MX   = ON
    config STVREN   = ON
    config LVP      = OFF
    config ICPRT    = OFF       ; Dedicated In-Circuit Debug/Programming
    config XINST    = OFF       ; Extended Instruction Set
    config CP0      = OFF
    config CP1      = OFF
    config CP2      = OFF
    config CP3      = OFF
    config CPB      = OFF
    config CPD      = OFF
    config WRT0     = OFF
    config WRT1     = OFF
    config WRT2     = OFF
    config WRT3     = OFF
    config WRTB     = OFF       ; Boot Block Write Protection
    config WRTC     = OFF
    config WRTD     = OFF
    config EBTR0    = OFF
    config EBTR1    = OFF
    config EBTR2    = OFF
    config EBTR3    = OFF
    config EBTRB    = OFF
;******************************************************************************

;Variable definitions

CONTADOR_H	equ	0x30
CONTADOR_L	equ	0x34
Tar_U		equ	0x31
Tar_D		equ	0x32
Tar_C		equ	0x33
Vuelta		equ	0x35

;******************************************************************************
;Reset vector
; This code will start executing when a reset occurs.

		ORG		0x0000
		goto	Main		;go to start of main code
;****************************************************************
		ORG		0x0008
		goto	HighPr_ISR	;go to start of ISR
;**************************************************************** 
;		ORG		0x0018			
;		goto	LowPr_ISR	;go to start of ISR

;****************************************************************
; Start of High Priority ISR
; rutina de servicio de interrupcion prioridad alta ****************************************************************
HighPr_ISR
		INCF	LATD
		RETFIE	FAST
							
		
;****************************************************************
; Start of Low Priority ISR
; Rutina de servicio de interrupcion prioridad baja ****************************************************************
;		ORG 0x1018
LowPr_ISR
		bsf		PORTA,4
		retfie

;****************************************************************

;******************************************************************************
;Start of main program
; The main program code is placed here.


ORG		0x1000
Main 				; *** main code goes here **
		call	C_PUERTOS
		call	C_VUELTAS

INICIO
		btfsc	PORTC,2
		goto	DIA
		btfsc	PORTC,4
		goto	NOCHE
		goto	INICIO

C_VUELTAS
		bcf		RCON,IPEN
		movlw	0x50
		movwf	INTCON
		bcf		INTCON2,INTEDG0
		movlw	0x0F
		movwf	Vuelta
		bsf		INTCON,GIE
		return

C_PUERTOS
		movlw	0x0F
		movwf	ADCON1
		clrf	TRISA
		clrf	TRISD
		setf	TRISC
		setf	TRISB
		clrf	LATA
		clrf	LATD
		return

DIA
		movlw	0x07
		movwf	Tar_U
		movff	Tar_U,LATD
D1		btfss	PORTC,5
		goto	D1
D4		movlw	d'12'
		movwf	CONTADOR_H
		movlw	d'4'
		movwf	CONTADOR_L		
		bsf		LATA,5
D2		call	TIMER
		decfsz	CONTADOR_H
		goto	D2
		bcf		LATA,5
D3		call	TIMER
		decfsz	CONTADOR_L
		goto	D3
		goto	D4

NOCHE
		movlw	0x15
		movwf	Tar_U
		movff	Tar_U,LATD
N1		btfss	PORTC,5
		goto	D1
N4		movlw	d'12'
		movwf	CONTADOR_H
		movlw	d'4'
		movwf	CONTADOR_L		
		bsf		LATA,5
N2		call	TIMER
		decfsz	CONTADOR_H
		goto	N2
		bcf		LATA,5
N3		call	TIMER
		decfsz	CONTADOR_L
		goto	N3
		goto	N4

TIMER
		movlw	0x08
		movwf	T0CON
		movlw	0xD1
		movwf	TMR0H
		movlw	0x20
		movwf	TMR0L
		bcf		INTCON,2
		bsf		T0CON,7
T1		btfss	INTCON,2
		goto	T1
		return
		



	
					; end of main	
;******************************************************************************
; Start of 
;******************************************************************************


;******************************************************************************

					
;******************************************************************************
;End of program
	END