;**********************************************************************
;   This file is a basic code template for assembly code generation   *
;   on the PICmicro PIC16F874. 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.                                                       *                         
;                                                                     *
;   Refer to the MPASM User's Guide for additional information on     *
;   features of the assembler (Document DS33014).                     *
;                                                                     *
;   Refer to the respective PICmicro data sheet for additional        *
;   information on the instruction set.                               *
;                                                                     *
;   Template file assembled with MPLAB V4.00 and MPASM V2.20.00.      *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename:	    test874.asm                                       *
;    Date:          13 February 2001                                  *
;    File Version:  1                                                 *
;                                                                     *
;    Author:        CDR Charles B. Cameron, USN                       *
;    Company:       United States Naval Academy                       *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files required:                                                  *
;                                                                     *
;                  p16f874.inc                                        *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;    This program is sufficient for ensuring that the PIC16F874       *
;    works with the HS clock mode.  If the microcontroller is         *
;    working then pin 2 will produce a pulse with a 25% duty cycle    *
;    (+5 V for one instruction cycle, 0 V for three instruction       *
;    cycles).                                                         *
;                                                                     *
;**********************************************************************


	list      p=16f874            ; list directive to define processor
	#include <p16f874.inc>        ; processor specific variable definitions
	
	__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_ENABLE_ON & _LVP_OFF & _CPD_OFF

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The labels following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.

; The particular choice given above turns code protection off, watch dog timer off,
; brown-out reset disabled, power-up timer enabled, HS oscillator mode selected,
; flash program memory write disabled, low-voltage in-circuit serial programming
; disabled, and data EE memory code protrection off.  Change these at will!




;***** VARIABLE DEFINITIONS
w_temp        EQU     0x20        ; variable used for context saving 
status_temp   EQU     0x21        ; variable used for context saving







;**********************************************************************
		ORG     0x000             ; processor reset vector
		clrf    PCLATH            ; ensure page bits are cleared
  		goto    main              ; go to beginning of program


		ORG     0x004             ; interrupt vector location
		movwf   w_temp            ; save off current W register contents
		movf	STATUS,w          ; move status register into W register
		bcf     STATUS,RP0        ; ensure file register bank set to 0
		movwf	status_temp       ; save off contents of STATUS register


; isr code can go here or be located as a call subroutine elsewhere


		bcf     STATUS,RP0        ; ensure file register bank set to 0
		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



main

; remaining code goes here

; ***********************************************************************************
; Here is the code to test a PIC16F874.

; Configure Port A.  Let all bits be outputs.  We'll use Bit 0 to output a test signal.
	bcf	STATUS,RP0	; Select Bank 0
	bcf	STATUS,RP1
	clrf	PORTA		; Initialize Port A by clearing the output latches.
	bsf	STATUS,RP0	; Select Bank 1
	movlw	B'00000110'	; Let all pins for digital use, not analog use.
	movwf	ADCON1
	movlw	B'00000000'	; Let all Port A pins be used for output.
	movwf	TRISA
	bcf	STATUS,RP0	; Revert to Bank 0


loop
	bsf	PORTA,0		; Toggle bit 0 of Port A
	bcf	PORTA,0		; Toggle it again
	goto	loop		; Continue indefinitely.  The result will be a 25% duty cycle
				; with a frequency 1/4 of the basic instruction frequency
				; or 1/16 of the clock frequency.
; ***********************************************************************************




		END                       ; directive 'end of program'


