1
\$\begingroup\$

I have a controller PIC18F and it supports input capture peripheral. I understand it can be used to find the frequency of the incoming signal. How can the duty ratio of PWM can be obtained from the following details in data sheet? Is there any way of doing so?

> bit 3-0 
 CCPxM<3:0>: CCPx Module Mode Select bits 0000 =
> Capture/Compare/PWM disabled (resets CCPx module) 0001 = Reserved 0010
> = Compare mode, toggle output on match (CCPxIF bit is set) 0011 = Reserved 0100 = 
 Capture mode:
 every falling edge 0101 = 
 Capture mode:
> every rising edge 0110 = 
 Capture mode: every 4th rising edge 0111 =
> Capture mode: every 16th rising edge 1000 = Compare mode: initialize
> CCPx pin low; on compare match, force CCPx pin high (CCPxIF bit is
> set) 1001 = Compare mode: initialize CCPx pin high; on compare match,
> force CCPx pin low (CCPxIF bit is set) 1010 = Compare mode: generate
> software interrupt on compare match (CCPxIF bit is set, CCPx pin
> reflects I/O state) 1011 = Compare mode: Special Event Trigger; reset
> timer on CCPx match (CCPxIF bit is set) 11xx = PWM mode
> 

data sheet page 257. PIC18F46J53.

Is there any way to calculate the duty ratio? I trust from this only frequency can be obtained.

JYelton
35.7k34 gold badges149 silver badges281 bronze badges
asked Oct 15, 2014 at 10:05
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

My understanding is that a CCP module can capture either a rising, or a falling edge, but not both.

To get the duty cycle you will need to capture both the rising and falling edges to then compare the times of both and work out the ratio. That can't be done with just one module.

However, there's nothing to stop you sending the signal in to two separate CCP modules, one configured for rising edge, and one for falling edge, and using the two to calculate the difference between the rising and falling.

You'd basically end up with two periods, which should both be the same, but a phase difference between them, and it's that phase difference that would equal the duty cycle.

answered Oct 15, 2014 at 10:13
\$\endgroup\$
3
  • 1
    \$\begingroup\$ That can't be done with just one module. It can be done, actually. I've done it recently with a PIC18. You have to set the CCP module to a rising edge, wait for the interrupt and change the mode to be falling edge and so on.. \$\endgroup\$ Commented Oct 15, 2014 at 11:12
  • \$\begingroup\$ @Majenko Thank you very much.I found a similar item on DSPIC CCP module application note the duty ratio mode (Edge Detection Mode) \$\endgroup\$ Commented Oct 15, 2014 at 11:14
  • \$\begingroup\$ @m.Alin thank you very much .I will do try your method. \$\endgroup\$ Commented Oct 15, 2014 at 11:16
1
\$\begingroup\$

To answer your question about other methods, some PICS have a very convenient timer gate that acts independently of the CCP module. If you can use one of these, its VERY convenient for this calculation. Count while the signal is high, using the gate, and get your Period with the CCP.

A quick search shows PIC18(L)F2X/4XK22 (link to pdf data sheet) seems to have this feature (see section 12)

answered Oct 15, 2014 at 12:51
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.