Log in

View Full Version : Measuring multiple PWMs in the background



jswayze
- 20th March 2005, 03:21
Hello all,

I've run into a hardware challenge and could use some help from those more experienced than myself (so that includes about everyone :) ) It's a fairly complex project, so I'll try to distill it down to the part that's relevant to my question - bear with me though as I try to describe it.

My system has 8 outputs, each tied to an output pin on a 16F628. I'm currently using the internal RC oscillator at 4MHz to limit part count, but will use a 20MHz resonator if necessary. The output is pulses at variable rates and durations, all determined by constants in my PICBasic program.

I use an interrupt to increment a counter which is then referenced by the loop that controls each output port. For example, the loop is constantly running and checking the counter. If the counter reaches the "start time" value then an output will go high. When the counter reaches "start time"+"duration", the output goes low, then the counter for that output port is reset.

I have two input pins, each connected to a PWM source (an R/C receiver channel). Therefore each input can see pulses of between 1 and 2ms with about a 20ms period. The measured pulse widths can modify some of the timing values within the main routine.

Now the tricky part. Somehow I need to simultaneously check two PWM inputs AND reliably increment my clock variable at a pretty fast rate. The problem I see is that, since the pulse period is 20ms, I'll have to sit and wait up to 2*20= 40ms while PULSIN tries to find the pulse widths.

This would be all fine and good if I could increment my timer variable in steps of 40ms or more, but I'd like to be able to use a much finer resolution - like 5ms per increment.

Ideally I would like to see the PWM routines run in the background and, like the interrupt, simply fill a variable with the current pulse width. That way the main program loop only has to reference 3 variables: PWM1, PWM2, and timer.

Is there any technique to do what I want here? Am I misinterpreting the time that PULSIN takes to do its job? Is this actually more of a "multitasking" question?

As usual, *any* thoughts would be ever so helpful.

Thank you in advance,

Jeff

jswayze
- 20th March 2005, 04:36
OK, as usual, the act of thinking about and typing my question has brought to light an idea.

Since I'm already running an interrupt, how about doing away all together with the PULSIN command and doing the equivalent checking/testing commands within the interrupt? It all boils down to how frequently I check, but at each interrupt I could get the value of the input port and compare it to its previous value. If I found a low->high transition I could zero a counter variable, then when it went from high->low I could read the counter and determine the width. As I said before, the resolution of my measurement would depend on how frequently I checked, but for my application variance within 5% or so would be acceptable.

It seems like this would be a little "too" easy... am I missing something? Would there be a reason to take multiple samples of the incoming signal?

I'm going to give this method a shot, but would still appreciate any input. Something like this has to have been done before...

Thanks,

Jeff