PDA

View Full Version : Sampling a PWM signal



picnaut
- 6th May 2005, 20:38
Hi,

I'm trying to measure 2 PWM signals with a frequency of about 100 Hz.
They are output from a dual-axis accelerometer.

I have an idea how to go about it, but I'd be interested to here from you guys (and gals).

Has anybody done this before?

How did you do it?

Do you have any sample code?

Anyway, as always, all feedback is appreciated.

Thanks.

Melanie
- 6th May 2005, 22:08
Take your PWM, thru a Diode (1N4148 for example) so as not to affect the PWM downstream, thru a Resistor (say 10K) to charge a Capacitor (eg 1uF) connected down to 0v. The Capacitor will charge proportionately to the PWM value. 100% PWM will charge the Cap to 100%. 50% PWM will charge the Cap to 50% etc. Connect a PICs ADC across the Cap to sample the charge level and so determine the PWM. Use a Resistor PD if your PWM is greater than 5v. Compensate in software for the voltage drop across the Diode. No fancy software required...just read the ADC... Assuming you had 5v PWM to start with, 0.6v drop across the Diode, then 4.4v will be 100% PWM, 2.2v will be 50% PWM etc etc proportionately. Simple.

picnaut
- 6th May 2005, 23:54
Hi Melanie,

Thanks, that's a cool approach.
I actually got it working with software in 662 bytes.
It was a good exercise.
I'll try your method too.

Thanks.

picnaut
- 7th May 2005, 00:03
Hi Melanie,


Take your PWM, thru a Diode (1N4148 for example) so as not to affect the PWM downstream.

If you use a diode, the capacitor has no where to discharge.
It seems to work just fine when it's directly connected though.

What are your thoughts on this?

Warrier
- 7th May 2005, 00:07
Well Ms. Newman, you need to discharge the cap to get the voltage to follow the PWM duty cycle - else the cap charges up to 4.4volts even at the lowest PRF and will stay there! The diode will make sure of that. So you need a discharge path thru another resistor (say, 10 times Rin or higher depending on the ripple one can tolerate) connected parallel to the 1uF, don't you think?

-warrier

Warrier
- 7th May 2005, 00:24
Hi picnaut:

you can obviously try to device a software exor the two PWM signal if a relative measure is what you need and then use internal timer to integrate the resultant pulse. Or, you can output exored pulse and use Ms. Newman's method of external analog integration and measure the magnitude...

you can also use a pulse of known duration to charge a cap and use the PWM to discharge it and measure the average voltage on the cap. The resultant value on the cap will of course be the inverse of the PWM duty cycle in this case.

-warrier

mister_e
- 7th May 2005, 00:29
The easy way, is to keep this diode there and add a resistor from the cathode to the ground to discharge the capacitor.

Usually what i use, some higher resistor value, lower capacitor value, connect this to a OP-AMP input, OP-AMP output to the PIC. THat's it. There's no big chance to interfer to the PWM signal and the PWM circuit driver charge and discharge the Cap via the same resistor. BUT in some case, using some kind of OPTO-Isolator can also do the job.

OR skipping the kinda 'frequency to voltage conversion' and measure the duty but... depending of you.

This might be useful... well some parts :) (http://www.picbasic.co.uk/forum/showthread.php?t=1297&page=2&highlight=mosfet+volt)

EDIT: Warrier point you to some good idea too. I can't really comment them but those really make sense.

Melanie
- 7th May 2005, 09:20
Thats true. I'm usually sampling values much greater than 5v so my PD Resistors discharge the Cap... if it's 5v or less then of course you wouldn't have it.

Stump
- 12th May 2005, 07:44
If the freq is known and constant, you can use the pulse in command
to measure the pulse width. With very little math you can repackage the result however you like. If the chip you are using has a capture module,
it can run in the background with very high resolution. (I am use this
for a 16 bit A/D converter)
R

Acetronics2
- 12th May 2005, 09:08
Mhhh ...

Let's see it closer : what's the precision of your accelerometer ??? 1% ...2% ?
so, 8 bits are sufficient...
@100 Hz ... a Pulsin 1, ... immediatly followed by a RCtime 0, ... will perfectly do the job. With no ref to the freq.

just take 1 of 2 or 3 pulses to let the PIC calculate or display.

use of the 8 bits timer in free running mode could also be seen ... as reading the timer "in flight" won't give too false a measure.

Alain