list p=16f84 radix dec #include p16f84.inc __CONFIG _XT_OSC & _WDT_OFF &_PWRTE_ON & _CP_OFF CBLOCK 0x0C breg cth cts tm0s tm0h tp1s tp1h aux1s aux1h aux2s aux2h tchs tchh t18s t18h ntries oldbreg Flags zero aux3h aux4h tchu1h tchd1h tchu2h tchd2h tchu3h tchd3h tchu4h tchd4h tchu5h tchd5h ENDC ;=============================================================================== ;define literal constants ;=========================================================================== ;define bits ;========================================================================== ;define parameters inioff equ 4*0x03 ; choose your dwell ; dwell = dwems/0.256 where dwems is dwell in ms. ; dwell should be integer ; assume dwems = 5 ms then dwell = 19.531 or approx. 20 ; in this software 0.256 ms. (miliseconds) is the smallest time unit ; one turn of 8 bit counter X-tal running at 4 Mhz. dwell equ 20 ; = 5.12 ms ; maxdwell computed automatically maxdwell equ dwell + 0x04 ; = 6.144 ms. , 0x04 = spark duration ; permaxdw computed automatically permaxdw equ (dwell/2 + 2) + 4 ; arbitrary min. dwell + spark ; duration for extra sparks ; computed automatically medlim equ 8*permaxdw ; 0x80 = 32.77 ms ; computed automatically lowlim equ 8*dwell ; 0xA0 = 40.96 ms. maxtim equ 0x06 ; = 6* 65.5 ms approx. 393 ms. ;============================================================================== ; Main entry point org 0x00 goto start ;============================================================================== ; Interrupt handler org 0x04 goto inte0 ; no interrupts ;============================================================================== ;============================================================================ ;main======================================================================== start bcf intcon, gie bsf status, rp0 ;portA always output clrf trisa clrf trisb bsf trisb, 0 movlw 0x88 movwf option_reg bcf status, rp0 clrf zero movlw 0x20 movwf intcon movlw 0x03 movwf ntries newread1 movf breg, w movwf oldbreg movlw 0x01 andwf portb, w movwf breg xorwf oldbreg, w btfss status, z bsf Flags, 7 ; goto change1 btfss intcon, t0if goto progtab bcf intcon, t0if incfsz cth, f goto progtab incf cts, f movlw maxtim subwf cts, w btfss status, c goto progtab decf cth, f ; keep max. cth = 0xff decf cts, f bcf portb, 3 ; cut coil current if engine stops progtab btfss Flags, 7 ; was btfsc goto progtab4 ; poll when to time events bcf Flags, 7 ; compute events till next spark btfss oldbreg, 0 goto shift3 bcf portb, 3 ; fire here! movlw inioff movwf tmr0 movf cth, w movwf tm0h movf cts, w movwf tm0s clrf cth clrf cts goto change2 progtab4 movf ntries, w btfss status, z goto newread1 ; movlw 0x1F andwf Flags, w btfss status, z goto extrasp btfsc portb, 3 ; new goto newread1 ; new movf cts, w btfss status, z goto calth movlw 0x04 subwf cth, w btfss status, c goto newread1 calth movf tchs, w subwf cts, w btfss status, c goto newread1 btfsc status, z goto lower goto setb3 ; bsf portb, 3 lower movf tchh, w subwf cth, w btfsc status, c goto setb3 ; bsf portb, 3 goto newread1 setb3 movlw 0x01 sublw maxtim subwf cts, w btfss status, z goto setb4 movlw 0xFF subwf cth, w btfss status, z setb4 bsf portb, 3 goto newread1 extrasp ; poll when extra sparks start and end btfss Flags, 0 ; goto Flg1 btfsc portb, 3 goto dn1 movf tchu1h, w subwf cth, w btfss status, c goto newread1 bsf portb, 3 goto newread1 dn1 movf tchd1h, w subwf cth, w btfss status, c goto newread1 resfl1 bcf Flags, 0 bcf portb, 3 goto newread1 Flg1 btfss Flags, 1 goto Flg2 btfsc portb, 3 goto dn2 movf tchu2h, w subwf cth, w btfss status, c goto newread1 bsf portb, 3 goto newread1 dn2 movf tchd2h, w subwf cth, w btfss status, c goto newread1 resfl2 bcf Flags, 1 bcf portb, 3 goto newread1 Flg2 btfss Flags, 2 goto Flg3 btfsc portb, 3 goto dn3 movf tchu3h, w subwf cth, w btfss status, c goto newread1 bsf portb, 3 goto newread1 dn3 movf tchd3h, w subwf cth, w btfss status, c goto newread1 resfl3 bcf Flags, 2 bcf portb, 3 goto newread1 Flg3 btfss Flags, 3 goto Flg4 btfsc portb, 3 goto dn4 movf tchu4h, w subwf cth, w btfss status, c goto newread1 bsf portb, 3 goto newread1 dn4 movf tchd4h, w subwf cth, w btfss status, c goto newread1 resfl4 bcf Flags, 3 bcf portb, 3 goto newread1 Flg4 btfss Flags, 4 goto newread1 ; Flg5 btfsc portb, 3 goto dn5 movf tchu5h, w subwf cth, w btfss status, c goto newread1 bsf portb, 3 goto newread1 dn5 movf tchd5h, w subwf cth, w btfss status, c goto newread1 resfl5 bcf Flags, 4 bcf portb, 3 goto newread1 Flg5 change2 movf ntries, w ; keep on computing btfss status, z goto newread1 firstorder movf tm0s, w movwf tp1s movf tm0h, w movwf tp1h clrf Flags ; bits 1 to 5 in Flags flag sparks 1 to 5 movf tp1s, w btfss status, z goto calc1 ; tp1s ne 0 movlw maxdwell ; now 24 = 0x06*4 subwf tp1h, w btfss status, c goto highsp ; cts = 0 movlw medlim ; now 128 = 0x20*4 = 32*4 subwf tp1h, w btfss status, c goto medsp ; cts = 0 calc1 bcf status, c ; here low speed tp1 > 40.96 ms. rrf tp1s, w movwf aux1s rrf tp1h, w movwf aux1h bcf status, c rrf aux1s, f rrf aux1h, f bcf status, c rrf aux1s, f rrf aux1h, f movf tp1s, w btfss status, z goto nolowsp movlw lowlim ; transition from dwell to (tp1)/8 subwf tp1h, w btfss status, c goto lowsp nolowsp movf aux1h, w ; tp1s ne 0 subwf tp1h, w movwf tchh movf aux1s, w btfss status, c incfsz aux1s, w subwf tp1s, w ; aux2 is bsf portb, 3 time movwf tchs ; tch = 7*tp1/8 ; fire = 8*tp1/8 goto calc2 lowsp movlw dwell subwf tp1h, w ; ++++++++++++++ movwf tchh movf tp1s, w movwf tchs calc2 movf aux1s, w movwf t18s movf aux1h, w movwf t18h ; goto newread1 ; if no extraspark wanted then uncomment this line ; tp1h > 8*permaxdw or t18h > permaxdw bsf Flags, 0 ; > permaxdw movlw maxdwell movwf aux4h ; 0x06 movlw permaxdw ; subwf t18h, w movwf aux3h movlw maxdwell subwf aux3h, f btfss status, c goto ne2 ; < 10 1 additi. spark bsf Flags, 1 ; > permaxdw + maxdwell movlw maxdwell addwf aux4h, f ; 0x0C movlw maxdwell subwf aux3h, f btfss status, c goto ne3 ; < 16 2 additi. spark bsf Flags, 2 ; > permaxdw + 2*maxdwell movlw maxdwell addwf aux4h, f ; 0x12 movlw maxdwell subwf aux3h, f btfss status, c goto ne4 ; < 22 3 additi. sparks bsf Flags, 3 ; > permaxdw + 3*maxdwell movlw maxdwell addwf aux4h, f ; 0x18 movlw maxdwell subwf aux3h, f btfss status, c goto ne5 ; < 28 4 additional sparks bsf Flags, 4 ; > permaxdw + 4*maxdwell movlw maxdwell addwf aux4h, f ; 0x1E ; 5 additi. sparks movlw permaxdw addwf aux3h, f movlw maxdwell subwf aux3h, f btfsc status, c goto ch5 movf t18h, w movwf tchd5h goto noch5 ch5 movf aux4h, w movwf tchd5h noch5 movlw dwell subwf aux4h, w movwf tchu5h movlw maxdwell subwf aux4h, f movlw permaxdw sublw maxdwell addwf aux3h, f ne5 btfss aux3h, 7 goto ch4 movlw permaxdw addwf aux3h, f btfsc status, c goto ch4 movf t18h, w movwf tchd4h goto noch4 ch4 movf aux4h, w movwf tchd4h noch4 movlw dwell subwf aux4h, w movwf tchu4h movlw maxdwell subwf aux4h, f movlw permaxdw sublw maxdwell addwf aux3h, f ne4 btfss aux3h, 7 goto ch3 movlw permaxdw addwf aux3h, f btfsc status, c goto ch3 movf t18h, w movwf tchd3h goto noch3 ch3 movf aux4h, w movwf tchd3h noch3 movlw dwell subwf aux4h, w movwf tchu3h movlw maxdwell subwf aux4h, f movlw permaxdw ; movlw 0x02 sublw maxdwell addwf aux3h, f ne3 btfss aux3h, 7 goto ch2 movlw permaxdw addwf aux3h, f btfsc status, c goto ch2 movf t18h, w movwf tchd2h goto noch2 ch2 movf aux4h, w movwf tchd2h noch2 movlw dwell subwf aux4h, w movwf tchu2h movlw maxdwell subwf aux4h, f movlw permaxdw sublw maxdwell addwf aux3h, f ne2 btfss aux3h, 7 goto ch1 movlw permaxdw addwf aux3h, f btfsc status, c goto ch1 movf t18h, w movwf tchd1h goto noch1 ch1 movf aux4h, w movwf tchd1h noch1 movlw dwell subwf aux4h, w movwf tchu1h ne1 goto newread1 highsp clrf tchs movlw dwell sublw maxdwell movwf tchh ; starts at 1.024 ms. after spark goto newread1 medsp clrf tchs movlw dwell subwf tp1h, w movwf tchh goto newread1 ; starts at tp1 - dwell shift3 ; what happens when input signal goes up ? movlw 0x01 sublw maxtim subwf cts, w btfss status, z goto shift31 movlw 0xFF subwf cth, w btfss status, z goto shift31 movlw 0x03 movwf ntries goto newread1 shift31 movf ntries, w btfsc status, z goto shift4 bsf portb, 3 ; the first 3 dwells are not controlled decf ntries, f shift4 goto newread1 inte0 retfie ; interrupt subroutine ;subroutines++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ;============================================================================ ;========================================================================== end