'======================================================= 'Demonstrate a simple Software Frequency Counter ' Uses timer0 as a 1 second timer '======================================================= func main() gconst freqin 15 dim bnum as float dim inum as integer again: countpulses() inum = BYTE1 * 256 + BYTE0 bnum = BYTE2 *65535 + inum print bnum goto again endfunc '=============================================== 'Go get a reading '=============================================== func countpulses() 'Clear counters BYTE0=0 BYTE1=0 BYTE2=0 '---------------------------------------------------- '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 '----------------------------------------- 'Inline assembly section '----------------------------------------- startasm bsf T0CON,7 'Start timer0 'Look for 1 loop1: btfsc INTCON,TMR0IF goto alldone if inp.freqin = 0 then goto loop1 endif 'look for 0 loop2: btfsc INTCON,TMR0IF goto alldone if inp.freqin = 1 then goto loop2 endif incf BYTE0,f btfss STATUS,Z goto loop1 incf BYTE1,f btfss STATUS,Z goto loop1 incf BYTE2,f goto loop1 alldone: clrf T0CON 'Stop timer bcf INTCON,TMR0IF 'Clear flag endasm '------------------------------------------ endfunc