|
|
View previous topic :: View next topic |
Author |
Message |
soondin
Joined: 21 Apr 2011 Posts: 9
|
How can I make exact delay_us or pulse ? |
Posted: Thu Apr 21, 2011 5:01 am |
|
|
Hello All,
Following is part of my code.
---------------------------------------
#int_ccp2
void isr()
{
rise = CCP_1;
fall = CCP_2;
p_wid = fall - rise;
}
void main()
{
setup_ccp1(CCP_CAPTURE_RE);
setup_ccp2(CCP_CAPTURE_FE);
setup_timer_1(T1_INTERNAL);
enable_interrupts(INT_CCP2);
enable_interrupts(GLOBAL);
while(TRUE)
{
us = p_wid/5;
output_low(PIN_A0);
delay_us (us);
output_high(PIN_A0);
delay_us (us);
}
}
---------------------------------------
Use that one, I try make square wave with CCP Capture interrupt.
But I can't make exact duration of wave.
when I check with Scope, some time be slight long what I want.
How can I make most same output from input pulse?
Please help me.
My PIC is 18F2550 with 20MHz. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Thu Apr 21, 2011 5:25 am |
|
|
Using your code....
You'll have to 'compensate' for the time to be in the ISR as well as your output.. functions.
Dump the listing, grab the datasheet and compute the time it takes to execute the instructions your program has.Then subtract the necessary instructions to make the output pulse the same width as the input pulse.
IE. if your input is 5 us 'wide' and the output is 7us 'wide', remove 2us of code.
You don't show how you declare your variables,but all should be int16 for better 'timing' |
|
|
soondin
Joined: 21 Apr 2011 Posts: 9
|
|
Posted: Thu Apr 21, 2011 6:19 am |
|
|
Thank you for reply.
unsigned long rise,fall,p_wid,us;
This is set of my Vals.
temtronic wrote: | Using your code....
You'll have to 'compensate' for the time to be in the ISR as well as your output.. functions.
Dump the listing, grab the datasheet and compute the time it takes to execute the instructions your program has.Then subtract the necessary instructions to make the output pulse the same width as the input pulse.
IE. if your input is 5 us 'wide' and the output is 7us 'wide', remove 2us of code.
You don't show how you declare your variables,but all should be int16 for better 'timing' |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19543
|
|
Posted: Thu Apr 21, 2011 9:52 am |
|
|
There is no way to make an 'exact' delay in software, if the processor is doing other things in interrupts.
Realistically this is the whole 'point' of the PWM....
You could, depending on the pulse rate involved, use just one CCP, to record both edges, by programming it to (say) the rising edge. Then when you get the interrupt for this, record the value, and change it to record the falling edge. On the second interrupt, you then have both edges recorded. This would then leave the second CCP, available for use as a PWM.
For longer delays, you could do your delay using a loop, and a global counter, and if the interrupt gets called, decrement the counter by the amount corresponding to the interrupt overhead. You will still get a slight variation if the edge is needed at the moment the interrupt is called, but a lot 'better'. Alternatively, using a hardware timer, and calculating the 'count' corresponding to the required edge, would allow the error to reduce to at most the overhead from one interrupt, instead of being 'cumulative' (error gets worse with a simple delay, if the interrupt occurs multiple times in the delay....).
Best Wishes |
|
|
|
|
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
|