;****************************************************************************************
;*  
;********************************************************************************	
;*                                                                 			*
;*    Filename:	    MICROCHIP OS                               		                *
;*    Date:         December, 2016                                   			*
;*    File Version: A, this is a new file                                    		*
;*                                                                     			*
;*    Author:       G Michael Drygas                                      		*
;*                  Applications Engineer                    				*
;*    Company:                                  			*
;****************************************************************************************
;*                                			                                *
;*    Files required:   m0-4.asm (macros)                                          		*
;*                                                                     			*
;*                                                                     			*
;*                                                                     			*
;****************************************************************************************
;*                                                                     			*
;*    Notes:        hardware 	dc to ac board							*
;*		         					*
;*                                                                     			*
;****************************************************************************************
	list      p=10F206	; list directive to define processor
	#include  ..\m0-4.asm
	config_
 	
#define	OPTIONinit	b'11001000'	; no wakup, no pullup, Clock 1:1
#define TRISIOinit 	b'00001000'	; 0 output, 1,2 output	
;#define GPIOinit 4	b'00000000'	; GPIO  all LOW 
;****************************************************************************************

	init_			; reasonable initiation of special function registers 


	cblock	_MINRAM			; start of variables
	 SyS1,shortDelay,TCsave:2,TCdiff:2
	endc
#define STACK		_MAXRAM
#define	TMR07Bit	SyS1,7
#define	firstRun	SyS1,6	

#define	block4ms	SyS1,3
	
;PWRUP	
		eq_	SyS1,=,0,lit8
		bsf firstRun
		bsf	block4ms
	    eq_ shortDelay,=,0,lit8
		
TC0used = 2							; use 3 bytes of timer for TC0
	cblock
	 TC0:TC0used
	 NmbrEvent0
	endc	 
START
		EVENT0in	.15000,msec,0xFF  ; set time @ 5000 milliseconds
		eq_			NmbrEvent0,=,.2,lit8	; set execution  for EVENT0
TC1used = 2
	if TC1used > 0
	cblock
	 TC1:TC1used,NmbrEvent1
	endc
		EVENT1in	.4995,msec,.15	;EVENT15 in 1.5 sec, goto MLOOP	 
	endif
JMPTable	andlw	0x0F
		addwf	PCL,F
		 goto	EVENT0	
		 goto	EVENT1
		 goto	EVENT2	
		 goto	EVENT3
		 goto	EVENT4
		 goto	EVENT5
		 goto	EVENT6
		 goto	EVENT7
		 goto	EVENT8
		 goto	EVENT9
		 goto	EVENT10
		 goto	EVENT11
		 goto	EVENT12
		 goto	EVENT13
		 goto	EVENT14
		 goto	EVENT15

;	org	0x100

MLOOP	clrwdt
		if__m	block4ms,not_but,GPIO,3
		   	do_	TCdiff,=,TCsave,-,TC1,byte;	
		   goto	EVENT2	
		end_m
	
	if_bit	TMR0,7			; software emulation of
	 goto	set7			; timer 0 overflow
	if_not	TMR07Bit		;
	 goto	set7+1
		bcf		TMR07Bit
		;	if_not	INTCON,T0IF		; wait for timer TMR0 to time out
		;	 goto	MLOOP
		;	bcf	INTCON,T0IF			
;	goto	PERIODIC8		; which is 256*4 us =1 milliseconds
exitPERIODIC8
	decfsz	TC0,F
	 goto	endMLOOP0
;	goto	PERIODIC2048		; after first TC0 expired, 2048 milliseconds 
;exitPERIODIC2048			; have passed
	if TC0used > 1
;	movlw	0x07			;example for very long periodic events
;	andwf	TC0+1,W
;	movwf	STACK
;	movlw	-7
;	addwf	STACK,W
;	skpnZ
;	 goto	PERIODIC16sec		; every 16 seconds (Event0 must be later!)
;exitPERIODIC16sec
	decfsz	TC0+1,F
	 goto	endMLOOP0
	endif
					; after TC0+1 expired, 8 minutes 44 seconds
	if TC0used > 2			; have passed
	decfsz	TC0+2,F
	 goto	endMLOOP0
	endif 
	movfw	NmbrEvent0		; after TC0+2 expired, 37 hours 17 minutes
	goto	JMPTable 		; now jump to loaded Event
	 
endMLOOP0				; and continue execution here	 
end0MLOOP0
	if TC1used > 0
	decfsz	TC1,F			; analogous as TC0
	 goto	endMLOOP1
	endif
	if TC1used > 1
	decfsz	TC1+1,F
	 goto	endMLOOP1
	endif
	if TC1used > 2
	decfsz	TC1+2,F
	 goto	endMLOOP1
	endif 
	if TC1used > 0
	movfw	NmbrEvent1
	goto	JMPTable 
	endif

	goto	endMLOOP1 
	 
	 
set7	bsf	TMR07Bit	
endMLOOP1	goto	MLOOP

;	org	0x200
;PERIODIC64usec			
;		goto	exitPERIODIC64usec
;
;PERIODIC8							; check fuse tripped for 255 times

;	goto    exitPERIODIC8			; signal gen is running, but waiting to collect
									; more Alarm, else decrease it in 60msec			
;;
;PERIODIC2048	
;		goto	exitPERIODIC2048
;		
;PERIODIC16sec	
;		goto	exitPERIODIC16sec	
;				 

#define fac .1
EVENT0	

#define	hiSide	SyS1,2
flflop
	if_m hiSide,is_on,1
	  bcf	hiSide
	else_m
	  bsf hiSide
	end_m
	
    if_m hiSide,is_on,1
	  do_	GPIO,=,GPIO,OR,b'00000010',lit8
	else_m
	  do_	GPIO,=,GPIO,OR,b'00000100',lit8
	end_m
	goto	MLOOP
								
EVENT1	

EVENT2								; zero crossing
	
	do_		GPIO,=,GPIO,AND,b'11111000',lit8

	clrC
	rrf		TCdiff,F
;	do_		TCdiff,=,TCdiff,/,2,lit8
	EVENT1in	.60,msec,0xFF
	eq_		TCsave,=,TC1,byte

	do_		TC0,=,TCdiff,-,.2,lit8		;half wave-1
	eq_		TC0+1,=,.1,lit8
 	eq_		NmbrEvent0,=,.3,lit8
	delay	.10
	bsf	 block4ms
	goto flflop

EVENT3
	do_		GPIO,=,GPIO,OR,b'11111001',lit8
	EVENT0in	.512,usec,.4
	
EVENT4						
    do_		GPIO,=,GPIO,AND,b'11111110',lit8	;
	bcf	block4ms
    EVENT0in	.60,msec,.14   ;timeout
			 
EVENT5		
	if_m	GPIO,3,is_on,1
	  EVENT0in	.300,msec,.6
	end_m
	EVENT0in	.100,msec,.5
EVENT6	
	if_	GPIO,3,is_off,1
	  goto	EVENT0
	EVENT0in	.100,msec,.6
EVENT7		

EVENT8		

EVENT9		

EVENT10		

EVENT11		


EVENT12	
	

EVENT13
		bcf	block4ms
		do_			GPIO,=,GPIO,OR,b'11111001',lit8
		EVENT0in .250,usec,.14
EVENT14 
	do_			GPIO,=,GPIO,AND,b'11111000',lit8	;

    EVENT0in .60,msec,.14							;come backin 200 msec

EVENT15 
	EVENT1in	.10,msec,0xFF
	eq_			TCdiff,=,TC1,byte
	decfsz	shortDelay,f
	goto	EVENT2
	goto    EVENT14


	
		end