'================================================ 'Multi Gate Frequency Counter '================================================ ' Uses hardware timer 0 as gate timer ' hardware timer 1 as counter ' Frequency input on pin 15 func main() global gate gate = 1 output 0,1 low 0 high 1 again: countpulses() goto again endfunc '=============================================== 'Go get a reading '=============================================== func countpulses() dim fcount again: pause 10 clearcounter setgate(gate) 'Turn on gate timer and start counter '----------------------------------------- startasm bsf T0CON,7 'Turn on timer0 Gate bsf T1CON,0 'Start counter 'Check timer loop: btfss INTCON,TMR0IF goto loop 'If we make it here then we have count bcf T1CON,0 'Stop counter endasm '------------------------------------------ if PIR1.bit(0)=1 then print "Overflow" gate = gate + 1 goto again endif fcount = TMR1L fcount = TMR1H * 256 + TMR1L if fcount < 6000 and gate > 1 then gate = gate -1 goto again endif displaycount(gate,fcount) endfunc '================================================= 'Display the counter results '================================================= func displaycount(tgate,fcount) dim v10000,v1000,v100,v10,v1 bintodec fcount,v10000,v1000,v100,v10,v1 branch tgate,do1,do1,do2,do3 do1: print tgate,": ",v10000,v1000,",",v100,v10,v1,"Hz" exit 0 do2: print tgate,": ",v10000,v1000,v100,".",v10,v1,"KHz" exit 0 do3: print tgate,": ",v10000,v1000,v100,v10,".",v1,"KHz" exit 0 endfunc '================================================= 'Sets the gate time '0 or 1 is 1 second gate '2 .1 second gate '3 .01 gate '================================================= func setgate(tgate) INTCON.bit(2)=0 'clear overflow flag TMR0IF T0CON.bit(7)=0 'Turn it off T0CON.bit(6)=0 '16 bit mode T0CON.bit(5)=0 'Internal clock T0CON.bit(3)=0 'Assign prescale branch tgate,do1,do1,do2,do3 '1 second gate do1: TMR0H=102 '1 second TMR0L=215 T0CON.bit(2)=1 'Prescale Each Tic = 25.6us T0CON.bit(1)=1 'Prescale T0CON.bit(0)=1 'Prescale exit 0 '.1 seconds gate do2: TMR0H=240 '.1 second TMR0L=175 T0CON.bit(2)=1 'Prescale Each Tic = 25.6us T0CON.bit(1)=1 'Prescale T0CON.bit(0)=1 'Prescale exit 0 '.01 seconds gate do3: TMR0H = 59 '.01 second TMR0L = 244 T0CON.bit(2)=0 'Prescale Each Tic = .2us T0CON.bit(1)=0 'Prescale T0CON.bit(0)=0 'Prescale exit 0 endfunc '================================================= 'This function clears the 16bit hardware counter ' and preps it for next gate '================================================= func clearcounter() T1CON.bit(0)=0 'Turn it off PIR1.bit(0)=0 'clear counter overflow TMR1IF TMR1H=0 TMR1L=0 T1CON.bit(7)= 1 '16 bit operation T1CON.bit(5)=0 'Prescale T1CON.bit(4)=0 'Prescale T1CON.bit(3)=0 'No Oscillator T1CON.bit(2)=1 'Ignored T1CON.bit(1)=1 'External endfunc