On Mon, 31 Mar 1997 18:35:14 +0200 Alastair writes: >Hi All >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. There are a lot of different ways to do this, depending on what else the PIC needs to be doing while it is measuring the pulse. If it doesn't need to be doing anything, just have software look for the start of the pulse then count time until the pulse ends. Since the pulse is only on about 1/16 of the time, there will be a lot of time between pulses to do other processes. The first step toward 'background' measurement would be to have the pulse cause an interrupt (probably RB0) and the ISR would use a software timer to measure each pulse once it starts. This again uses about 1/16 of the processor's time, in a big block. A further refinement would be to have the pulse cause an interrupt again, but the ISR would just save the TMR0 count and reconfigure the interrupt hardware so the end of the pulse would cause another interrupt. On this interrupt, the TMR0 count would be sampled again and the count saved at the start subtracted from it to find the pulse duration. This method uses very little processor time, however the TMR0 timer needs to be free-running at a suitable rate (ideally, it would roll over at intervals a little longer than 1 ms.) If another interrupt is active it could delay the recognition of the pulse interrupts and cause the pulses to be mis-timed. A completely different approach is to establish a pulse-measuring "state machine" which is executed at rapid, regular intervals. If the pulse is present, it increments a counter, if no pulse is present it resets the counter for the next one. If the counter was not 0 when it was reset, this means the pulse just ended so the result should be copied somewhere else as the pulse length result. Unfortunately, to acheive the 7-bit accuracy required, it would need to be executed every 1ms/128 or 7.8 us. This is about 20 PIC instructions with a 10 MHz clock. Though it is conceivable to implement, it would be tricky. Prehaps it's best to keep this technique in mind for some other time when the timing is less demanding. _____________________________________________________________________ You don't need to buy Internet access to use free Internet e-mail. Get completely free e-mail from Juno at http://www.juno.com Or call Juno at (800) 654-JUNO [654-5866]