Pic18f4620 captures are not working

General discussion on mikroC.
Post Reply
Author
Message
ademir
Posts: 14
Joined: 18 Feb 2006 09:41

Pic18f4620 captures are not working

#1 Post by ademir » 28 Jul 2010 11:59

Hi All
I want to measure the time difference between two capture(CCP1 and CCP2) inputs .The details are shown
in the gorogram listing below .
A 1Khz TTL square wave is connected to both inputs .When the first rising edge is captured CCP1 interrupts
are disabled and program waits for the second rising edge to capture the rising edge at CCP2 .CCp2 interrupt
is then disabled and the pulse count that represents the time difference between two captures are calculated .
It appears the program captures the CCP1 and CCP2 correctly as observed by usart output but
it registers the output as zero count .
Any ideas , What I am doing wrong ?
Any help will be greatly appreciated .
Cheers
Ad

*EDITED BY ADMIN

Code: Select all

/*Program pic_dual_capture */
// Measures the time between two rising edge captures
/*Capture from rising edge
to rising edge and send to termianl via RS232 */
// Test configuration:
//MCU: PIC18F4620
//MikroC V5
//Oscillator: HS, 20.0000 MHz

//global variables
char capture = 0;
unsigned told, tnew ;
char edge=0;
//unsigned tword ;
char txt[5] ;
char str1[]="pulse count=";
char str2[]="Time in micro sec=";

/**********************interrupt routine *******************************/
void interrupt() {
if(PIR1.CCP1IF){
PIE1.CCP1IE=0; //Turn off ccp1 ints
PIR1.CCP1IF=0; //Clear ccp1 flag
tOld = (256*CCPR1H)+CCPR1L ; // save ccp1 register value
edge=1 ; //First edge occured
}
if(PIR2.CCP2IF) {
if(edge) { //To make sure second edge always occurs after the first
PIE2.CCP2IE=0; //stop ccp2 ints
PIR2.CCP2IF=0; //Clear ccp2 flag
tnew = (256*CCPR1H)+CCPR1L; // save ccp2 register value
capture=1; //For main to do the calculations while ints pending
edge=0; //To detect the first capture
}
}
}
/***********************************************/
void main() {
char i;
unsigned pulse_count ;

TRISB = 0; // PORTB is output
PORTB = 0; //PORTB outputs= 0
TRISA = 0;
TRISC = 0;
TRISC.F2 = 1; // RC2 CCP1 capture input
TRISC.F1 = 1; //CCP2 capture input
T1CON = 0x01; // timer1 ON, internal clock Fosc/4, no prescaler
// 20/4=5Mhz.5=0.2 micro sec per timer pulse count
T3CON =0X00 ;//CCP1 and CCP2 will share tmr1 . tmr3 is off , not needed 
CCP1CON = 0x05;//Capture every ccp1 rising edge
CCP2CON=0X05 ; //Capture every ccp2 rising edge
INTCON.GIE = 1; //Enable global ints
INTCON.PEIE =1; //Enable peripheral ints
PIE1.CCP1IE = 1; // enable capture1 ints
PIE2.CCP2IE =1 ; //Enable capture2 ints
PIR2.CCP2IF=0; //Clear ccp2 flag
PIR1.CCP1IF=0; //Clear ccp1 flag
usart_init(9600);

while(1){
if(capture){
capture = 0; // Get ready for next capture
// Calculate time of pulse from rising edge to rising edge
pulse_count = ~tOld + tNew+1; //Subtract tOld from tNew
for(i=0; i<11;i++)
usart_write(str1[i]);
WordToStr(pulse_count, txt); // convert to string and
//send to terminal
for(i=0; i<6; i++) usart_write(txt[i]);
Usart_Write(0x0a);
Usart_Write(0x0d);
delay_ms(500);
PIE2.CCP2IE = 1; //Enable ccp2 ints
PIE1.CCP1IE = 1; // enable ccp1 ints

     }
   }
}

Post Reply

Return to “mikroC General”