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, CCP2 interrupts

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







CCP1, CCP2 interrupts
PostPosted: Thu Nov 17, 2005 12:03 pm     Reply with quote

Hello,

I am using the Both CCP modules in the capture mode to measure frequency on a PIC18F2410. I Have CCP1 set for Timer1 and CCP2 set for Timer3.

SETUP_CCP1(CCP_CAPTURE_RE);
SETUP_CCP2(CCP_CAPTURE_RE|CCP_USE_TIMER3);

I have CCP2 set for the RC1 pin

#FUSES HS,NOWDT,CCP2C1

When I see a pulse on the input pin I go to the interrupt routine and grab the time measurement. Then reset the timer to zero for the next measurement.

#INT_CCP2
void ccp2_isr()
{
time2 = CCP_2;
SET_TIMER3(0);
flips2=rollover2;
rollover2 = 0;
}


#INT_CCP1
void ccp1_isr()
{
time1 = CCP_1;
SET_TIMER1(0);
flips1=rollover1;
rollover1=0;
}

The rollover is coming from the Timer interrupt routine

#INT_TIMER1
void timer1_isr()
{
rollover1++;
}

#INT_TIMER3
void timer3_isr()
{
rollover2++;

}

My problem stems from only having one input. I place a square wave at one of the inputs and although the correct channel reads the correct frequency, the other interrupt routine is still being called. I know this because on a break, there is a value in time2, which is not set anywhere else. I know there are no other pulses coming in because I have grounded the input. If I comment out the ENABLE_INTERRUPTS(INT_CCP2); time2 will stay at a value of 0x00. Will an interrupt on any CCP module fire both interrupts? any help would be appreciated. I am basically trying to add two frequencies and with only one input the total frequency is messed up.

Thank you,

Steve
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 17, 2005 3:17 pm     Reply with quote

Can you clarify what you're saying in your last paragraph ?
You keep referring to "one input". Do you mean one input signal ?
Or do you mean one input pin on the PIC ? Just re-phrase the whole
paragraph.
Steve
Guest







re: CCP1, CCP2
PostPosted: Thu Nov 17, 2005 4:07 pm     Reply with quote

Sorry about that.

The object of the code will be to add two frequencies. Each frequency will be put through some circuitry so that the micro pin will have levels that it can handle. I started out with just the CCP1 module to see if I could get the micro to correctly read the frequency; which it did. I then tried to copy that using the CCP2 module of the micro. Once again it worked (I had shut down the CCP1 module while testing this half). The next step was to have both modules active.

With a frequency injected into the CCP1 module, the frequency is read correctly but the CCP2 interrupt is firing multiple times. I can tell this because the interrupt routine is the only place time2 is set with a value. Also, if I comment out the enable interrupt for CPP2, the time2 value stays at 0x00. Now I thought maybe the frequency was getting to the CPP2 input pin so a grounded it. I still had the multiple interrupts. If I switched the inputs around so that the frequency was at the CCP2 input and the CCP1 input was grounded, I get a good frequency calculation in time two and multiple interrupts on CCP1.

So I guess my question is do the CCP modules work in conjuction; that is if one is interrupted do both flags get set and both interrupt routines need servicing?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 17, 2005 4:33 pm     Reply with quote

Can you post a small, but complete test program that shows the
problem ? I want to see the lines of code for the CCP setup.
Make sure the program is complete. It should be "drop in" compilable
into an MPLAB project. ie., with all necessary #include, #use, #fuses
statements.

Also post the version of your PCH compiler. This will be a number such
as 3.191 or 3.239, etc. It's at the top of the .LST file which is in your
project folder.
Steve
Guest







re: CCP1, CCP2
PostPosted: Mon Nov 21, 2005 2:01 pm     Reply with quote

Sorry for the time delay. This is a side project that I had not been able to get back to for the weekend. Here is the code, I believe this will compile but I did not actually compile it. I have multiple files and I did a cut and paste job.

#include <18f2410.h>
#include <math.h>


#FUSES HS,NOWDT,CCP2C1

#use delay (clock=4000000)

#define BASE 0.000001 // time base for a 4Mhz crystal
static int1 flag;
static int1 output;
static int rollover1, flips1;
static int rollover2, flips2;
static int1 measure;
static long time1, time2;
static float freq1, freq2, total_freq;
static float remainder;



static int ready;
static float useconds;
static long time;


#INT_CCP2
void ccp2_isr()
{
time2 = CCP_2;
SET_TIMER3(0);
flips2=rollover2;
rollover2 = 0;
}


#INT_CCP1
void ccp1_isr()
{
time1 = CCP_1;
SET_TIMER1(0);
flips1=rollover1;
rollover1=0;
}



#INT_TIMER0
void timer0_isr()
{
output = !output;
if (output)
OUTPUT_HIGH(PIN_A0);
else
OUTPUT_LOW(PIN_A0);
SET_TIMER0(time);
}

#INT_TIMER1
void timer1_isr()
{
//zero_flow1=1;
rollover1++;
}

#INT_TIMER2
void timer2_isr()
{
ready++;
if (ready == 30)
{
measure = 1;
ready = 0;
}
}

#INT_TIMER3
void timer3_isr()
{
//zero_flow2=1;
rollover2++;

}



void main(void)
{
SET_TIMER0(0);
SETUP_TIMER_0(RTCC_DIV_1|RTCC_INTERNAL);

SET_TIMER1(0);
SETUP_TIMER_1(T1_DIV_BY_1|T1_INTERNAL);

SET_TIMER2(0);
SETUP_TIMER_2(T2_DIV_BY_16, 255, 16);

SET_TIMER3(0);
SETUP_TIMER_3(T3_DIV_BY_1|T3_INTERNAL);

SETUP_CCP1(CCP_CAPTURE_RE);
SETUP_CCP2(CCP_CAPTURE_RE|CCP_USE_TIMER3);

// turn on the pullup for the sensors
// OUTPUT_LOW(PIN_A4);
// OUTPUT_LOW(PIN_A5);

// turn on the 2,5 Volt Level for the sensors
OUTPUT_HIGH(PIN_A2);
OUTPUT_HIGH(PIN_A3);





// ENABLE_INTERRUPTS(INT_TIMER0);
ENABLE_INTERRUPTS(INT_TIMER1);
ENABLE_INTERRUPTS(INT_TIMER2);
ENABLE_INTERRUPTS(INT_TIMER3);
ENABLE_INTERRUPTS(INT_CCP1);
ENABLE_INTERRUPTS(INT_CCP2);
ENABLE_INTERRUPTS(GLOBAL);




while (1)
{
if (flag)
OUTPUT_LOW(PIN_A1);
else
OUTPUT_HIGH(PIN_A1);
if (measure)
{
//freq1 = Calc_Freq1();
freq1=1/(((65536 * flips1) + time1) * BASE);
flips1=0;
if (freq1 > 2000000)
freq1=0.0;

// freq2 = Calc_Freq2();
freq2=1/(((65536 * flips2) + time2) * BASE))
flips2=0;
if (freq2 > 2000000)
freq2=0.0;

total_freq = freq1 + freq2;

measure =0;
}//end of if measure

}// end of while
}// end of main
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 21, 2005 3:31 pm     Reply with quote

What is your compiler version ?
Steve
Guest







Re:CCP1, CCP2
PostPosted: Tue Nov 22, 2005 8:41 am     Reply with quote

I am sorry, I have been doing this posting on a different computer than the development is on. The version is: 3.218. Thanks for the help, as I have still not yet been able to progress past this problem.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 22, 2005 2:57 pm     Reply with quote

I don't have that version, so I can't look at the .LST file for it.

The implication of your post is that the CCP1 isr is being called.
But if you ground the CCP1 input pin, you still get the problem.
So some internal process is setting the CCPIF bit. The question is,
how is this happening ?

The data sheet offers a few suggestions:


Quote:
When the Capture mode is changed, a false capture
interrupt may be generated. The user should keep the
CCPxIE interrupt enable bit clear to avoid false inter-rupts.
The interrupt flag bit, CCPxIF, should also be
cleared following any such change in operating mode.

Are you calling setup_ccp1() anywhere else in your program ?


Quote:
Switching from one capture prescaler to another may
generate an interrupt. Also, the prescaler counter will
not be cleared; therefore, the first capture may be from
a non-zero prescaler. Example 14-1 shows the
recommended method for switching between capture
prescalers. This example also clears the prescaler
counter and will not generate the “false” interrupt.

I don't think you're doing this.


Quote:

Note: If RB3/CCP2 or RC1/CCP2 is configured
as an output, a write to the port can cause
a capture condition.

This note is about CCP2. I wonder if it also applies to CCP1.
Steve
Guest







re CCP1, CCp2
PostPosted: Tue Nov 22, 2005 3:22 pm     Reply with quote

Hello again,

THe only Setup calls are those listed in the first few line of main. As you suggest, I do not change prescalers anywhere on CCP1 or CCP2 or Timer1 or Timer 3, so I do not think that is the case. I do not have a TRIS staement for the CCP2 input, so I think that is what I will try first. If I set all the inputs/outputs of the port maybe it will point me in a direction. The tris statement seems to be a confusing thing, depending on who I talk with. Some say it is out of date and the compiler should set the port pins as needed and some use the Tris statement in every project they have.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 22, 2005 3:39 pm     Reply with quote

The compiler does set the TRIS in the CCS functions. (PCH vs. 3.239)
Quote:
.................... SETUP_CCP1(CCP_CAPTURE_RE);
0736: MOVLW B7
0738: ANDWF T3CON,F
073A: BSF TRISC.2
073C: CLRF CCP1CON
073E: MOVLW 05
0740: MOVWF CCP1CON
.................... SETUP_CCP2(CCP_CAPTURE_RE|CCP_USE_TIMER3);
0742: MOVLW B7
0744: ANDWF T3CON,F
0746: BSF T3CON.3
0748: BSF TRISC.1
074A: CLRF CCP2CON
074C: MOVLW 05
074E: MOVWF CCP2CON


Some other things to check:
1. Check the .SYM file and see if the Time1 variable ram addresses
are used by any other variables. They shouldn't be, but verify this.

2. Check the CCS interrupt dispatcher code which starts at address
0x0008, and verify that it checks for both the CCP1IE and CCP1IF bits
to be true before it jumps to the CCP1 isr. Example (compiled with
the .LST file set for Symbolic format):
Code:

007E:  BTFSS  PIE1.2  // CCP1IE
0080:  GOTO   008A
0084:  BTFSC  PIR1.2  // CCP1IF
0086:  GOTO   00F4    // CCP1 isr
008A:  BTFSS  PIE2.0  // CCP2IE
008C:  GOTO   0096
0090:  BTFSC  PIR2.0  // CCP2IF
0092:  GOTO   00DC    // CCP2 isr
Steve
Guest







re:CCP1, CCP2
PostPosted: Wed Dec 28, 2005 7:46 am     Reply with quote

After many frustrating hours, a friend has located an errata sheet. I had no idea about such a beast. In it, it states that a breakpoint can cause unwanted interrupts. Well it seems that this is the case in my scenario. If I free run the instrument, it appears to be working correctly, when I attempt to actually read the internal variables by breaking at a certain point, interrupts fire and cause values to be written in certain variables which makes no sense. I guess I will have to take into account these strange occurances when verifying my code. Anyway thanks to all for the help.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Dec 28, 2005 9:29 am     Reply with quote

Quote:
After many frustrating hours, a friend has located an errata sheet.
What is the exact name of the errata sheet you found? The latest errata sheet I found for the PIC18F2410 is Rev.B from 2-2-2005. This document doesn't describe your problem. What am I missing?
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