; injection controle program for SDR
;****************************************

	list     p=16F88
	#include "p16F88.inc"
	radix dec

	__CONFIG	_CONFIG1, _WDT_OFF & _XT_OSC  & _LVP_OFF

; PRESET VALUES
;****************************************
#define prescaler	006H
#define max_count	01EH
#define a_input		PORTA,0
#define input		PORTA,4
; PORTB 0-3 is digital switch input
#define output		PORTB,4
#define motor_high	PORTB,5
#define m_output	PORTB,6
#define light		PORTB,7
#define TMR0		1

tmr0h	EQU	020H
timl	EQU	021H
timh	EQU	022H
first	EQU	023H
count	EQU	024H
counts	EQU 025H
adcres	EQU 026H
dadj	EQU 027H
work	EQU	028H

; START SEQUENCE
;****************************************
	org 0x00
	GOTO	INITIALIZE

; INTERRUPT SEQUENCE
;****************************************
	org 0x04
	MOVF	tmr0h,W
	CALL	read_w_array
	MOVWF	TMR0
	MOVLW 	max_count
	SUBWF 	tmr0h,W
	BTFSS 	STATUS,002H
	INCF	tmr0h,F
	BCF		INTCON,002H
	retfie

; INITIALIZE
;****************************************
INITIALIZE:
	BSF		STATUS,RP0

	MOVLW	00FH		;Set Port B0-3 to input
	MOVWF	TRISB		;Set Port B4-7 to output

	MOVLW	011H
	MOVWF	TRISA        ;Set Port A0,4 to input

	MOVLW	prescaler
	MOVWF	OPTION_REG   ;set prescaler

	MOVLW	000H
	MOVWF	PIE1

	MOVLW	001H
	MOVWF	ANSEL		;Set Port A0 to Analog
	MOVLW	000H
	MOVWF	ADCON1		;set Analog port option

	BCF		STATUS,RP0

	MOVLW	041H
	MOVWF	ADCON0
	MOVLW	0DFH
	MOVWF	TMR0
	MOVLW	0A0H         ;INTCON=0xa0; //T0IE=1
	MOVWF	INTCON

	BSF		first,0      ;first pulse flag
	CLRF	tmr0h
	CLRF	PORTB
	BCF		light		;light_on
	BCF		output      ;set output low
; Pump on 3sec
	BSF		motor_high
	BSF		m_output	;motor pulse high
	MOVLW	07H
	MOVWF	count
loop1:
	DECFSZ	count,F
	GOTO	loop1
	BCF		m_output	;motor pulse low	
; Wait about 3sec
	MOVLW	01EH
	MOVWF	work
loop2
	MOVLW	0C8H
	MOVWF	counts
loop3:
	MOVLW	0A6H
	MOVWF	count
loop4:
	DECFSZ	count,F
	GOTO	loop4
	DECFSZ	counts,F
	GOTO	loop3
	DECFSZ	work,F
	GOTO	loop2
	BCF		motor_high
	BSF		m_output	;motor pulse high

; Wait for ADC initialize
	MOVLW	07H
	MOVWF	count
loop5:
	DECFSZ	count,F
	GOTO	loop5
;A/D Convert
	BSF		ADCON0,2	;ADC strat
loop6:
	BTFSC	ADCON0,2	;Wait ADC finish
	GOTO	loop6
	MOVF	ADRESH,W	;read ADC

; MAIN ROUTINE
;****************************************
MAIN:  
	CALL	wait_pulse

	BTFSS	first,0
	GOTO	ADC_SEQUENCE

	CALL	wait_pulse
	BCF		first,0

; READ MAP DATA
;****************************************
ADC_SEQUENCE:
;A/D Convert
	BSF		ADCON0,2	;ADC strat
loop7:
	BTFSC	ADCON0,2	;Wait ADC finish
	GOTO	loop7
	MOVF	ADRESH,W	;read ADC
	MOVWF	adcres
;READ MAP DATA
	ANDLW	0F0H
	MOVWF	counts
	MOVLW	080H
	SUBWF	counts,W ;need -5 x2
	BTFSC	STATUS,0
	GOTO	upper_map
	RLF		counts,W
	ADDWF	timh,W
	BSF		PCLATH,3
	CALL	read_i0_array
	CLRF	PCLATH
	MOVWF	count       ;count=i0[timh]
	MOVF	timh,W
	MOVWF	counts
	MOVLW	01AH		;31-5
	SUBWF	counts,F
	BTFSC	STATUS,0
	GOTO	low_rpm
	BSF		motor_high
	GOTO	adjust
upper_map:
	BCF		STATUS,0
	RLF		counts,W
	ADDWF	timh,W
	BSF		PCLATH,3
	BSF		PCLATH,0
	CALL	read_i1_array
	CLRF	PCLATH
	MOVWF	count       ;count=i1[timh]
	BSF		motor_high
	GOTO	adjust
low_rpm:
	BCF		motor_high
	BCF		STATUS,0
adjust:
	MOVF	PORTB,W
	ANDLW	00FH
	MOVWF	dadj
	INCF	dadj,F

;PULSE OUTPUT
;****************************************
PULSE_OUT:
    BSF		light		;light_off
	BCF		m_output	;motor pulse low
	BSF		output		;set output high	

;Wait 32us x count
loop8:
	MOVLW	009H
	MOVWF	work
loop9:
	DECFSZ	work,F
	GOTO	loop9
	NOP
	DECFSZ	count,F
	GOTO	loop8
;Wait 32us x adjust
loopa:
	MOVLW	009H
	MOVWF	work
loopb:
	DECFSZ	work,F
	GOTO	loopb
	NOP
	DECFSZ	dadj,F
	GOTO	loopa

	BCF		output		;set output low
	BSF		m_output	;motor pulse high
    BCF		light		;light_on

	GOTO	MAIN

; PICKUP PULSE WAIT AND READ TIMER SUBROUTINE
;****************************************
wait_pulse:
waithigh:
    BTFSS	input
	GOTO	waithigh

waitlow:  
	BTFSC	input
	GOTO	waitlow

; READ TIMER DATA
	MOVF	TMR0,W
	MOVWF	timl
	MOVF	tmr0h,W
	MOVWF	timh

; INITIALIZE TIMER
	MOVLW	0DFH
	MOVWF	TMR0
	CLRF	tmr0h

	retlw	0

; READ pulse interval table
;for 500rpm step:
;****************************************
read_w_array:
	addwf  PCL,F
interval0 DT 255,255,255,255,255,255,254,254,254,254,254,254,253,253,253,252
interval1 DT 252,251,250,249,247,246,243,239,234,225,209,178,100, 25, 19,  0

; READ injection_table dadj=a is normal
;****************************************
org 0800H
read_i0_array:
	addwf  PCL,F
;				15000->0rpm
injection00 DT  00,00,00,00,00,00,00,00,00,00,01,02,03,04,08,14
injection01 DT  21,25,26,26,26,26,26,26,25,21,16,14,14,15,17,18 ;  0%
injection10 DT  01,01,01,01,01,01,01,01,01,01,02,03,04,06,09,16
injection11 DT  23,26,26,26,26,26,26,26,26,25,22,21,21,21,22,23 ;  7%
injection20 DT  03,03,03,03,03,03,03,03,03,03,04,05,07,09,13,20
injection21 DT  27,31,32,32,32,32,32,32,32,31,29,28,28,28,28,28 ; 13%
injection30 DT  04,04,04,04,04,04,05,05,05,05,06,08,10,13,18,24
injection31 DT  32,36,37,37,37,37,37,37,37,36,35,34,34,34,34,34 ; 20%
injection40 DT  06,06,06,06,06,06,06,06,06,07,09,11,14,17,22,29
injection41 DT  36,42,44,44,44,44,44,44,43,43,41,40,40,40,40,40 ; 27%
injection50 DT  07,07,07,07,07,07,08,08,08,09,11,14,17,21,27,33
injection51 DT  41,47,50,50,50,50,50,50,49,48,46,45,45,45,45,45 ; 33%
injection60 DT  09,09,09,09,09,09,09,09,09,11,14,16,21,26,32,38
injection61 DT  46,52,56,57,57,57,57,57,56,54,52,51,51,51,51,51 ; 40%
injection70 DT  10,10,10,10,10,10,11,11,11,13,16,19,24,29,36,43
injection71 DT  52,58,62,63,63,63,63,63,61,59,57,57,57,57,57    ; 47%
org 0900H
read_i1_array:
	addwf  PCL,F
injection80 DT  12,12,12,12,12,12,12,12,12,14,19,22,27,32,40,48
injection81 DT  57,64,68,69,69,69,69,69,67,65,63,62,62,62,62,62 ; 53%
injection90 DT  13,13,13,13,13,13,14,14,14,16,20,24,29,35,44,53
injection91 DT  63,70,73,75,75,75,75,75,73,70,69,68,68,68,68,68 ; 60%
injectiona0 DT  15,15,15,15,15,15,15,15,15,18,22,26,32,38,49,59
injectiona1 DT  69,75,78,80,80,80,80,79,78,75,74,73,73,73,73,73 ; 67%
injectionb0 DT  16,16,16,16,16,16,17,17,17,20,24,29,34,41,53,64
injectionb1 DT  74,80,83,84,84,84,84,84,83,81,80,79,79,79,79,79 ; 73%
injectionc0 DT  18,18,18,18,18,18,18,18,19,22,26,30,36,44,56,66
injectionc1 DT  77,83,86,87,87,87,87,87,86,85,84,83,83,83,83,83 ; 80%
injectiond0 DT  19,19,19,19,19,19,20,20,21,23,27,32,37,46,58,69
injectiond1 DT  79,85,89,90,90,90,90,90,90,89,88,87,87,87,87,87 ; 87%
injectione0 DT  21,21,21,21,21,21,21,21,22,24,28,33,38,48,60,72
injectione1 DT  82,87,91,92,93,93,93,93,93,92,92,90,90,90,90,90 ; 93%
injectionf0 DT  22,22,22,22,22,22,22,22,23,25,28,33,39,50,63,74
injectionf1 DT  84,90,93,94,96,96,96,96,96,96,96,94,94,94,94    ;100%

	END
