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 support@ccsinfo.com

CCP interrupt/while loop issue

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



Joined: 23 Apr 2005
Posts: 73

View user's profile Send private message Send e-mail AIM Address

CCP interrupt/while loop issue
PostPosted: Tue Oct 03, 2006 5:26 pm     Reply with quote

I am trying to measure the pulse widths of various signals using the CCP function. I know that the CCP is working correctly because I have pin A2 mirroring the input signal coming from an outside source and barring a slight delay the input and output signals (output signal is the aforementioned A2 pin) are identical when read on an oscilloscope. In the CCP interrupt routine (where I set the output pin high or low depending on the edge it captures) I increment a variable (initial value of 0) to detect how many times it captures a rising or falling edge. Later I have a while loop that doesn't go false until this counter gets past 0 meaning ccp captured the first edge as you can see from the code below. I see on the oscilloscope that it is capturing the edges, but this variable either never gets incremented (should be impossible) or the while loop comparison syntax is somehow wrong because the code never leaves the while loop. Code follows:

Code:

#int_ccp1
void ccp_isr()
{   
   
    if (ccpre)
    {
        rise = CCP_1;
        output_high(PIN_A2);
       
        //If the timer did not roll over mid pulse
        if (rise >= fall)
        {
            data[bitpos] = rise - fall;
        }
           
        /*If the timer rolled over mid pulse, add its fall time from 0
            and the difference between its rise time and the timer's max
            value of 65535 (16 bit)*/
        else
        {
            data[bitpos] = fall + (65535 - rise);
        }
           
        setup_ccp1(CCP_CAPTURE_FE);
       
        bitpos++;
           
        ccpre = false;
    }
   
    else
    {
        fall = CCP_1;
        output_low(PIN_A2);
       
        //If the timer did not roll over mid pulse
        if (fall >= rise)
        {
            data[bitpos] = fall - rise;
        }
           
        /*If the timer rolled over mid pulse, add its fall time from 0
            and the difference between its rise time and the timer's max
            value of 65535 (16 bit)*/
        else
        {
            data[bitpos] = rise + (65535 - fall);
        }
           
        setup_ccp1(CCP_CAPTURE_RE);
       
        bitpos++;
           
        ccpre = true;
    }
}

.
.
.
.
void main()
{
//Ensure CCP pin begins low
    output_low(PIN_C2);
   
    output_high(PIN_A2);
   
    setup_timer_1(T1_INTERNAL);
    enable_interrupts(GLOBAL);
    enable_interrupts(INT_TIMER1);
    enable_interrupts(INT_CCP1);
   
    while(bitpos == 0); // Never gets past here


Any thoughts on why this might be happening?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 03, 2006 5:56 pm     Reply with quote

Before we look at this, can you post the declarations of all the
variables that are used in the posted code ?
Bryan



Joined: 23 Apr 2005
Posts: 73

View user's profile Send private message Send e-mail AIM Address

PostPosted: Tue Oct 03, 2006 6:00 pm     Reply with quote

Code:

unsigned long data[89];
boolean ccpre = false;
unsigned long rise = 0;
unsigned long fall = 0;
unsigned long pulsewidth = 0;
long bitpos = 0;


Here are all of the variables
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 03, 2006 8:14 pm     Reply with quote

In your main() function, I don't see a setup_ccp1() statement.
The power-on reset state for the CCP is to be disabled.
I'm not sure how you're getting interrupts when the CCP is shut off.

Can you post a complete test program that shows the problem ?
You've already posted the #int_ccp1 function, so you don't have
to post that again, but I would like to see a complete test program.
Basically, this means filling in the main() function some more.
Also, show the #include statement for the PIC,and the #fuses
statement, and any #use statements as well.

Also give your compiler version.

If you do that, I can install your version and run the code on
a PicDem2-Plus board and try to duplicate the problem.
Guest








PostPosted: Tue Oct 03, 2006 10:03 pm     Reply with quote

Thank you for your help. I do have it set up in main, I just didn't show that part of the code because I didn't think it pertained directly to my problem.

I figured out what was going on - I had the timer 1 interrupt enabled without a timer1 ISR and this messed with my entire system. Once I disabled the timer1 interrupt timer1 still runs since I set it up, but it no longer interferes with the other interrupts (namely CCP).
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