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

CCP1 question
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 19795

View user's profile Send private message

PostPosted: Tue Aug 28, 2012 7:02 am     Reply with quote

pulse_width = 65535-rise+fall;

Correct value is 65536, not 65535.

The counter counts from 0 to 65535, then back to 0. 65536 counts from 0 to 0.

Best Wishes
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Tue Aug 28, 2012 7:57 am     Reply with quote

Thank you.

For the reason of minimizing the hardware, keeping the designed circuit board as small as possible as well as minimizing the costs, I do not want to add an external oscillator or crystal. I will have to make this as good as it gets with the internal oscillator.

So based on all the previous posts - not to use timer interrupt, try to keep it as short and clear as possible here is the ccp1 interrupt rutine:

Code:

#int_CCP1
void  CCP1_isr(void)
{
     If (!ccpflag){
         rise = CCP_1;
         ccpflag = 1;
         setup_ccp1(CCP_CAPTURE_FE);
     }
     Else {
         fall = CCP_1;
         If (rise < fall) pulse_width = fall - rise;
         Else {       
            pulse_width = 65536-rise+fall;
         }
         iflag = 1;
         ccpflag = 0;
         setup_ccp1(CCP_CAPTURE_RE);
     }
         
}


this way if an overflow is detected it will calculate the correct value of the pulse with the "Else" condition and while no overflow is detected it will use the "if" portion of the statement. I think this is as good as it can get with the existing hardware.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Aug 28, 2012 1:10 pm     Reply with quote

you are SO NOT GETTING the point of what you are being told:

let me quote:

Quote:

In Capture mode, the CCPRxH, CCPRxL register pair
captures the 16-bit value of the TMR1 register when an
event occurs on pin CCPx. An event is defined as one
of the following and is configured by the CCP1M<3:0>
bits of the CCP1CON register:
• Every falling edge <--
• Every rising edge




nearly everything you need to understand about doing this is in the quote above and HINT: it has nothing to do with reading timer1 inside an ISR , either !!!

if done right - with the timer overflow margin YOU Have - will work fine - no wrap worries and NOT be influenced by INT latency either .....
as 65535 usecs of range is FAR longer than you require for RC servo pulse width measurement. you can imagine the improvement in resolution that an 8 or 16 mhz clock could offer and still have 32 or 16 seconds of total range.

think hard about that quote - versus what you coded
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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