EDN logo

Design Ideas

March 3, 1997


Simple method measures duty cycle

Jarkko Vuori, Helsinki University of Technology, Finland


Various methods exist to measure the duty cycle of a digital waveform. For example, some simple PWM A/D converters give their output as a square wave whose mark/space ratio varies according to the analog input signal. Some modern light and pressure sensors produce an output that is a duty-cycle-modulated digital signal. You can also measure duty cycle with a µP by using two counters gated with the appropriate mark/space states of the input signal. The problem with this approach is that today's tiny µCs, such as the 68HC05K1, include no general-purpose timers.

One interesting alternative approach is to take random samples of the digital input signal. The results of these samples are either 0s or 1s, depending on the state of the input signal. The duty cycle of this input signal is then the average of S samples:

where N is the sample number.

This average is easy to calculate, even for the simplest mCs. For example, the operating system can sample the input signal when the system is idle. If the sample is a logic 1, the system must increment the averaging and sample counters. Otherwise, the system increments only the sample counter. When the sample counter reaches a predetermined value--65536, for example--the result of the conversion is the averaging counter shifted by 16 bits to the right.

You can calculate the resolution, or how many bits you have in your simple ADC, using statistical methods. The random sample has only two values, mark or space. The probability of the mark is p, and the probability of the space is 1­p (Figure 1). Probability p is also the duty cycle of the input waveform. You can reasonably assume that the random samples are independent and uniformly distributed. This kind of random process has a binomial distribution. The mean of this random process is µ=Np, and the variance is lowercase sigma2=Np(1­p). The mean of the averaged sample is then

and the variance is

The variance of the normal ADC is

where B is the resolution of the converter (in bits). Equating those variances and solving for B produces the following equation for the resolution using the random-sampling method:

Figure 1b shows the resolution you obtain with this method when you take 1000 to 100,000 samples. Notice that the number of bits necessary to get the appropriate resolution increases exponentially. For example, if you take 1 million samples, you get the equivalent resolution of 9.2 bits. Thus, this method is suitable only for low-resolution measurements for which the necessary resolution is a maximum of 9 bits. But when you don't need a high-resolution measurement?, the proposed method results in a simple analog front end and straightforward software implementation.

Listing 1 includes simple program code for the 68HC05K1 µC that calculates the average of 256 1-bit samples. (Click here to download DI-SIG, #1994.) The effective resolution with 256 samples is approximately 3 bits. (DI #1994)

Figure 1
One way to measure duty cycle is to take random samples of a digital signal. You can use statistical methods to determine the resolution; p is the probability of a mark, and 1­p is the probability of a space (a). The number of samples necessary to reach a certain resolution increases exponentially (b).

 

Listing 1
Duty-cycle measurement routine
; Measurement subroutine to be called at random intervals
;
; First, increment bit accumulator if input at 1 state
TSTBIT: BRCLR 0,PORTAD,NOHIT
INC HITCLO
BNE NOHIT
INC HITCHI
; otherwise, just check to see if you have 256 samples
NOHIT: DEC SMPLC
BNE NOTYET
; if all samples taken, divide the result by 256
; (just take the upper 8 bits of the result)
LDA HITCHI
STA RESULT
CLR HITCLO
CLR HITCHI
NOTYET: RTS
HITCLO: RMB 1 ; upper 8 bits
HITCHI: RMB 1 ; lower 8 bits
SMPLO: RMB 1 ; sample counter


| EDN Access | Feedback | Subscribe to EDN | Table of Contents |


Copyright © 1996 EDN Magazine. EDN is a registered trademark of Reed Properties Inc, used under license.