View previous topic :: View next topic |
Author |
Message |
Bryan
Joined: 23 Apr 2005 Posts: 73
|
CCP interrupt/while loop issue |
Posted: Tue Oct 03, 2006 5:26 pm |
|
|
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
|
|
Posted: Tue Oct 03, 2006 5:56 pm |
|
|
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
|
|
Posted: Tue Oct 03, 2006 6:00 pm |
|
|
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
|
|
Posted: Tue Oct 03, 2006 8:14 pm |
|
|
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
|
|
Posted: Tue Oct 03, 2006 10:03 pm |
|
|
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). |
|
|
|