OK, here you go.


The interrupt period is 500uSec.

All transitions in .5 seconds are added, since FanClock has a count value
of 1000. 500E-9 X 1000 = .5 seconds.






'------------------ Timer 0 interrupt handler-----------------------------------

Asm
ReadTachs

movlw 0xEC ; Reload TMR0
movwf TMR0H
movlw 0x7E
movwf TMR0L

bcf INTCON,2 ; Clear the TIMER0 interrupt flag

infsnz MasterClock
incf MasterClock + 1 ; 16 bit Master system clock

btfss TPE,0 ; Fan counter active gate
bra CheckFans
bra DoneForNow

CheckFans
infsnz FanClock
incf FanClock + 1 ; 16 bit loop counter

movlw 0x03 - 1 ; 1000 = 0x3E8, but must have one less
cpfsgt FanClock + 1 ; to compare with greater than
bra FanRoutine
movlw 0xE8 - 1 ; Again, subtract one
cpfsgt FanClock
bra FanRoutine
bsf TPE,0 ; Set the bit if completed 1000 loops
clrf FanClock
clrf FanClock + 1
bra DoneForNow


FanRoutine

movf PORTB,0,0 ; Read the port bits all at once
movwf Temp,0 ; Copy port into temp register
xorwf OldPortB,0,0 ; XOR bits to see which ones have changed
movwf changedB,0 ; Bits of changed ports will be set to "1"
movff Temp,OldPortB ; Save the register for next pass

movf PORTC,0,0
movwf Temp,0
xorwf OldPortC,0,0
movwf changedC,0
movff Temp,OldPortC

movf PORTD,0,0
movwf Temp,0
xorwf OldPortD,0,0
movwf changedD,0
movff Temp,OldPortD


Fan1
btfss changedB,0
bra Fan2
infsnz Fan1Counter
incf Fan1Counter+1

Fan2
btfss changedB,1
bra Fan3
infsnz Fan2Counter
incf Fan2Counter+1

Fan3
btfss changedB,2
bra Fan4
infsnz Fan3Counter
incf Fan3Counter+1
Fan4
btfss changedB,3
bra Fan5
infsnz Fan4Counter
incf Fan4Counter+1

Fan5
btfss changedB,4
bra Fan6
infsnz Fan5Counter
incf Fan5Counter+1

Fan6
btfss changedB,5
bra Fan7
infsnz Fan6Counter
incf Fan6Counter+1

Fan7
btfss changedC,0
bra Fan8
infsnz Fan7Counter
incf Fan7Counter+1

Fan8
btfss changedC,1
bra Fan9
infsnz Fan8Counter
incf Fan8Counter+1

Fan9
btfss changedC,5
bra Fan10
infsnz Fan9Counter
incf Fan9Counter+1

Fan10
btfss changedC,4
bra DoneForNow
infsnz Fan10Counter
incf Fan10Counter+1

DoneForNow
INT_RETURN ; Return from the interrupt
ENDASM

;---------------------- End of Timer 0 Int handler -----------------------------