If you want to use CCP with interrupts and use the libraries, than there is such an example in my blog, which measures the pulse time of a wave. Its written for a 18F1320 but would do your task with some editing.
I have seen your code...and I have used it to measure my signal´s frequency...but...It doesn´t working very good...
I think that I don´t use the interrupts correctly. The frequency is different in each case with the same signal.
This is the code...it´s practically the same that yours...
Thanks for all!!
//This is a program that displays the use of the CCP feature with
//Microchips Libraries documented on page 21.It measures the High and the Total Time
//and sends it to the serial port.
//The maximum Pulse Time is 16.38ms
//The Timer Section is documented on P#57 of C18 Libraries
//Use 181320capture.DSN
//In this simulation a switch has been used, for hardware use a function generator since the debounce time for the //switch will exceed the highest possible time measurement.
#include <p18f4620.h>
#include <delays.h>
#include <capture.h>
#include <stdio.h>
#include <timers.h>
#include <stdlib.h>
#pragma config DEBUG=OFF
#pragma config OSC=XT
#pragma config WDT=OFF
#pragma config LVP=OFF
#pragma config PWRT=OFF
#pragma config MCLRE=ON
#pragma config CCP2MX = PORTBE
unsigned int result,hightime,totaltime,dutycycle;//Variables used
unsigned char start=1;
unsigned char highlow,end;
unsigned float uSxTick = 1.0;
unsigned float f;
unsigned float st=0.0;
/*******************************************************************************************************
/* Interrupcion con direccion 0x08
/***************************************************************************************************** */
void InterruptServiceHigh(void);
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
_asm
goto InterruptServiceHigh //Salta a la direccion de interrupcion ALTA
_endasm
}
/*******************************************************************************************************
/* Interrupcion con direccion 0x18
/***************************************************************************************************** */
void InterruptServiceLow(void);
#pragma code InterruptVectorLow = 0x18
void InterruptVectorLow (void)
{
_asm
goto InterruptServiceLow //Salta a la direccion de interrupcion BAJA
_endasm
}
#pragma interrupt InterruptServiceHigh // Directiva pragma "interrupt" para interrupcion de alta prioridad
void InterruptServiceHigh(void)
{
if(PIR2bits.CCP2IF==1)
{
if(start==1)
{
WriteTimer1(0);//Start timer 1 for maximum count
OpenCapture2( CAPTURE_INT_ON & //Initialize CCP for HtoL
C2_EVERY_FALL_EDGE );
start=0;
highlow=1;
PIR2bits.CCP2IF=0;// Enable interrupt again
}
else
{
if(highlow==1)
{
hightime = ReadTimer1();// Read contents of capture registers
OpenCapture2( CAPTURE_INT_ON & //Initialize CCP for LtoH
C2_EVERY_RISE_EDGE );
highlow=0;
end=1;
PIR2bits.CCP2IF=0;
}
else
if(end==1)
{
totaltime=ReadTimer1();// Read contents of Capture registers
st = uSxTick * totaltime;
f = 1 / (st/1000000);
CloseTimer1();// Close timer 1
CloseCapture2();// Close CCP Module
INTCONbits.GIE=0;//Disbale interrupts
//while(1);// wait here
}
}
}
}
void main(void)
{
OSCCON=0x60;//Internal 4MHz oscillator
ADCON1=0x7f;//Make all pins Digital
TRISE=0x00;//Only for test
LATE=0;
TRISBbits.TRISB3=1;//Make CCP1 pin an input for the pulse
//Initiazlize the capture moudule(ccp) for LtoH
OpenCapture2( C2_EVERY_RISE_EDGE
& CAPTURE_INT_ON );
OpenTimer1( TIMER_INT_OFF &//configure Timer1 for ccp
T1_16BIT_RW &// Can also use Timer3
T1_SOURCE_INT & // Timer1 clock source is internal
T1_SOURCE_CCP &// Make Timer1 source for CCP module
T1_PS_1_1);// No prescaler for timer1
INTCONbits.PEIE=1;// Enable peripheral interrupts
INTCONbits.GIEH=1;// Enable all interrupts
}
/*******************************************************************************************************
********************************************************************************************************
/* Interrupcion con direccion 0x18
/***************************************************************************************************** */
#pragma interruptlow InterruptServiceLow// Directiva pragma "interruptlow" para interrupcion de baja prioridad
void InterruptServiceLow(void)
{
/**********************
/* aqui va el programa que corresponde a la interrupcion de baja prioridad
***********************/
}