CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How can I make exact delay_us or pulse ?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
soondin



Joined: 21 Apr 2011
Posts: 9

View user's profile Send private message

How can I make exact delay_us or pulse ?
PostPosted: Thu Apr 21, 2011 5:01 am     Reply with quote

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: 9241
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Apr 21, 2011 5:25 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 21, 2011 6:19 am     Reply with quote

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: 19537

View user's profile Send private message

PostPosted: Thu Apr 21, 2011 9:52 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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