;=================================================================================
;      CLOCK: Internal RC 4MHz (instruction execution time: 1usec)
;		This is a basic start template for PIC12F683
;		Includes delay routines 
;		Program name:
;		Date:
;		Author:
;		
;==================================================================================
        list     P=12F683, ST=OFF   ; Turnoff Symbol Table in List file.

        errorlevel  -302        ; Ignore error message when storing to Bank 1.
;==================================================================================

        #include   <p12f683.inc>

   __config _INTOSCIO & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF &  _CP_OFF & _CPD_OFF & _BOD_NSLEEP & _IESO_OFF & _FCMEN_OFF

; ------------------------------------
; Configuration Bit Settings Explained
; ------------------------------------
; Internal RC Oscillator, No Clock Out
; Watch Dog Timer = OFF
; Power Up Timer = ON
; MCLR = INTERNAL
; Code Protect = ON
; Data EE Read Protect = OFF
; Brown Out Detect = Enabled in Run, Disabled in Sleep, SBOREN Disabled
; Internal External Switch Over Mode (2-speed Startup) = OFF
; Monitor Clock Fail-safe = OFF
;======================================================================

;*****[ Data Storage Reg's ]*****
            CBLOCK  0x20 ; Assign each reg. from Bank 0 RAM area.
            count1              
            count2             
            count3
            count4
			w_temp       ; variable used for context saving 
			status_temp  ; variable used for context saving		
            ENDC         ; Conclude Bank 0 RAM assignments.

;**********************************************************************
	ORG     0x000             ; processor reset vector
	goto    setup              ; 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
	movwf	status_temp       ; save off contents of STATUS register

	; isr code can go here or be located as a call subroutine elsewhere

	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

;**********************************************************************
			
			
setup
        BANKSEL OSCCON          ; Switch to Bank 1.
        MOVLW   b'01100001'     ; 4MHz Clk, IntOsc, SysClk via IntOsc
        MOVWF   OSCCON

        BANKSEL CMCON0          ; Switch to Bank 0.
        MOVLW   b'00000111'     ; Turn off Comparator.
        MOVWF   CMCON0  
 
        BANKSEL ANSEL           ; Switch to Bank 1.
        CLRF    ANSEL           ; Set I/O pins to Digital.

        ; Define inputs & outputs.
        CLRF   TRISIO ; all output
        
        BANKSEL GPIO            ; Switch to Bank 0.
        CLRF    GPIO            

goto main


main


; main code here


goto main
		
; delay routines *************************************

; Calculating a 1mSec delay. 4mHz is divided by 4 internally to
; 1,000,000. Take reciprocal divide 1mSec by 1uSec = 1000.
; GOTO uses 2 cycles DECFSZ 1 cycle = 3 cycles or 3uSec.
; 3 * 167 * 2 = 1000uSec. or 1mSec.


delay_1ms
	; 4 mHz crystal
	movlw D'2'
	movwf count1
aa	movlw D'167'
	decfsz count2, F
	goto $-1 ; two cycles
	decfsz count1, F
	goto aa 
	return


delay_100ms
	; 16  mHz crystal
	movlw d'100'
	movwf count3
bb	call delay_1ms
	decfsz count3, F
	goto bb
	return
	
end