Run this in the MPLAB sim. Have the stimulus window open, click animate, and when it stops on "IF (capture = 0) Then loop" ' Wait here until captured click the stimulus "fire button" to toggle RC2.
If you're running this with animate, then go have yourself a cup of coffee because it's going to take a while before the EEPROM write happens.Code:DEFINE LOADER_USED 1 ' Optional, allows use of melabs Loader DEFINE osc 20 capture VAR PIR1.2 ' CCP1 capture flag overflow VAR PIR1.0 ' Timer1 overflow flag A VAR WORD ' Word variable that stores the value I VAR BYTE period var WORD T1CON=%00000000 ' Timer1 reset TMR1L=0 ' Reset Timer TMR1H=0 ' Remember Timer is OFF TRISC.2 = 1 CCP1CON=%00000000 CCPR1L=0 CCPR1H=0 pir1.2=0 ' Reset Capture Flag pir1.0=0 ' TMR1 register did not overflow CCP1CON = %00000101 ' Enable the CCP1 capture, RISING edge PIE1=%00101111 ' Enables:USART-SSP-CCP1-TMR2 to PR2 match - ' TMR1overflow interrupt '********************* MAIN ************************* loop: IF (capture = 0) Then loop ' Wait here until captured T1CON = %00000001 ' TMR1 prescale=1, and turn it on (1uS per count) CCP1CON = %00000100 ' Enable the CCP1 capture, FALLING edge pir1.2=0 PERIOD.lowBYTE = CCPR1L ' Store the captured value in PERIOD.highBYTE = CCPR1H ' period variable capture = 0 ' Clear the capture flag write 0,PERIOD.lowbyte goto Loop
If you use the run button, then click the stimulus fire button a few times while it's running, then click stop after a few seconds. In the EEPROM window, you'll see location 00 holding the captured value.




Bookmarks