;   This file is a basic code template for assembly code generation   *
;   on the PICmicro PIC12F683. 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.                               *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename:	    xxx.asm                                           *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;    Author:                                                          *
;    Company:                                                         *
;                                                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files required:                                                  *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************

	list      p=12F683        ; list directive to define processor
	#include <p12F683.inc>    ; processor specific variable definitions

	errorlevel  -302          ; suppress message 302 from list file

	__CONFIG   _FCMEN_ON & _IESO_OFF & _CP_OFF & _CPD_OFF & _BOD_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 

; '__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
w_temp        EQU     0x70        ; variable used for context saving 
status_temp   EQU     0x71        ; variable used for context saving
TEMP01		EQU	0x20
TEMP02		EQU	0x21
TEMP03		EQU	0x22
COUNTER01	EQU	0x23
FLAGS		EQU	0x24
PULSE1		EQU	0x25
DWELL1		equ	0x26
SHADOW		EQU	0x72
SHADOW2		EQU	0x73
FREQ		EQU	0x74
STATER		EQU	0x75
FLASH		EQU 0x76

;*****	FLASH COUNTER TIME *****
FLASHCNT	EQU	.50

;*****	FIRERATES *****
FIRERATE1	equ	.167		;6 sps
FIRERATE2	equ	.250		;4 sps
FIRERATE3	equ	.125		;8 sps
;to get desired speed divide 1000 by your desired sps then enter
;it here and recompile and reprogram the code


;*****	FLAGS	*****
OFF		EQU		0		;set equ rf off
DBN		EQU		1
DBN2	EQU		2

;*****	GPIO	*****
BUTTON	EQU		4
LED		EQU		2
RCOM	EQU		1

;*****	FLASH	*****
ON1		EQU		0
OFF1	EQU		1
ON2		EQU		2
OFF2	EQU		3
ON3		EQU		4
OFF3	EQU		5
ON4		EQU		6
OFF4	EQU		7

;**********************************************************************
	ORG     0x000             ; processor reset vector
	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
	movwf	status_temp       ; save off contents of STATUS register

	btfsc	FLAGS,OFF			;skip if mode 0
	goto	Offstate
	bcf		STATUS, RP0			;bank 0
	btfss	SHADOW, 1
	goto	DwellRF
	nop
	decfsz	PULSE1, f
	goto	EndInterupt
Clears
	bcf		SHADOW, 1
	movfw	FREQ
	movwf	PULSE1
	goto	EndInterupt2

DwellRF
	decfsz	DWELL1, f
	goto	EndInterupt
	bsf		SHADOW, 1
	movfw	FREQ
	movwf	DWELL1	
	goto	EndInterupt2
	
Offstate
	nop
	nop
	nop
	nop
	goto	Clears

EndInterupt
	nop
	nop
	nop
	nop
EndInterupt2
	nop
	nop
	bcf		PIR1, TMR2IF
	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

;********************subroutines****************************************

Wait500us
	movlw	0x07
	movwf	TEMP01
	movlw	0x12
	movwf	TEMP02
	
Loop01
	decfsz	TEMP01, f			;20usec loop
	goto	Loop01

	movlw	0x07
	movwf	TEMP01
	decfsz	TEMP02, f
	goto	Loop01			;25usec loop
	return					;really 478us

FLASHOFF1
	bcf		GPIO,LED
	decfsz	COUNTER01
	return
	bcf		FLASH,OFF1
	bsf		FLASH,ON1
	return

FLASHON1
	bsf		GPIO,LED
	bcf		FLASH,OFF1
	movlw	FLASHCNT
	movwf	COUNTER01
	return

FLASHOFF2
	bcf		GPIO,LED
	decfsz	COUNTER01
	return
	movlw	FLASHCNT
	movwf	COUNTER01
	bcf		FLASH,OFF2
	bsf		FLASH,ON2
	return

FLASHON2
	bsf		GPIO,LED
	decfsz	COUNTER01
	return
	movlw	FLASHCNT
	movwf	COUNTER01
	bcf		FLASH,ON2
	bsf		FLASH,OFF1
	return

FLASHOFF3
	bcf		GPIO,LED
	decfsz	COUNTER01
	return
	movlw	FLASHCNT
	movwf	COUNTER01
	bcf		FLASH,OFF3
	bsf		FLASH,ON3
	return

FLASHON3
	bsf		GPIO,LED
	decfsz	COUNTER01
	return
	movlw	FLASHCNT
	movwf	COUNTER01
	bcf		FLASH,ON3
	bsf		FLASH,OFF2
	return

Waitup
	btfss	GPIO,BUTTON
	return

	movlw	0x40
	addwf	STATER, f			;cyclic change in state manipulates
								;last 2 bits in STATER.
	btfsc	STATER, 7			;state 1 or 2
	goto	Continue01
	btfsc	STATER, 6
	goto	State2
State1		;OFF
	clrf	FLASH
	bcf		GPIO,LED
	bsf		FLAGS,OFF
	goto	Startover

State2
	movlw	FIRERATE1
	movwf	FREQ
	clrf	FLASH
	bsf		GPIO,LED
	bcf		FLAGS,OFF
	goto	Startover

Continue01
	btfss	STATER, 6		;state 3 or state 4
	goto	State4
State3
	movlw	FIRERATE3
	movwf	FREQ
	clrf	FLASH
	bsf		FLASH, OFF3
	bcf		FLAGS,OFF
	goto	Startover

State4
	movlw	FIRERATE2
	movwf	FREQ
	clrf	FLASH
	bsf		FLASH, OFF2
	bcf		FLAGS,OFF
	goto	Startover

Startover
	bcf		FLAGS,DBN
	return

	

;***********************************************************************
;***********************************************************************
;***********************************************************************

main
	bsf		STATUS, RP0			;bank 1
	movlw	b'111011'			;all ports inputs
	movwf	TRISIO
	movlw	0x02			;GP1 analog
	movwf	ANSEL
	movlw	b'00010000'
	movwf	WPU					;enable week pullups
	bcf		OPTION_REG, 7		;enable all week pullups 
	movlw	0x8F			;Vref on, highest setting
	movwf	VRCON
	bcf		STATUS, RP0			;bank 0
	movlw	0x04
	movwf	CMCON0				;comparator on
	bsf		GPIO, 1			;latch output high
	bcf		GPIO, LED
	
	clrf	FLASH
	clrf	SHADOW
	clrf	SHADOW2
	clrf	FLAGS
	clrf	STATER
	bsf		FLAGS,OFF

	movlw	FLASHCNT
	movwf	COUNTER01	

	movlw	0x01
	movwf	T2CON				;timer 2, prescale 1:4, no postscale
	bsf		STATUS, RP0			;bank 1
	movlw	0x7C				
	movwf	PR2					;setup 500us hardware interupt
	bsf		PIE1, TMR2IE		;enable timer2 interupt
	bcf		STATUS, RP0			;bank 0
	bsf		T2CON, TMR2ON		;turn on timer2
	movlw	0xC0
	movwf	INTCON

Start
	btfss	CMCON0, COUT
	goto	Start			;wait for falling edge of R2

	call	Wait500us		;skip over R2
	movf	SHADOW, w
	movwf	SHADOW2
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop

	bcf		INTCON, GIE
	movlw	0x07
	movwf	CMCON0			;comp off
	bsf		STATUS, RP0			;bank 1
	bcf		ANSEL, 1		;digital
	movf	SHADOW2, w
	xorwf	TRISIO,f		;output
	bcf		STATUS, RP0			;bank 0
	bsf		INTCON, GIE

	call	Wait500us		;wait out R1

	
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop						;maintain 500 usec
	bcf		INTCON, GIE
	bsf		STATUS, RP0			;bank 1
	movf	SHADOW2, w
	xorwf	TRISIO, f		
	bsf		ANSEL, 1		;analog
	bcf		STATUS, RP0			;bank 0
	bsf		INTCON, GIE

	call	Wait500us		;wait out triangle
	
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	call	Wait500us		;wait out circle
	
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	call	Wait500us		;wait out X
	
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	call	Wait500us		;wait out square
	
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	call	Wait500us		;wait some more

	bsf		STATUS, RP0			;bank 1
	bsf		TRISIO, 1			;force input
	bcf		STATUS, RP0			;bank 0

	btfsc	FLASH, ON1			;Handling of LED flashing
	call	FLASHON1

	btfsc	FLASH, OFF1
	call	FLASHOFF1

	btfsc	FLASH, ON2
	call	FLASHON2

	btfsc	FLASH, OFF2
	call	FLASHOFF2

	btfsc	FLASH, ON3
	call	FLASHON3

	btfsc	FLASH, OFF3
	call	FLASHOFF3

	btfss	GPIO,BUTTON		;Button read
	bsf		FLAGS,DBN

	btfsc	FLAGS,DBN
	call	Waitup	

	bsf		GPIO,RCOM		;Relatch RCOM high

	movlw	0x04
	movwf	CMCON0				;comparator on

	goto	Start
	

	



; initialize eeprom locations

	ORG	0x2100
	DE	0x00, 0x01, 0x02, 0x03


	END                       ; directive 'end of program'

