How can I measure Duty cycle ?


Closed Thread
Results 1 to 21 of 21

Hybrid View

  1. #1
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default What's wrong?

    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.

  2. #2
    Join Date
    Nov 2006
    Location
    ist
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    @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



  3. #3
    Join Date
    Nov 2006
    Location
    ist
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    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://www.yandanyandan.com/Maxiboost/duty1.avi

    video is ~800kb

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by MaxiBoost View Post
    @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!

  5. #5
    Join Date
    Nov 2006
    Location
    ist
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    @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.




  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by MaxiBoost View Post
    @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.
    Last edited by skimask; - 28th December 2006 at 17:42. Reason: Changed my mind :)

Similar Threads

  1. Replies: 9
    Last Post: - 8th October 2008, 11:15
  2. how to generate 83.33khz with 16.7% duty cycle?
    By donatelo in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 23rd September 2008, 17:08
  3. HPWM the old fashioned way
    By Srigopal007 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 19th November 2004, 19:09
  4. PWM _ Duty Cycle in Tenths
    By rwskinner in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 17th May 2004, 12:09
  5. Duty Cycle Dilemmas
    By crankshaft in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 27th February 2003, 12:40

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts