Hi Alex (Alex Holden), in <199712310951.BAA00577@geocities.com> on Dec 31 you wrote: > Marc Heuler wrote: > > ?!? Setup a timer int at the rate you wish to output PWM. Then sample > the > > PWM-input during this int, too. If it's low, increase the "low counter", > > else increase the "high counter". > > > > The relation of those two counters represent the duty cycle, no matter > what > > the PWM-in-frequency was. > > > > Reset the counters whenever you begin a new PWN-out cycle. > > I don't quite see how that would work. Do you mean: have a timer interrupt > which just simply resets your duty cycle counters, and have the main > routine both checking your counters against the timer and counting how long > the incoming pulses are at the same time? Let me elaborate.. I haven't read the original msg, so I don't know the exact problem. But from the replies it appears to me that there's an input PWM signal from an unknown source, and an output PWM signal that is to be generated in the PIC. The PIC should be able to sync the duty cycle of the output to the input, or change the duty cycle arbitrarily, whatever it thinks is appropriate depending on other unknown events. If that is right, you can measure the input PWM to a quite good accuracy by sampling it in a regular time interval. The sampling frequency should be at least as high as the PWM-in granularity. For ease of programming, one could lock the sampling frequency to the output frequency. Setup an int with 1khz. During each int, sample the input PWM signal. It's either hi or lo. Increase one of two counters, CNT_LO or CNT_HI. After 1 second, examine the CNT_LO and CNT_HI. When the duty cycle was 100%, you'll find CNT_LO=0 and CNT_HI=1000. For 50% you have CNT_LO=500 and CNT_HI=500. For 0% you have CNT_LO=1000 and CNT_HI=0. So the vars show the duty cycle. You just have to be sure to read them a) in lock with the PWM sender, or b) after a long time, so many high/lo cycles have elapsed and the error is small, or c) at the same frequency or multiple of the PWM sender, but phase-shifted. You can also collect several input periods, while outputting the same duty cycle over and over. After 5 or so iterations, you can read the two vars and adjust your output duty cycle accordingly. This will maintain desired PWM frequency but lower time resolution for the sake of simplicity. The same interrupt will be used to generate the output pulses. Remember we're not talking about the best solution, but the simplest one, that could fit a few lines of C code..