Alastair wrote: > I need to measure the width of a pulse which occurs at about 30Hz > with a 16c(f)84, the pulse width is variable between 1ms and 2ms > and I need as high a resolution as possible(about 7bit should do) > on the 1ms difference between the two extremes. As usual the > routine needs to use as little as possible processor time. Any > code will be most helpful. Robert: Here's a copy of the message I posted the last time someone wanted to measure RC servo pulse-widths... I hope it helps. ------- Forwarded Message Follows ------- From: Self To: PICLIST@MITVMA.MIT.EDU Subject: Re: Measuring pulse lenght Date: Mon, 23 Jun 1997 18:36:53 -0800 Quentin wrote: > can anybody help me with the following for a 16C54: I want to put a > pic in between a RC (Radio control) receiver and the servos. The pic > must do calculations on these signals (mixing etc.) and send them to > the outputs.. My question is about measuring the pulse lengths and > generating them on an output. RC pulses are 1 to 2 ms long, repeated > every 20 ms. The length the pulse determine the position of the > servo. I have tried working out a general code for measuring the > pulse, but I am not happy with getting an accurate reading and I > think the code will be to long. Anybody who has done this before, > can you please help. Quentin: I haven't done it before (one of the OTHER Andys is the resident RC servo expert), but I'd probably do something like this. It requires 17 words of code space and assumes a 4 MHz clock: ; Masure the width of a pulse on RCIN. ; ; If the pulse is > 2 ms, return with w = 1. ; If the pulse is < 1 ms, return with w = 2. ; If the pulse is between 1 and 2 ms, return with w = 0 and ; TIME = (pulse width in microseconds - 1000)/4... That is: ; ; Pulse width in microseconds: TIME: ; ---------------------------- ----- ; 1000 0 ; 1100 25 ; 1200 50 ; 1300 75 ; .... ... ; 1900 225 ; 2000 250 ; ; NOTE: THIS ROUTINE ASSUMES A 4 MHZ CLOCK! #DEFINE RCIN PORTA.0 ;or whatever. TIME EQU [any general-purpose reg] .... MEASURE_A_PULSE: MOVLW (256-250) ;Setup to watch the pulse for 1 ms. MOVWF TIME ; WAIT4GAP: BTFSC RCIN ;Wait for a high-going edge. GOTO WAIT4GAP ; ; WAIT4PULSE: ; ; BTFSS RCIN ; GOTO WAIT4PULSE ; ; We've seen a high-going edge. Make sure the pulse lasts for at ; least 1 millisecond. WAIT_1MS: BTFSS RCIN ;If RCIN is still hi, skip ahead. GOTO ERROR_SHORT ;Otherwise, the pulse is too short, ;so go exit with w = 2. INCFSZ TIME ;Have we been watching for 1 ms? GOTO WAIT_1MS ;If not, loop back and keep waiting. ; RCIN has been high for 1 millisecond. TIME = 0 at this point; ; keep watching RCIN, incrementing TIME every 4 microseconds ; until either RCIN goes low or TIME = 256. TIME_THE_REST: BTFSS RCIN ;If RCIN is still hi, skip ahead. GOTO GOT_TIME ;Otherwise, RCIN has gone low after ;1 ms but before 2 ms, so go exit ;with w = 0. INCFSZ TIME ;Has RCIN been hi for > 2 ms total? GOTO TIME_THE_REST ;If not, loop back and keep timing. ; The pulse is > 2 ms wide [well, actually, it's greater than ; 2.016 ms, but who's counting?]. Exit with w = 1. ERROR_LONG: RETLW 1 ;The pulse is longer than 2 ;milliseconds, so return with w = 1. ; The pilse is < 1 ms wide, so exit with w = 2. ERROR_SHORT: RETLW 2 ;The pulse is shorter than 1 ;millisecond, so return with w = 2. ; The pulse is between 1 ms and 2 ms wide. Exit with w = 0 and ; TIME = (pulse width in microseconds - 1000)/4. GOT_TIME: RETLW 0 ;The pulse is between 1 and 2 ;milliseconds wide. Return with ;w = 0 and pulse width in TIME. ------- End of Forwarded Message ------ -Andy === Andrew Warren - fastfwd@ix.netcom.com === Fast Forward Engineering - Vista, California === http://www.geocities.com/SiliconValley/2499