;******************************************************************************
;   This file is a basic relocatable code template for code generation        *
;   on the  PIC12HV615. This file contains the basic code                     *
;   building blocks to build upon.                                            *
;                                                                             *
;   If interrupts are not used all code presented between the ORG             *
;   0x004 directive and the label main can be removed. In addition            *
;   the variable assignments for 'w_temp' and 'status_temp' can               *
;   be removed. If the internal RC oscillator is not implemented              *
;   then the first four instructions following the label 'main' can           *
;   be removed.  The TMR0 sample code may be uncommented for test.            *
;                                                                             *
;   Refer to the MPASM User's Guide for additional information on             *
;   features of the assembler.                                                *
;                                                                             *
;   Refer to the respective data sheet for additional                         *
;   information on the instruction set.                                       *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Filename:         xxx.asm                                                *
;    Date:                                                                    *
;    File Version:                                                            *
;                                                                             *
;    Author:                                                                  *
;    Company:                                                                 *
;                                                                             *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Files required: P12HV615.INC                                             *
;                                                                             *
;                                                                             *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Features of the 12HV615:                                                 *
;                                                                             *
;    Flash Program Memory                                                     *
;    Internal 4/8MHz Oscillator                                               *
;    Comparator with hysterisis (user configurable)                           *
;    Mid-Range core with 35 instruction, 8 stack levels                       *
;    25 mA Source/Sink Current I/O                                            *
;    Two 8-bit Timers (TMR0/TMR2)                                             *
;    One 16-bit Timer (TMR1)                                                  *
;    Watchdog Timer (WDT)                                                     *
;    Enhanced Power-On/Off-Reset                                              *
;    Brown-Out Reset (BOR)                                                    *
;    In Circuit Serial Programming (ICSP)                                     *
;    Enhanced Capture Compare PWM (Pulse Width Modulation)                    *
;    Wide Operating Voltage (2.0V - User Defined Max)                         *
;    Internal Shunt Regulator for High Voltage Vdd Support                    *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Notes: 2/28/2007                                                         *
;                                                                             *
;    In order to debug the 12HV615 with an ICD2, you will need a              *
;    special debug header (part AC162083) available from                      *
;    http://microchipdirect.com/.  Refer to the 'Header Board                 *
;    Specification' DS51292M as well the datasheet for the 12HV615 to         *
;    make sure your jumper settings are correct.                              *
;                                                                             *
;    The biggest difference between the 12F615 and the 12HV615 is the         *
;    addition of a permanent internal 5 Volt (nominal) shunt                  *
;    regulator in parallel with the Vdd pin.  This eliminates the             *
;    need for an external voltage regulator in systems sourced by an          *
;    unregulated supply.  All external devices connected directly to          *
;    the Vdd pin will share the regulated supply voltage and                  *
;    contribute to the total Vdd supply current.  For information             *
;    more information on the regulator, see chapter 12 of the                 *
;    datasheet for the 12HV615.                                               *
;                                                                             *
;                                                                             *
;                                                                             *
;******************************************************************************


     list      p=12HV615            ; list directive to define processor
     #include <P12HV615.inc>        ; processor specific variable definitions

     __CONFIG   _CP_OFF & _BOR_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _IOSCFS_8MHZ

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.

;------------------------------------------------------------------------------
; Variable Definitions
;
; Available General Purpose Memory divided into Bank 0 and Bank 1.  In 
; Bank 0 General Purpose Registers range from 0x40-0x7F.   In Bank 1 the only
; General Purpose Registers available are Access Bank Registers.  These are 
; actually shared between Bank 0 and Bank 1. The Access Bank Registers  
; range from 0x70-0x7F in Bank 0 address space, which is also 0xF0-0xFF in
; Bank 1 address space.  
;
;------------------------------------------------------------------------------

; UDATA declares a section of uninitialized data
VARIABLES   UDATA           ; VARIABLES is the name of the section of memory
my_var      RES     1       ; uninitialized data, placed by linker in GPR's. 

; UDATA_SHR declares a section of shared (across all banks) uninitialized data
INT_VAR        UDATA_SHR        ; INT_VAR is the section name in Access RAM
w_temp         RES     1        ; variable used for context saving 
status_temp    RES     1        ; variable used for context saving

;------------------------------------------------------------------------------
; RESET VECTOR
;------------------------------------------------------------------------------
RESET_VECTOR    CODE   0x0000
          goto    START             ; When using debug header, ICD2 may not stop
                                    ; on instruction 0 during reset.

;------------------------------------------------------------------------------
; INTERRUPT SERVICE ROUTINE
;------------------------------------------------------------------------------

INT_VECTOR        CODE   0x0004 ; Interrupt Vector Location
          goto    INTERRUPT

PROGRAM     CODE

INTERRUPT                     ; Relocatable Interrupt Service Routine

;         Context saving for ISR
          MOVWF   w_temp            ; save off current W register contents
          MOVF    STATUS,w          ; move status register into W register
          MOVWF   status_temp       ; save off contents of STATUS register

;         INSERT INTERRUPT CODE HERE

;;         -----SAMPLE CODE----- if the interrupt came from the timer, execute
;;         the TMR0 interrupt service routine. 
;          BTFSC   INTCON, T0IF ; Uncomment this line to test sample code 
;          CALL    TMR0_ISR     ; Uncomment this line to test sample code     

;         Restore context before returning from interrupt
          MOVF    status_temp,w   ; retrieve copy of STATUS register
          MOVWF   STATUS          ; restore pre-isr STATUS register contents
          SWAPF   w_temp,f
          SWAPF   w_temp,w        ; restore pre-isr W register contents
          RETFIE                  ; return from interrupt

START

; SET OSCILLATOR TO FACTORY FREQUENCY AND CLEAR GPR's 

          errorlevel -302     ; disable warning accessing register not in bank 0
          banksel OSCTUNE     ; select bank 1 using mpasm macro 
          movlw   0x00        ; set oscillator to factory calibrated frequency 
          movwf   OSCTUNE
          banksel GPIO        ; select bank 0 using mpasm macro 
          errorlevel +302     ; re-enable warning accessing register not in bank 0
CLEAR_RAM                     ; code sequence initializes all GPR's to 0x00
                              ; unnecessary if init. all variables before use
          movlw   0x40        ; initialize pointer
          movwf   FSR         ; to RAM
NEXT  
          clrf    INDF        ; clear INDF register
          incf    FSR, F      ; inc pointer
          btfss   FSR,7       ; all done?
          goto    NEXT        ; no clear NEXT
CONTINUE                      ; yes CONTINUE
          nop

;------------------------------------------------------------------------------
; SAMPLE PROGRAM... REMAINDER OF YOUR PROGRAM GOES HERE
;------------------------------------------------------------------------------

;----SAMPLE CODE-----

;; This is some example code that uses the timer 0 interrupt to branch to the 
;; Interrupt Service Routine.  This code is not necessary.
;          CLRF    TMR0              ; Uncomment this line to test sample code
;          BSF     INTCON, GIE       ; Uncomment this line to test sample code
;          BSF     INTCON, PEIE      ; Uncomment this line to test sample code
;          BSF     INTCON, T0IE      ; Uncomment this line to test sample code
;          errorlevel -302           ; Uncomment this line to test sample code
;          BANKSEL OPTION_REG        ; Uncomment this line to test sample code
;          BSF     OPTION_REG, PSA   ; Uncomment this line to test sample code
;          BCF     OPTION_REG, T0CS  ; Uncomment this line to test sample code
;          errorlevel +302           ; Uncomment this line to test sample code
;LOOP      GOTO    LOOP              ; Uncomment this line to test sample code
;
;;TIMER 0 Interrupt routine
;TMR0_ISR                            ; Uncomment this line to test sample code
;          BANKSEL INTCON            ; Uncomment this line to test sample code
;          BCF     INTCON, T0IF      ; Uncomment this line to test sample code
;          RETURN                    ; Uncomment this line to test sample code

          END
