'======================================================= 'Demonstrate a simple Software Frequency Counter ' Uses timer0 as a 1 second timer '======================================================= func main() dim fcount again: fcount = countpulses() print fcount,"Hz" goto again endfunc '====================================================== 'Count the cycles '====================================================== func countpulses() dim fcount clear fcount '---------------------------------------------------- 'First thing setup timer 0 '---------------------------------------------------- INTCON.bit(2)=0 'clear timer0 overflow flag T0CON.bit(7)=0 'Make sure its off TMR0H=103 '1 second (you must always write high byte first TMR0L=150 T0CON.bit(6)=0 '16 bit mode T0CON.bit(5)=0 'Internal clock T0CON.bit(3)=0 'Assign prescale T0CON.bit(2)=1 'Prescale Each Tic = 25.6us T0CON.bit(1)=1 'Prescale T0CON.bit(0)=1 'Prescale T0CON.bit(7)=1 'Start Timer loop1: if INTCON.bit(2) = 1 then exit fcount if IOPORT(15) = 0 then goto loop1 loop2: if INTCON.bit(2) = 1 then exit fcount if IOPORT(15) = 1 then goto loop2 inc fcount goto loop1 endfunc