' Name : CCPX2.pbp ' Compiler : PICBASIC PRO Compiler 2.6 ' Assembler : PM or MPASM ' Target PIC : PIC16F877 ' Hardware : LAB-X2 Experimenter Board ' Oscillator : 4MHz external crystal ' Keywords : CAPTURE, SEROUT, TIMER1 ' Description : PICBASIC PRO program to demonstrate the use of the ' hardware capture module to measure the period of a frequency input. ' Written for the PIC16F877 and LAB-X2 Experimenter Board. ' Input signal should be connected to RC2/CCP1. ' Define LOADER_USED 1 ' Optional, allows use of boot-loader Include "modedefs.bas" ' Modes for SerOut So VAR PORTC.6 ' Serial output pin capture VAR PIR1.2 ' CCP1 capture flag overflow VAR PIR1.0 ' Timer1 overflow flag period VAR WORD ' Word variable that stores the value CCP1CON = %00000100 ' Enable the CCP1 capture, falling edge T1CON = %00000001 ' TMR1 prescale=1, and turn it on (1uS per count) mainloop: IF (capture = 0) Then mainloop ' Wait here until captured period.lowbyte = CCPR1L ' Store the captured value in period.highbyte = CCPR1H ' period variable IF overflow = 0 Then ' Skip the output if the timer overflowed SerOut So, T2400, [ "Input Period: ",#period, "uS", 10,13] ' Output EndIF capture = 0 ' Clear the capture flag reset: IF (capture = 0) Then reset ' Wait for beginning of next period TMR1L = 0 ' Clear Timer1 low register TMR1H = 0 ' Clear Timer1 high register capture = 0 ' Clear capture flag overflow = 0 ' Clear overflow flag GoTo mainloop ' Do it forever End