Measuring duty cycle of short duration pulses & long period

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
bilinkl
Posts: 13
Joined: Fri Dec 14, 2012 11:32 am
Location: Perth

Measuring duty cycle of short duration pulses & long period

Post by bilinkl » Mon Feb 06, 2017 2:19 am

Hi All,

I want to measure the Duty Cycle of piezo automotive (in particular diesel) injectors where typical idle on-time is 500us and full load is 1000us. Results will be used in a feedback loop to control other parameters. The clincher is shortest period will be around 20ms or 50hz frequency at maximum revs so the pulses are far between noting only one cylinder's injector needs to be monitored.

With gasoline solenoid injectors this is easy as their on-time is measured in ms so DC math follows the traditional (on-time*100)/off-time.

I can probably rig up some analog stuff to integrate the pulses and then scale the output voltage in A2D measurement but the varying frequency component will have major effects and is totally devoid of digital precision in the acquisition section.

I can't think of any way to measure/calculate DC without first defining what 100% DC is in microseconds - can this be done otherwise in real time? PIC of choice is 18F2550 but can be changed if required.

All suggestions appreciated.

Regards,
Bill

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Measuring duty cycle of short duration pulses & long per

Post by Jerry Messina » Mon Feb 06, 2017 11:43 am

I think you're right... you'll probable want to count in usecs.
You should be able to do that using one of the Capture/Compare/PWM modules (CCP) in Capture mode along with one of the Timers.

Here's what I'd try:
Setup the Timer (ie TMR1) to count usecs. Depending on your system clock frequency that may or may not be easy to do.

The 18F4550 doesn't have a lot of timer prescalers so you just have a few selections (1:1, 1:2, 1:4, and 1:8 I think).
If those don't match up well you can always use another timer (like TMR2) in PWM mode to create a 1MHz 50% duty cycle clock,
and connect the TMR2 output pin to the TMR1 ext clock input. That way you'll have a 1us clock for the timer and you can just set the TMR1 prescaler to 1:1

In Capture mode TMR1 is 16-bits so since you're counting 1us ticks you can have up to a 65536us period (approx 65ms) with 1us resolution.

For info on how to use the CCP Capture mode, check out the following:
http://ww1.microchip.com/downloads/en/D ... 41214a.pdf (tip #4)
http://ww1.microchip.com/downloads/en/D ... /ccpwm.pdf

If you need any help just drop a note here. The timer/CCP settings can be confusing at first if you've never used them.

You should be able to do all that "in real time". What clock freq are you running the 4550 at?

bilinkl
Posts: 13
Joined: Fri Dec 14, 2012 11:32 am
Location: Perth

Re: Measuring duty cycle of short duration pulses & long per

Post by bilinkl » Wed Feb 08, 2017 7:33 am

Hi Jerry, thanks for the information.

That's a cunning trick to achieve a 1Mhz clock... I'm not locked into any particular cpu speed but will probably go faster as the need for more tasks increases - for the moment I need to get the concept operational and then tweak it later.

I'll look into your suggestions beginning with the CCP module and seek further advice if I stumble.

Cheers,
Bill

Post Reply