;**********************************************************************
; Alain BIZOT	Source file 	GLDAB_1200 	rel. 09 05 07-31 10 08
; PIC 12F683	Software release	GLDAB_1.2	
; 2009/10/06 By KK  throttle low ->no motor Stop run motor ->stop at magnet detect
;**********************************************************************
; THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION WITH NO WARRANTIES.
; OVIRC AND THE DESIGNER SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR
; SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON
; WHATSOEVER.
;
;**********************************************************************
; Pulse IN GP2 from Rx ch1 motor ctrl -> Pulse OUT GP4
; if Pulse IN -> Pulse OUT (same as IN)
; if no Pulse IN >25mS -> 1mS Pulse OUT every 25mS
; Wing positon detection lock for Gliding
; position detector GP5 (GPIO change IT)
; uses: Timer1 (no IT) + CCP1 (IT) + Timer0 (IT)
; GP0: Red LED (remove strap for programming)
;**********************************************************************
;Starting up -> operational :
;2sec throttle min (LED ON) -> normal operation
;
;Starting up -> theshold setting :
;2sec throttle max (LED blink) -> 2sec trottle min (LED ON)
;then 4sec (LED blink) 
;	before setting threshold level (flapping rate) -> EEPROM
;
;Normal operation, following previous starting procedures :
;Speed control from TX ... until Speed < threshold
;  -->   stop for (2sec-->0.2sec)0.2 sec
;  then  stop if locked position
;  else  speed low threshold (LED ON) until magnet position reached 
;				or timeout 2 sec
;resume normal speed control when speed > threshold
;
;**********************************************************************

	list      p=12F683      ; list directive to define processor
	#include <p12F683.inc>  ; processor specific variable definitions
	#include <My_Math16.asm>	;16b unsigned math macros
	errorlevel  -302        ; suppress message 302 from list file
;configuration bits
	__CONFIG   _FCMEN_ON & _IESO_OFF & _CP_OFF & _CPD_OFF & _BOD_OFF & _MCLRE_ON & _WDT_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 
;***** CONSTANT & VARIABLE DEFINITIONS
w_temp        EQU     0x71    ; variable used for context saving 
status_temp   EQU     0x72    ; variable used for context saving
;ICD2 debugger used: PM 0x700-0x7FF  FR 0x65-06F 0x70,0xF0

Tau1mS_LO	EQU	0x06C	;1mS -> 0.950mS  12-02-07 $$$
Tau1mS_HI	EQU	0x07	;
MS25		EQU	0x3B	;25mS count -> FF-C4=3B init TMR0
;				 25mS = 196 x 256 x 0.5 micro sec
T125mS_LO	EQU	0xC4	;1.25mS
T125mS_HI	EQU	0x09
T175mS_LO	EQU	0xAC	;1.75mS
T175mS_HI	EQU	0x0D
T_sec		EQU	.10	;10 x 20ms = 0.2sec

Delay_1	EQU	.10 	;2 sec --> T0
Delay_2	EQU	.20	;4 sec --> T2 delay for TauTHR setting
Delay_3	EQU	.5	;1 sec --> T3
Delay_4 EQU     .1      ;0.2 sec -->T3 ThrottDown-motorStart=0.2sec
;
;GPIO
Red_LED	EQU	.0	;OUT LED ON, OFF or Blinking
Pulse_IN	EQU	.2	;IN  CCP1 pulse from RX
Pulse_OUT	EQU	.4	;OUT pulse to motor speed variator
Det_Pos	EQU	.5	;IN Hall effect position detector (neg pulse)

;Flag_A
Flag_T0	EQU	.0	;Throttle pos > 1.75mS for t >2 sec --> T1
Flag_T1	EQU	.1	;Throttle pos < 1.25mS for t >2 sec	--> THR set
Flag_T2	EQU	.2	;T1 --> T2 Threshold set at t+4 sec
Flag_T3	EQU	.3	;
Flag_T4	EQU	.4	;Pos_det not looked after 2sec -> TauOUT=1mS
Flag_T5	EQU	.5	;Throttle pos < 1.25mS for t >2 sec -> OPS

;Flag_B
Flag_OPS	EQU	.0	;1: Operational state
Flag_Pos	EQU	.1	;Detector position reached
Flag_Rcd	EQU	.2	;Record Threshold in EEPROM
Flag_Stop	EQU	.3	;Motor stopped: TauOUT=1mS
Flag_Pls	EQU	.4	;Pulse Front=1 Fall=0

	CBLOCK	0x20
Tau125:0,  Tau125_LO,  Tau125_HI	;1.25 mS
Tau175:0,  Tau175_LO,  Tau175_HI	;1.75 mS

Flag_A,    Flag_B				;8 flags
Count_sec, Count_Delay			;Timer counters
_REG_A,    _REG_B	;pseudo 'registers' for the 16b operations

LEADING:0, LEADING_LO, LEADING_HI ;PULSEIO
TauIN:0,   TauIN_LO,   TauIN_HI	;Tau pulse IN
TauOUT:0,  TauOUT_LO,  TauOUT_HI	;Tau pulse OUT
	ENDC
; access 2 banks  0X71 & 0x72 used for context saving

	CBLOCK	0x73	;bank0 equ 0xF1 bank1
TauTHR:0,  TauTHR_LO,  TauTHR_HI	;Tau Threshold -> stop
	ENDC

;**********************************************************************
	ORG     0x000	; processor reset vector
	nop
	goto    GoFirst	; go to beginning of program
	
;*****INTERRUPT SUBROUTINE---------------------(B0)
	ORG     0x004	; interrupt vector location
	movwf	w_temp	; save off current W register contents
	swapf	STATUS,w	; swap does't affect status bits !!!!!
	movwf	status_temp	; save off contents of STATUS register

;isr code
	call	PulseIO	;pulse IN (CCP1) GPIO_2 -> OUT GPIO_4
;end of isr code

IsrEnd:
	swapf	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

;*****BEGINNING OF MAIN PROGRAM----------------(B0) & (B1)!
GoFirst:
	nop
	BANKSEL GPIO	;				(B0)
;Delay 32mS ??? necessary ??? $$$ 22 02
	movlw	0xFF
	movwf	Count_sec
Delt1:
	movlw	0xFF
	movwf	Count_Delay
Delt2:
	decfsz Count_Delay,f
	goto	Delt2
	decfsz Count_sec,f
	goto	Delt1
;End delay

	BANKSEL OSCCON	;				(B1)
	movlw	B'01110001'	;8 Mhz internal oscillator
	movwf	OSCCON

;read Tau Threshold from EEPROM
	movlw	0xF0		;Threshold Low adress in EEPROM
	movwf	EEADR		;address to read		(B1)
	bsf	EECON1,RD	;EE read
	movf	EEDAT,w
	movwf	TauTHR	;(B0 & B1)

	movlw	0xF1		;Threshold High adress in EEPROM
	movwf	EEADR		;address to read		(B1)
	bsf	EECON1,RD	;EE read
	movf	EEDAT,w
	movwf	TauTHR+1	;(B0 & B1)

;Registers init
	BANKSEL GPIO	;(B0)
	clrf	Flag_A
	clrf	Flag_B
	movlw	T_sec		;0.2 second
	movwf	Count_sec
	movlw	Delay_1	;Throttle min or max position for t>2sec
	movwf	Count_Delay
	movlw	T125mS_LO	;1.25 mS
	movwf	Tau125
	movlw	T125mS_HI
	movwf	Tau125+1
	movlw	T175mS_LO	;1.75 mS
	movwf	Tau175
	movlw	T175mS_HI
	movwf	Tau175+1

;GPIO init
	BANKSEL GPIO	;				(B0)
	clrf	GPIO
	movlw	0x07		;GP <2:0> Data IO
	movwf	CMCON0	;no comparator

	BANKSEL ANSEL	;all GP = data IO		(B1)
	clrf	ANSEL		;no analog input

	movlw	B'00101100'	;GP <4,1:0> output=0	(B1)
	movwf	TRISIO	;GP <5,3:2> input=1

;Timer0 init (continuous run...)
	movlw	B'11010111'	;Fosc/4_clk=0 PS_T0=0 PS_256=111
	movwf	OPTION_REG	;				(B1)
	BANKSEL GPIO	;				(B0)
;Init Timer0
	clrf	TMR0		; 32 ms count with PS=256
	bcf	INTCON,T0IF	;clr IT flag
;

;Init outputs
	bcf	GPIO,Pulse_OUT ;Pulse OUT=0
	bsf	GPIO,Red_LED   ;LED=OFF

;init IT all + timer0
;	movlw	B'11100000'	;GIE=1  PEIE=1	T0IE=1
;	movwf	INTCON
	bsf	INTCON,GIE	;for GP5 change (pos detector)
	bsf	INTCON,PEIE	;for IT CCP
	bsf	INTCON,T0IE	;authorise IT timer0
;
;init CCP/IT, timer1, IOC5
	call	Init_PulseIO

;MAIN LOOP ************************************************************
Main:
	nop			; Main DO NOTHING !
	clrwdt		; clear watchdog timer !  $$$ 25 02 $$$
	goto	Main

;*****included source files (other files)
	#include <GLD_Pulse_1200.asm> ;IN GPIO 2 -> OUT GPIO 4

;**********************************************************************
	END				; directive 'end of program'

