Menu

Read Duty Cycle Input - PIC16F1823

Phil D
2021-01-01
2021-01-02
  • Phil D

    Phil D - 2021-01-01

    Hi GCB team,

    Happy New Year!!!

    I am wanting to read/measure the duty cycle on an input pin using the PIC16F1823. Is this possible? I cannot seem to find anything in the documentation. Do you have an example codes to help me set this up ?

    Thanks,
    Phil

     
  • David Stephenson

    I've done this for a particle detector (PM2.5). The basic procedure is to use the gate on TIMER1 connected to the signal so that TIMER1 will only run if the gate is high (or low - can be selected).
    One of the other timers needs to run continuously (with suitable pre and post scale selected).
    When this second timer overflows the values in TMR1H:TMR1L give the amount of time the gate has been on (or off).
    This is something of a niche activity so will probably have to be done by writing to the registers rather than using GCB commands. Below is my example it is on a 16F18326 so I have more 16-bit timers available. In the example time1 is running continuously (from an external clock crystal) and timer3 and timer5 are gated (from the particle detector).
    The processor is normally in SLEEP and wakes up on a TIMER1 overflow (every 8 seconds) when timer3 and timer5 can be read.
    Note the processor you have has only one 16-bit counter (timer1) and the register names may be different, but I hope it gives you some ideas.

    t1con=b'10101101' 'timer1 4:1, sosc
    TMR1H=b'00000000' '8 sec roll-over
    TMR1L=b'00000000'
    pir1.0=0 'set interrupt flag off
    intcon.6=1 'enable peripheral interrupts
    pie1.0=1 'enable timer1 overflow interrupt
    t1gcon.7=0 'timer1 continuous counting
    
    t3con=b'11101101' 'timer3 prescaler 4:1
    TMR3H=b'00000000' 'zero counter high byte
    TMR3L=b'00000000' 'zero counter low byte
    t3gcon=b'10000000' 'count when t3gate pin low
    
    t5con=b'11101101' 'timer5 4:1
    TMR5H=b'00000000' 'zero counter
    TMR5L=b'00000000'
    t5gcon=b'10000000' 'count when t5gate pin low
    
     
  • William Roth

    William Roth - 2021-01-02

    The ideal code/method /PIC depends upon how quickly and how often you need to measure the duty cycle.

    Many PIC chips include a Signal Measurement Timer (SMT) peripheral that simplifies measuring frequency, pulse width, duty cycle, et al. It is very fast and very accurate.

    If speed a accuracy are important would use a PIC 16F18426 with SMT.

     
  • William Roth

    William Roth - 2021-01-02

    Measuring a pulse width alone will not provide enough information to allow calculation of duty cycle unless the frequency is fixed. If the frequency varies then both the off time and on time will need to be measured. In some circles these are referred to as mark and space. The mark time and space time are summed to give Cycle time/Period then the period is divided by the mark time to give duty cycle in percentage.

    duty cycle % = 100 / ( (Mark + Space) / Mark )

    The SMT Timer peripheral does this automagically

    The same general method applies to any chip.
    1. Measure the on time
    2. Measure the off time
    3. Do the math

    Skip # 2 if the frequency/period is fixed

     

    Last edit: William Roth 2021-01-02
  • mkstevo

    mkstevo - 2021-01-02

    Due to the way the mains waveform is sampled in my design the timing of the "on" and "off" pulses are not symmetrical, I do indeed add the two timings together in order to get an accurate (or probably, less inaccurate) measurement of the period.

    I had not been aware of the SMT peripheral, thanks for advising me of that, I'll look into it should I be needing genuine accuracy.

    In the Grid Frequency Meter, I can resolve the frequency of the 50Hz (nominally) supply to within a few mHz. Once you get into that level of resolution it becomes difficult to gauge the accuracy to any useful degree. Compared to the original Elektor design that is crystal controlled, I can calibrate mine to within a few mHz, after calibration it remains fairly consistent. I can live with that.

     
  • mkstevo

    mkstevo - 2021-01-02

    There is an entry in the GCB help for the SMT timer here:
    http://gcbasic.sourceforge.net/help/_smt_timers.html

     
  • William Roth

    William Roth - 2021-01-02

    You could use the Zero Cross Module in combination with the SMT Module (in Mode2) to make a nice Grid Freq Meter. Use a good external crystal for the PIC OSC and it should be quite accurate.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.