Electronics > Microcontrollers

Duty Cycle meter Using PIC Mircontroller


     i want to build a simple duty cycle meter using PIC microcontroller CCP module,

 the frequency domain is about 1hz to 4mhz

  i have use timer prescaler a large value for low frequency ,
and i have to use timer prescaler a small value for high frequency range.

so my problem is how to automatically  select the range  for frequency

please suggest me something

Flavour Flave:
I copied and pasted the following from Microchip app note "Compiled Tips 'N Tricks Guide". This should help you out.  :) Find the app note and download it, lots of good stuff in it.

The duty cycle of a waveform is the ratio
between the width of a pulse (W) and the
period (T). Acceleration sensors, for example,
vary the duty cycle of their outputs based on
the acceleration acting on a system. The CCP
module, configured in Capture mode, can be
used to measure the duty cycle of these types
of sensors. Here’s how:

1. Configure control bits CCPxM3:CCPxM0
  (CCPxCON<3:0>) to capture every rising
  edge of the waveform.

2. Configure Timer1 prescaler so that Timer1
  will run to Tmax  without overflowing.(see Note 1)

3. Enable the CCP interrupt (CCPxIE bit).

4. When CCP interrupt occurs, save the
  captured timer value (t1) and reconfigure
  control bits to capture every falling edge.

Note 1: Tmax is the maximum pulse period
  that will occur.

5. When the CCP interrupt occurs again,
subtract saved value (t1) from current
captured value (t2) – this result is the pulse
width (W).

6. Reconfigure control bits to capture the next
rising edge.

7. When the CCP interrupt occurs, subtract
saved value (t1) from the current captured
value (t3) – this is the period (T) of the

8. Divide T by W – this result is the Duty Cycle

9. Repeat steps 4 through 8.

Edit: This didn't really answer your question but I assume this is the way you are doing it. Is the duty cycle varying alot? If timer 1 overflows you change the prescaler value.

thank you sir   for you reply

i have already make a software based on this

but the my software works only with a certain range of frequency like about 10hz to 50khz

all i wanted to do is add the functionality of auto ranging like if frequency is in mhz range then for  Tmax the prescaler is set to high prescaler

but for few khz range the prescaler must be set  to a lower value

i can set the range manually in the software or by some switch with pic

but how to do this auto range

For very high periods you can use a volatile variable as an auxiliary counter. Increment this counter on every timer overflow.

For auto-ranging set the prescaler to measure the lowest frequency. As soon you get 2 values from the capture module, calculate the difference between them and if the value is too small, decrease the prescaler to get a better measure resolution and repeat the measure.


[0] Message Index

There was an error while thanking
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod