PDA

View Full Version : How can I measure Duty cycle ?



MaxiBoost
- 17th December 2006, 18:17
Hello friends ,

I`m trying measure my cars injectors duty cycle .
Have any body experince about this ?

For example ,
injectors running pwm cycle ,
if injectors full open ,I must see %100 duty cycle on the display.

Im trying measure injectors cycle (ms) than calculate duty ,
but this is not correct way I think .

This kit ,
http://www.jaycar.com.au/productView.asp?ID=KC5375&CATID=25&keywords=&SPECIAL=&form=CAT&ProdCodeOnly=&Keyword1=&Keyword2=&pageNumber=&priceMin=&priceMax=&SUBCATID=347

working great ,They are use a 16F84 pic only .

thanks

skimask
- 18th December 2006, 03:38
Hello friends ,

I`m trying measure my cars injectors duty cycle .
Have any body experince about this ?

For example ,
injectors running pwm cycle ,
if injectors full open ,I must see %100 duty cycle on the display.

Im trying measure injectors cycle (ms) than calculate duty ,
but this is not correct way I think .

This kit ,
http://www.jaycar.com.au/productView.asp?ID=KC5375&CATID=25&keywords=&SPECIAL=&form=CAT&ProdCodeOnly=&Keyword1=&Keyword2=&pageNumber=&priceMin=&priceMax=&SUBCATID=347

working great ,They are use a 16F84 pic only .

thanks


PBP...
Use the Pulsin twice; once to measure the pulse width when low, then right after that, measure the pulse width when high. Add the two results together. Multiply the total result by 100 and divide that total by the pulse-high result and you'll get a percentage relating to a duty cycle. This will get you close, but not absolutely perfect. The total pulse width could change a lot between successive readings (i.e. during fast acceleration or deceleration) and completely ruin the end result. Maybe a little bit of software filtering or averaging over a couple of cycles added to the software.
JDG

sayzer
- 18th December 2006, 03:50
What about "Capture" ?


----------------------------------

mister_e
- 18th December 2006, 04:03
Yup that's the much accurate way if the PIC have the CCP module. Case not, Pulsin may work... but accuracy is pretty poor.

So you can still detect the rising edge, start a timer, wait 'till falling edge. You have now the High Pulse measure. From there you reset the Timer and wait the next rising edge. There you have the Low pulse measure.

Few maths later, you have the duty cycle.
http://www.picbasic.co.uk/forum/showpost.php?p=16636&postcount=4

also look at this one
http://www.picbasic.co.uk/forum/showthread.php?t=4299&highlight=pulse+measurement

HTH

MaxiBoost
- 23rd December 2006, 18:27
Yup that's the much accurate way if the PIC have the CCP module. Case not, Pulsin may work... but accuracy is pretty poor.

So you can still detect the rising edge, start a timer, wait 'till falling edge. You have now the High Pulse measure. From there you reset the Timer and wait the next rising edge. There you have the Low pulse measure.

Few maths later, you have the duty cycle.
http://www.picbasic.co.uk/forum/showpost.php?p=16636&postcount=4

also look at this one
http://www.picbasic.co.uk/forum/showthread.php?t=4299&highlight=pulse+measurement

HTH


Hi again and thank your answers ,

Dear mister_e ,

I give to a circuit example in my first message (jaycar kit) use a 16F84A ,
This kit is working good and without any problem .
What is your comment ? or have you another idea ?
I think ,they must use very simple programme !
16F84 not have ccp module .

If you can help me ,thank you
if you can not ,thank you again :)

regards

skimask
- 23rd December 2006, 22:12
Hi again and thank your answers ,
16F84 not have ccp module .



which is why I said to use the pulsin commands in the earlier post.

MaxiBoost
- 25th December 2006, 08:35
which is why I said to use the pulsin commands in the earlier post.

I can try but fail ,
Maybe Im not expert ,I dont .

ardhuru
- 25th December 2006, 11:28
which is why I said to use the pulsin commands in the earlier post.

I'm afraid this does not work; after the first pulsin has taken its reading, since the transition has *already* happenned, the next pulsin of the opposite polarity (level?) will take place only at the next-to-next pulse. Of course, if the duty cycle for a sequence is going to stay somewhat the same then this should make no difference. I was trying to decode an IR signal by using these consecutive pulsins, and got completelty wrong results.

sougata
- 25th December 2006, 15:05
Hi,

I am not sure about the fuel injection stuff. Possibly a variable duty and variable frequency. One option may be to use the portb on change interrupt feature. So for every falling or rising edge you get an interrupt. One problem is that depending on the timer prescaler you may run out of time if the frequency is too low causing a timer overflow. Again that can be accounted with ISR checking for timer overflow and incrementing a pseudo timer high byte counter making the timer a 16bit one.

Acetronics2
- 25th December 2006, 17:39
Hi,Sougata,

you're right ...

But the big problem is injectors are nor open 100% of the cycle !!!

a "100%" aperture equals, say, only 20 or 30 degrees at crankshaft ....

and this value has to be entered somewhere to measure something useful.

now, a simple "PULSIN" is enough to measure the ON duration in µs ... but maximal aperture ( vs. rotation speed ? ) has to be known to calculate a displayable percentage.

add to that needles inertia is not negligible at those timings ... and you'll see your measure is somewhat false !!!

Some data is missing here ...

Alain

skimask
- 26th December 2006, 15:54
I can try but fail ,
Maybe Im not expert ,I dont .

Ok, just to prove that the pulsin idea does work.........(within certain parameters anyways, I know it takes a finite amount of time for an injector to open and close and this amount would have to be accounted for in final calculations, and I also know that fuel rail pressure, system voltage, and injector/fuel temperature also have an effect on open/close times. I never said this method would be 100% accurate, but I'm sure the results can be tweaked thru software to get something fairly accurate.)

Do you have working hardware? In other words, do you have an input wire that could be or is connected directly to a PIC input pin? And if you connected an o'scope to that pin/wire, would the resultant signal on the 'scope follow/match the signal on the wire to the injector?

Without working hardware, further discussion would be fruitless...
So if you're just talking in theory, an idea you have...say so now so I (others?) don't use up too much time on this.

MaxiBoost
- 27th December 2006, 19:25
@skimask you are right ,
I work on proteus before ,

Now Im just build circuit on breadboard.
And Im take some shoot.

This is "duty cycle meter" working circuit ,I say before ..
Its working good on bench and my car ..
http://img393.imageshack.us/img393/4281/duty1zo7.jpg


This is duty cycle shema
http://img218.imageshack.us/img218/6923/dsc01492resizemb5.jpg

MaxiBoost
- 27th December 2006, 19:26
PWM generator ,this simple circuit come with duty cycle meter .
Its use for test.
http://img460.imageshack.us/img460/8074/pwm1ql7.jpg


PWM generator shema ,simple 555 pwm generator .
http://img242.imageshack.us/img242/9986/pwm2qu6.jpg


Starting circuit on breadboard,
16F84A ,10mhz osc
http://img393.imageshack.us/img393/5743/circuit1sq5.jpg

MaxiBoost
- 27th December 2006, 19:27
Here simple "pulsin" code ,
yes its not finish .Only run for test .

'DEVICE 16F84A
DEFINE OSC 10

DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 3
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 2
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2

TEST1 VAR WORD

PULSIN PORTA.0,1,TEST1
Lcdout $fe, 1,"Puls :" ,#TEST1
PAUSE 200


lcd picture
http://img393.imageshack.us/img393/8717/lcd1bk8.jpg

This is proteus circuit,
http://img165.imageshack.us/img165/8307/proteus1sn3.jpg

MaxiBoost
- 27th December 2006, 20:16
whats wrong ?


'DEVICE 16F84A
DEFINE OSC 10

DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 3
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 2
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2

TEST1 VAR WORD 'PULS_LOW
TEST2 VAR WORD 'PULS_HIGH
TEST3 VAR WORD 'ADD TWO RESULT (PULS_LOW+PULS_HIGH)
TEST4 VAR WORD 'MULTIPLY 100 (TEST3 * 100)
TEST5 VAR WORD 'DIVIDE PULS_HIGH

PULSIN PORTA.0,0,TEST1
PULSIN PORTA.0,1,TEST2
TEST3=TEST1+TEST2
TEST4=TEST3 * 100 'WHATS WRONG ??
TEST5=TEST4 /TEST2

LCDOUT $FE,$80,"1:",#TEST1 'puls_low
LCDOUT $FE,$88,"2:",#TEST2 'puls_high
LCDOUT $FE,$C0,"3:",#TEST3 'puls_low + puls_high
LCDOUT $FE,$C8,"4:",#TEST4 '
'LCDOUT $FE,1,"5:",#TEST5

PAUSE 200


http://img117.imageshack.us/img117/1844/proteus2bb5.jpg

skimask
- 27th December 2006, 22:52
What's wrong? What's wrong is a little thing that trips me up once in awhile...
Overflow...
Take a number that's larger than 8 bits, multiply it by another number larger than 8 bits, and the result won't fit in 16 bits! It'll look like it works, but if you take (for example), 12000 (fits in 14 bits), multiplied by 100 (fits in 7 bits), the result is 1,200,000 (fits in 21 bits). The result doesn't fit in a 16 bit word, and instead of 1,200,000, all you see is 20,352...because the extra 5 bits got lopped off by the math routines.

But first, are the pulsin (test1 and test2) numbers correct from the circuit? If they are good numbers, then the rest should work ok...

And your math was a bit backwards...you had the total pulsewidth divided by the pulsehi. That math doesn't give percentage. You've got to divide the part by the whole to get a percentage (44/100, 44%, 44parts divided by 100 whole, etc. and so on, easy to get tripped up by that too).

Give this a try and see what happens:


'same defines as you had before
PLO VAR WORD 'PULS_LOW
PHI VAR WORD 'PULS_HIGH
PWIDTH VAR WORD 'ADD TWO RESULT (PULS_LOW+PULS_HIGH)
DTYCYC VAR WORD 'DIVIDE PULS_HIGH

PULSIN PORTA.0 , 0 , PLO : PULSIN PORTA.0 , 1 , PHI

cutdown2:
PWIDTH = PLO + PHI
if PWIDTH > 655 then 'divide by 2 until 'TEST4' result 3 lines below won't overflow
PLO = PLO / 2 : PHI = PHI / 2 : goto cutdown2
endif

DTYCYC = ( PHI * 100 ) / PWIDTH
LCDOUT $FE , $80 , "PLO:" , DEC5 PLO 'puls_low
LCDOUT $FE , $88 , "PHI:" , DEC5 PHI 'puls_high
LCDOUT $FE , $C0 , "PW:" , DEC5 PWIDTH
LCDOUT $FE , $C8 , "DC:" , DEC5 DTYCYC



Of course, with all this dividing, you will lose some resolution (i.e. you'll see 10%, but the real result might be 9, 11, 10.1, whatever, don't know how far off it'll be).
But, read the PBP manual and there is some 16/32 bit math handling functions in there that'll help you get better resolution of the final Duty Cycle measurement.

MaxiBoost
- 28th December 2006, 08:50
@Skimask ,
yess its work ,thank your interesting .
Its not bad ,so close result with duty cycle meter.
I can use it on my car .

Some problem I have ,

1-why I see on the lcd "35" %100 duty cycle ?

2-People use RB0 input "schmit trigger" on duty cycle meter .
How can use schmit trigger input my circuit ?


'DEVICE 16F84A
DEFINE OSC 10

DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 3
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 2
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2

PLO VAR WORD 'PULS_LOW
PHI VAR WORD 'PULS_HIGH
PWIDTH VAR WORD 'ADD TWO RESULT (PULS_LOW+PULS_HIGH)
DTYCYC VAR WORD 'DIVIDE PULS_HIGH

PULSIN PORTA.0 , 0 , PLO : PULSIN PORTA.0 , 1 , PHI

cutdown2:
PWIDTH = PLO + PHI
if PWIDTH > 655 then 'divide by 2 until 'TEST4' result 3 lines below won't overflow
PLO = PLO / 2 : PHI = PHI / 2 : goto cutdown2
endif

DTYCYC = ( PHI * 100 ) / PWIDTH
'LCDOUT $FE , $80 , "PLO:" , DEC5 PLO 'puls_low
'LCDOUT $FE , $88 , "PHI:" , DEC5 PHI 'puls_high
'LCDOUT $FE , $C0 , "PW:" , DEC5 PWIDTH
'LCDOUT $FE , $C8 , "DC:" , DEC2 DTYCYC

LCDOUT $FE , $82 , "DutyCycle:" , DEC2 DTYCYC


http://img95.imageshack.us/img95/1176/10ls0.jpg

MaxiBoost
- 28th December 2006, 10:50
This code is better work ,

'DEVICE 16F84A
DEFINE OSC 10

DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 3
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 2
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2

PLO VAR WORD 'PULS_LOW
PHI VAR WORD 'PULS_HIGH
PWIDTH VAR WORD 'ADD TWO RESULT (PULS_LOW+PULS_HIGH)
DTYCYC VAR WORD 'DIVIDE PULS_HIGH

PULSIN PORTA.0 , 0 , PLO : PULSIN PORTA.0 , 1 , PHI

cutdown2:
PWIDTH = PLO + PHI
if PWIDTH > 655 then 'divide by 2 until 'TEST4' result 3 lines below won't overflow
PLO = PLO / 2 : PHI = PHI / 2 : goto cutdown2
endif

DTYCYC = ( PHI * 100 ) / PWIDTH
'LCDOUT $FE , $80 , "PLO:" , DEC5 PLO 'puls_low
'LCDOUT $FE , $88 , "PHI:" , DEC5 PHI 'puls_high
'LCDOUT $FE , $C0 , "PW:" , DEC5 PWIDTH
'LCDOUT $FE , $C8 , "DC:" , DEC2 DTYCYC

LCDOut $fe, 1
LCDOUT $FE , $84 , "DUTY:" , DEC2 DTYCYC +1 ,"%"
pause 250

http://img246.imageshack.us/img246/7527/last1ae6.jpg

http://www.yandanyandan.com/Maxiboost/duty1.avi

video is ~800kb

skimask
- 28th December 2006, 13:50
@Skimask ,
yess its work ,thank your interesting .
Its not bad ,so close result with duty cycle meter.
I can use it on my car .

Some problem I have ,

1-why I see on the lcd "35" %100 duty cycle ?

2-People use RB0 input "schmit trigger" on duty cycle meter .
How can use schmit trigger input my circuit ?




1 - 100% (and 0%) duty cycle is a special case...100% pulshi and 0% pulslo. Put in a test to see if either pulshi or pulslo is 0 and handle it as required.

2 - why mess with it if it works?

Quite frankly, I'm surprise it worked that well for a first try, and that code was off the top of my head.

And your pictures were practically useless. Either backlight the LCD or turn on some lights! :)

MaxiBoost
- 28th December 2006, 14:36
@skimask ,
I`ll take this picture only for you :)

Schmit trigger ;
Because I`m work on the table at home ,not on car ,
and use a 555 for signal not car enjector`s signal .
Im sure cars injector signal or ecu` injector out signal is Not clean ..

Thank you again.

http://img72.imageshack.us/img72/6660/last2vs3.jpg

http://img99.imageshack.us/img99/3957/last3bb2.jpg

skimask
- 28th December 2006, 15:02
@skimask ,
I`ll take this picture only for you :)

Schmit trigger ;
Because I`m work on the table at home ,not on car ,
and use a 555 for signal not car enjector`s signal .
Im sure cars injector signal or ecu` injector out signal is Not clean ..

Thank you again.



If you want to use RB0 for the input, then use it, change the lines in the code. The schmidt trigger input will help to clean up a noisy signal a little bit, but I don't know if I'd go so far as to start messing with different pins and methods of reading the pulses until you've tried it in the car. I know the signals would be dirty being in an automotive environment...but... The injectors won't be firing at more than (give or take) 5,000/sec (10,000 rpm). So why not a simple opamp before the PIC with a 5khz rolloff or an even simpler R/C filter? Play with it and see what happens... that's the only way you'll figure out how much filtering you may or may not need.