;***********************************************************************
;*   This file is the firmware to implement a pause width modulated    *
;*   signal of 1..5 microseconds(depending on supply voltage 4.8-3V)   *
;*   at GPIO 2 Transistor output driver                                *
;*    File Version: A.2                                                *
;*                                                                     *
;*    Author:       G M DRYGAS                                          *
;*                  Principal Applications Engineer                    *
;*    Company:      MICHAELS Prototype                                 *
;*                                                                     *
;***********************************************************************
;*                                                                     *
;*    Files required:   macros3b                                    *
;*                                                                     *
;*                                                                     *
;*                                                                     *
;***********************************************************************
;
	list      p=12F683	; list directive to define processor
	#include  ..\macros3b.asm
	config683
;***************************************************************************************
;VARIABLE DEFINITIONS in General Purpose Registers  
;***************************************************************************************

	IFDEF __10F222
	cblock 0x09				; var starting at  0x09

	endc
	ENDIF

	cblock 0x20
	 SystemStatus,TimerVector
	 PR2,TimerTick,TimeCounter:2,AsyncCounter:2

	endc

#define	TMR07Bit	SystemStatus,7
#define STACK	0x2F

#define GPIOinit	b'00000000'	; all io initiate as LOW
#define TRISIOinit	b'11111011'	; configure NFET_Gate_Port as output 
#define	OPTIONinit	b'11000000'	; no wakup, no pullup, Clock 1:2
;#define	ADCON0init	b'01000001'	; gP0 analog, chanel 00,enable
#define	ADCON0init	b'10000101'	; gP1 analog, chanel 01,enable


	init683
	goto	MAIN

#define Lo_Intensity	.240
#define Hi_Intensity	.16
#define	NFET_Gate_Port	GPIO,2


afterwdt 
	settrap206B .500,msec,ADC,TimeCounter,TimerVector
MAIN	eq_	PR2,=, Lo_Intensity,lit8	;start with intensity
	
cycle	incf	PR2,W 			; 5 .. 255=off
	skpnz
	 goto	ploop

	bsf	NFET_Gate_Port		; NFET or Transistor on
	movfw	PR2
	addwf	TMR0,W			; 4 us
	movwf	TimerTick
	bcf	NFET_Gate_Port		; and off

ploop	if_bit	TMR0,7
	 goto	set7
	if_not	TMR07Bit
	 goto	clred
	bcf	TMR07Bit
	decfsz	TimeCounter,F
	 goto	clred
	decfsz	TimeCounter+1,F
	 goto    clred
	movfw	TimerVector
	movwf	PCL

set7	bsf	TMR07Bit
clred
	movfw	TimerTick
	subwf	TMR0,W
	andlw	B'11110000'
	skpZ
	 goto	ploop
	goto cycle



async	decfsz	AsyncCounter,F
	 goto	ploop
	decfsz	AsyncCounter+1,F
	 goto	ploop			; 7msec to 158ms check input

	goto 	cycle

ADC	bsf	ADCON0,GO
	btfsc	ADCON0,GO
	 goto	$-1

		eq	x,.0
		incfsz  x,F
		 goto linearze
		goto $		; loop
	
vtable	addwf	PCL,F
	dt	2* (1+1)-1
	dt .32,	Hi_Intensity
	dt .240,Lo_Intensity

#define MdB	STACK-0
#define LoB	STACK-1
#define HiB	STACK-2

#define MdD	STACK-3
#define LoD	STACK-4
#define HiD	STACK-5
  
#define	AL	ADRES

 
linearze		eq_	MdB,=,.1,	lit8
		call	vtable
		movwf	LoB
		if_	AL,>,LoB,	byte
		 goto	bases
exbdry		do_	MdB,=,MdB,+,1,	lit8
		movfw	MdB
		call	vtable
		movwf	MdD
		goto done

bases		movlw	0
		call	vtable
		movwf	MdB
		call	vtable
		movwf	HiB
		if_	AL,>=,HiB,	byte
		 goto exbdry

basesearch	do_	MdB,=,MdB,-,(1+1),lit8 
		movfw	MdB
		call	vtable
		movwf	LoB
		if_	AL,>=,LoB,	byte
		 goto 	binsearch
		eq_	HiB,=,LoB,	byte
		goto	basesearch

binsearch	do_	MdB,=,MdB,+,1,	lit8
		movfw	MdB
		call	vtable
		movwf	LoD
		do_	MdB,=,MdB,+,(1+1),	lit8
		movfw	MdB
		call	vtable
		movwf	HiD

binloop		do_	MdB,=,LoB,+,HiB,	(byte)
		rrf	MdB,F
		do_	MdD,=,LoD,+,HiD,	(byte)
		rrf	MdD,F
		
		if_	AL,>,MdB,		(byte)
		 goto	higher

		skpNZ				; AL equal MdB
		 goto	done

		eq_	HiB,=,MdB,	byte
		eq_	HiD,=, MdD,	byte
		goto	binloop
higher		eq_	LoB,=,MdB,	byte
		eq_	LoD,=,MdD,	byte
		goto	binloop

done		e_q	E1,[,x,byte,]=,MdD,byte		

eq_	PR2,=,MdD,	byte
		goto	cycle
	end