|
|
View previous topic :: View next topic |
Author |
Message |
mrYalamen
Joined: 13 Aug 2011 Posts: 9 Location: Viet Nam
|
how to measure PWM signal with only CCP1 capture mode? |
Posted: Thu Sep 06, 2012 4:28 am |
|
|
Hello everybody!
I have some trouble to get ideas how to mesure duty and cycle form a PWM signal with only CCP1 capture mode.
I read "CCP tips n tricks" and they make me follow this steps:
+S1: config timer1, config CCP1 as capture mode rising edge, enable capture interrupt
+S2: When the interrupt is coming--> save t1 (CCP_1 register form timer1), reconfig Capture to Falling edge mode.
+S3: When the interrupt is coming--> save t2 (still form CCP_1 reg), continute reconfig Capture to Rising edge mode.
+S4: When the interrupt is coming--> save t3 (still form CCP_1 reg),
then repeat Step2 again.
Finally we got duty=t2-t1, cycle=t3-t1.
Do you have any idea to help me make it easy ? Because this process happen in uS (1kHz----20kHz may be).
I am very grateful if you help me! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Thu Sep 06, 2012 8:26 am |
|
|
What clock rate are you going to run the PIC?.
At 20KHz, you _will_ run into problems with not having enough time to service the first interrupt, before the second edge, unless your PIC is at a very high clock speed.
Remember at 20KHz, even if 50/50 mark/space, the two edges will be just 25uSec apart. If instead the duty cycle was just 10% (say), then just 5uSec apart. Stage 2, involves perhaps 50 machine instructions, so you need to be running the processor at something over 10MIPS (40MHz), to have any hope.
If the test cycle only needs to be done occasionally, then it can be handled much faster, by _polling_ the interrupt flags, rather than using an interrupt handler. You could probably halve the processor speed needed.
Best Wishes |
|
|
mrYalamen
Joined: 13 Aug 2011 Posts: 9 Location: Viet Nam
|
|
Posted: Thu Sep 06, 2012 9:48 am |
|
|
Thanks for your reply !
I am using PIC16F886 20MHz oscillator. In fact, i want to make my PIC become a H-bridge controller using ECCP (full bridge CCP1). So my PIC needs to measure duty cycle from Master controller using Capture mode from CCP2 . But you are right, this driver must be faster than PWM duty cycle. When the interrupt happens, too much cycle machine are needed to be process. Here is my idea code and it lost too much time.
Code: |
#include "16F886.h"
#fuses HS,NOPROTECT,NOWDT,NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7,bits=8)
unsigned int16 W,T,rise,fall;
unsigned int32 x=0;
#INT_CCP2
void cap_isr(void)
{
if(x%2==0)
{
rise=CCP_2;
setup_ccp2(CCP_CAPTURE_FE);
}
if(x%2!=0)
{
fall=CCP_2;
setup_ccp2(CCP_CAPTURE_RE);
}
x++;
W = fall - rise;
}
void main(void)
{
setup_ccp2(CCP_CAPTURE_RE);
setup_timer_1(T1_INTERNAL);
enable_interrupts(GLOBAL);
enable_interrupts(INT_CCP2);
while(true)
{
delay_ms(1000);
printf("%lu\r",W);
}
}
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|