Input Capture Library
Capture Library: (Page # 21)
The capture library can be used to use the CCP/ECCP in the capture mode. One also needs to configure the relevant timer that one wants to use with the CCP/ECCP Module separately. The functions with ?ECapturex are used whenever an ECCP module is implemented in ECCPxCON registers, else use the ?Capturex to initialize the ECCP (such a condition exists in the 18F4455). Before checking out the code, please read through this document which describes how the Capture module in the PIC18 functions:
ww1.microchip.com/downloads/en/DeviceDoc/41214a.pdf
It consists of the following functions:
OpenCapturex(arguments)/ OpenECapturex(arguments)
These functions are used to configure the CCP and ECCP modules in capture mode. x stands for the number of the peripheral(determined by the number of CCPxCON registers. ReadCapturex()/ReadECapturex():
Reads the result of a capture event from the specified (enhanced)capture peripheral. This copies the contents of the CCPRx registers.
CloseCapturex()/CloseECapturex():
Disables the (enhanced)capture module.
The code measures the high time and the total time of a pulse:
Here is the Code(it requires several other files that are present in the attached zipped folder to run):
Code
//This is a program that shows the use of the CCP feature with
//Microchips Libraries documented on P#21.It measures the High and
//the Total Time of a pulse 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 may exceed the highest possible time measurement.
#include<p18f1320.h>// Include files and definitions for the Processor
#include<capture.h>//Include function definitions for the Capture library
#include<timers.h>// Include function definitions for Timer Library
#include <usart.h>// Include function definitions for USART Library
#include<stdlib.h>// Include function definitions for string manupulation Library
#pragma config LVP=OFF,OSC=INTIO2,WDT=OFF// Configuration settings for 18f1320
unsigned int result,hightime,totaltime,dutycycle;//Variables used
unsigned char start=1;
unsigned char highlow,end;
char str[5];
void Send(void);// Function for sending data to serial port
void chk_isr(void);// ISR Handler
void hi_prioriint(void)// ISR Routine
{
_asm
GOTO chk_isr
_endasm
}
#pragma interrupt chk_isr
void chk_isr(void)
{
if(PIR1bits.CCP1IF)
{
if(start==1)
{
WriteTimer1(00);//Start timer 1 for maximum count
OpenCapture1( CAPTURE_INT_ON & //Initialize CCP for HtoL
C1_EVERY_FALL_EDGE );
start=0;
highlow=1;
PIR1bits.CCP1IF=0;// Enable interrupt again
}
else
{
if(highlow==1)
{
hightime = ReadCapture1();// Read contents of capture registers
OpenCapture1( CAPTURE_INT_ON & //Initialize CCP for LtoH
C1_EVERY_RISE_EDGE );
highlow=0;
end=1;
PIR1bits.CCP1IF=0;
}
else
if(end==1)
{
totaltime=ReadCapture1();// Read contents of Capture registers
CloseTimer1();// Close timer 1
Send();// Send the measured time values to the serial port
}
}
}
}
void main(void)
{
OSCCON=0x60;//Internal 4MHz oscillator
ADCON1=0x7f;//Make all pins Digital
//Initiazlize the capture moudule(ccp) for LtoH
OpenCapture1( C1_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
//Initialize USART for tx high time,total time
OpenUSART(USART_TX_INT_OFF &// Trasnmit interrupt off
USART_ASYNCH_MODE & // Use USART in asynchronous mode
USART_EIGHT_BIT &// Eight bits for data
USART_CONT_RX &// Enable continous receiving
USART_BRGH_LOW,// Dont use baud rate multiplier
12);// SPBRG value for a BR of 4800
INTCONbits.PEIE=1;// Enable peripheral interrupts
INTCONbits.GIEH=1;// Enable all interrupts
while(1);// wait here
}
void Send(void)// For sending the times
{
putrsUSART ("\nThe High Time in us is");
ultoa(hightime,str);// convert integer in to string
putsUSART(str);// Send string to serial port
putrsUSART ("\nThe total time in us is");
ultoa(totaltime,str);// convert integer in to string
putsUSART(str);// Send string to serial port
CloseCapture1();// Close CCP Module
INTCONbits.GIE=0;//Disbale interrupts
while(1);// wait here
}
The Code for the Input Capture functions can also be downloaded from: