Hi Sylvain, Any signal you connect to a digital input would look like a square signal :) Make sure the thresholds of PIC act as you want. May be add a comparator, e.g. LM393 and set a threshold with a resistor dividor, or use a Schmidt trigger. Measuring pulse period is pretty simple. Connect the signal to RB0/INT. The interrupt will be called on each edge you set in OPTION, either rising or falling. All you have to do is count the pulses during a long period of time, more then 256/1000 Hz, to provide the required precision. When you count the pulses you have N (number of pulses) and T (period of measurement, as set by TMR0). The pulse period Tp = T / N. Note that the measurement period can end anywhere between the pulses (or exactly on the edge) and you can miss up to 2 periods. So the maximum error will be about / N \ 100*|1 - -----| % \ N-2 / Pulse width can be measured with 3 cycles precision with this very nice routine: ;------------------------------------------------------ ;up to 19 bit pulse timer with 3 cycle resolution ;concept by Scott Dattalo, this version by Dwayne Reid ;now measure HI period clrf MH_low clrf MH_mid clrf MH_high ;used as known zero for main loop MH_loop btfss PULSE goto MH_1st movlw 1 btfss PULSE goto MH_2nd addwf MH_low,F btfss PULSE goto MH_3rd rlf MH_high,W ;get C into W (add 0 or 1 to next byte) btfss PULSE goto MH_4th addwf MH_mid,F ;add previous carry btfss PULSE goto MH_5th ;use either line below (not both) btfss MH_mid,5 ;5=16 bits; 6=17 bits; 7=18 bits ; skpc ; skpc=19 bits btfss PULSE goto MH_6th nop ;spare cycle! btfss PULSE goto MH_7th clrwdt btfsc PULSE goto MH_loop MH_8th incf MH_high,F ;MH_high now used to accumulate LSBs MH_7th incf MH_high,F MH_6th incf MH_high,F MH_5th ;use either line below (not both) btfsc MH_mid,5 ;5=16 bits; 6=17 bits; 7=18 bits ; skpnc ;skpnc=19 bits goto overflow ; subwf MH_mid,F ;undo increment, if any incf MH_high,F MH_4th incf MH_high,F MH_3rd decf MH_low,F ;undo increment incf MH_high,F MH_2nd incf MH_high,F MH_1st ;normalize high period movfw MH_high ;get LSB count into w clrf MH_high ; clrc ;make room for lower 3 LSBs rlf MH_low,F ; (shift everything to the left 3 bits) rlf MH_mid,F rlf MH_high,F ; rlf MH_low,F rlf MH_mid,F rlf MH_high,F rlf MH_low,F rlf MH_mid,F rlf MH_high,F ;none of this affects LSB count in w iorwf MH_low,F ;put LSBs into low byte ;------------------------------------------------------ You can alternatively use TMR0 as a time base for pulse width measurement, but that's not going to beat the 3 cycles. To obtain the better precision make the ISR as small as possible and sample BOTH input pin and timer value at the same time in ISR. After pin and timer are sampled then flags can be checked to determine what caused the interrupt. You will need to use RB0/INT pin (or maybe interrupt on change pins RB4-RB7) to generate interrupt on edges. After a rising edge is detected, change the edge selector in OPTION to switch it to falling edge. And similar when a falling edge is detected. There is a gotcha when another interrupt occurs when you already process one. For example you got both flags set - RB0 interrupt and timer overflow interrupt. Assuming RB0 is checked first, the timer overflow may be missed. That leads to big errors. And it doesn't solve the situation if you check timer interrupt first. Take a look at http://www.piclist.com/faq and especially at http://www.piclist.com/techref/default.asp?from=/techref/microchip/&url=timenint.htm for nice ideas of how to use the timer for more then 8 bit counters and for pulse measurement. Hope it helps. Nikolai On Thursday, July 20, 2000 Sylvain Meunier wrote: > Hi, > I would like to measure the pulse width or period of a near square > signal with a 18F84. The constraint are : > * No external clock (other than Pic oscillator) > * More than 8 bits measurement > * No prescaler (I would like to keep it for WDT) > * Input signal is about 1 kHz on any pin of your choice > * Accuracy should be the best possible > * TMR0 is usable > I Imagine to use TMR0 as timer, to watch overflow flag > to increment upper 8bits but I don't see how to fast/accurate > stop counting on input change... > Any ideas are welcome > Thanks > Sylvain > -- > http://www.piclist.com hint: To leave the PICList > mailto:piclist-unsubscribe-request@mitvma.mit.edu -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]: PIC only [EE]: engineering [OT]: off topic [AD]: advertisements