1.2 Possible Solutions

The most accurate measurement will consist of the fastest clock source with a timer on the lowest prescale. The lowest timer prescale yields the highest resolution. Higher resolution typically increases the maximum count, which may necessitate compensation for rollover of the timer.

An interrupt can be incorporated into these routines only if non blocking code is used. The accuracy of this implementation may suffer as a result. For example, the Interrupt-on-Change (IOC) pin can cause an interrupt when a rising or falling edge is detected. The pulse measurement can now be completed inside of the ISR without the need to constantly poll the pin. While this may sound ideal, the user must now accommodate for the 3-5 instruction cycle delay that is caused by servicing an asynchronous interrupt.

When absolute accuracy is important, then an external crystal should be used since the internal oscillator block can have a drift up to 5% of its nominal frequency.

All of the measurements in this document including the associated code use an internal 16 MHz system clock with all timers on a 1:1 prescale of the system clock (FOSC), unless otherwise noted. The low and high waveform length constraints, as well as the accuracy of the measured result are calculated using Table   1 and Table   2. If rollover is accounted for in software-based methods, then the accuracy of the measurement will decrease in proportion to the software routine overhead.

These methods assume a pulse to be active-high and a period to be the time between two rising edges. See Figure   1 and Figure   2 for clarification. A generic duty cycle equation (Figure   3) is also provided based on Figure   1 and Figure   2. There are more method-based equations later in the document.

Figure 1-2. Pulse Width Definition
Figure 1-3. Duty Cycle is the Ratio of Pulse Width and Period
Equation 1-1. Generic Duty Cycle Equation
duty_cycle=(T2T1)(T3T1)*100%
Table 1-1. Associated Code Numbers
SolutionModulesPIC MCUInterruptLanguage*Limit HighResolution
Pulse Measurements – Code Numbers
Timer1 GateTimer1 GatePIC16F18076YesC4.1 ms62.5 ns
Timer1
CLC and NCOCLC1/2(/3)PIC16F18076YesC65.54 ms62.5 ns
NCO1
CCPCCP1PIC16F18076YesC16.38 ms250 ns
Timer1/3
IOC with TimerIOC/INTPIC16F18076YesC1 ms

250 ns

Timer0
IOC without TimerIOCPIC16F18076YesC4.194s2.75 μs
Polled InputPolled InputPIC16F18076NoC4.194s2.5 μs
Duty Cycle Measurements – Code Numbers
Timer1 GateTimer1 GatePIC16F18076YesC4.1 ms125 ns
Timer1
CLC and NCOCLCPIC16F18076YesC65.54 ms62.5 ns
NCO1
CCPCCP1PIC16F18076YesC16.38 ms250 ns
Timer1/3
IOC with TimerIOC/INTPIC16F18076YesC1 ms250 ns
TimerX
IOC without TimerIOCPIC16F18076YesC4.194s5.5 μs
Polled InputPolled InputPIC16F18076NoC4.194s5 μs

*The upper limit on these calculations can be extended to any set amount. The limit that is represented in this table reflects a single timer rollover.

Table 1-2. Equations used for the Results shown in the ‘Associated Code Numbers’ table
SolutionModulesLimit HighResolution
Pulse Measurement – Equations
Timer1 GateTimer1 Gate(1f)*2n1f
Timer1
CLC and NCOCLC1/2(/3)(4f)*2n

n = NCO ACCUM bit width

1f
NCO1
TimerX
CCPCCP1(1f)*2nSoftware dependent
Timer1/3
IOC with TimerIOC/INT(1f)*2n*4Software dependent (16x prescaler)
Timer0
IOC without TimerIOC(4f)*2nSoftware dependent
Polled InputNone(4f)*2nSoftware dependent
Duty Cycle Measurements – Equations
Timer1 GateTimer1 Gate(1f)*2n1f
Timer1
CLC and NCOCLC1/2(/3)(1f)*2n

n = NCO ACCUM bit width

1f
NCO1
TimerX
CCPCCP1/2(4f)*2nSoftware dependent
Timer 1/3
IOC with TimerIOC/INT(1f)*2n*4Software dependent (16x prescaler)
Timer0
IOC without TimerIOC(4f)*2nSoftware dependent
Polled InputNone(4f)*2nSoftware dependent

n = TimerX bit width

f = Clock Frequency