Quote Originally Posted by Bruce View Post
That'll work too ... if you have a PIC with the TMR1 gate option.
Code:
' PIC12F609 using Timer1 gate input to record pulse widths

@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _IOSCFS_4MHZ & _CP_OFF

CMCON0 = 0         ' comparator disabled
CMCON1 = %00000010 ' TMR1 gate source is T1G pin
GPIO = 0           ' outputs all low at POR
TRISIO = %00010000 ' GPIO.4 = TMR1 gate input

PVAL VAR WORD      ' holds pulse time

TMR1H = 0
TMR1L = 0
T1CON = %11000001 ' TMR1 gate active-high, Fosc/4 clock, TMR1 on

Main: ' apply a pulse < 65536uS to TMR1 gate input GPIO.4 to use
  WHILE !GPIO.4 ' wait for high signal to trigger TMR1 gate pin
  WEND
  WHILE GPIO.4  ' wait for signal to return low (stops TMR1)
  WEND
  PVAL.LowByte = TMR1L  ' get TMR1 counts into PVAL
  PVAL.HighByte = TMR1H ' from TMR1
  TMR1H = 0             ' reset TMR1
  TMR1L = 0
  GOTO Main

  END
For all the NOOBS (like me) don't forget the
Code:
ANSEL=0
to make your T1G pin a digital input. I spent a couple hours scratching my head trying to figure out why I couldn't get this code to run on my 12f683......that was all I needed.

Thank you Bruce for the code snippets...can't wait for the book. I actually got started with micros about 10 years ago with the help of your website. I'm only coming back to working with them in the last few months, and your code is helping me again!