Duty Cycle meter Using PIC Mircontroller - Page 1
Forum section: electronics / general

author: RoadRunner
author_rank: Frequent Contributor
timestamp: January 02, 2012, 06:35:05 pm
content: Sir,i want to build a simple duty cycle meter using PIC microcontroller CCP module,the frequency domain is about 1hz to 4mhzi 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 frequencyplease suggest me something

author: Flavour Flave
author_rank: Regular Contributor
timestamp: January 02, 2012, 08:15:25 pm
content: 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 ratiobetween the width of a pulse (W) and theperiod (T). Acceleration sensors, for example,vary the duty cycle of their outputs based onthe acceleration acting on a system. The CCPmodule, configured in Capture mode, can beused to measure the duty cycle of these typesof sensors. Here’s how:1. Configure control bits CCPxM3:CCPxM0(CCPxCON<3:0>) to capture every risingedge of the waveform.2. Configure Timer1 prescaler so that Timer1will run to Tmax without overflowing.(see Note 1)3. Enable the CCP interrupt (CCPxIE bit).4. When CCP interrupt occurs, save thecaptured timer value (t1) and reconfigurecontrol bits to capture every falling edge.Note 1: Tmax is the maximum pulse periodthat will occur.5. When the CCP interrupt occurs again,subtract saved value (t1) from currentcaptured value (t2) – this result is the pulsewidth (W).6. Reconfigure control bits to capture the nextrising edge.7. When the CCP interrupt occurs, subtractsaved value (t1) from the current capturedvalue (t3) – this is the period (T) of thewaveform.8. Divide T by W – this result is the Duty Cycle9. 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.

author: RoadRunner
author_rank: Frequent Contributor
timestamp: January 02, 2012, 08:35:19 pm
content: thank you sir for you replyi have already make a software based on thisbut the my software works only with a certain range of frequency like about 10hz to 50khzall 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 prescalerbut for few khz range the prescaler must be set to a lower valuei can set the range manually in the software or by some switch with picbut how to do this auto range

author: johnmx
author_rank: Frequent Contributor
timestamp: January 02, 2012, 10:35:50 pm
content: 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.