I am including my code for you all to have a look ! I am trying to capture input frequency of 122 Khz, my PIC internal clock is 8MHZ. I get nothing with this code as of now. What am I doing wrong? :roll:
bit 3-0 CCPxM3:CCPxM0: CCP Module x Mode Select bits
0100 = Capture mode, every falling edge
0101 = Capture mode, every rising edge
0110 = Capture mode, every 4th rising edge
0111 = Capture mode, every 16th rising edge
Well I had it to capture at every rising edge(0x05) and CCPR1 data reg was incrementing with Timer1. Having it set to 4 , gives me nothing as well but CCPR1 data reg does not increment with Timer 1. It stays at 0.
#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#define LED LATEbits.LATE0void main(){unsignedinttime;unsignedfloat uSxTick =1.0;unsignedfloat f;unsignedfloat st=0.0;
ADCON1=0XFF;
TRISBbits.TRISB3=1;
TRISEbits.TRISE0=0;// Configure Timer1
OpenTimer3( TIMER_INT_OFF &
T3_SOURCE_INT &
T3_PS_1_8);while(PORTBbits.RB3==1);//Wait for end of previous pulsewhile(PORTBbits.RB3==0);//Wait for start of new pulse
WriteTimer3(0);//Clear Timerwhile(PORTBbits.RB3==1);//Wait for end of new pulsewhile(PORTBbits.RB3==0);//Wait for start of next pulsetime=ReadTimer3();//read timer
st = uSxTick *time;
f =1/(st/1000000);}
#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#define LED LATEbits.LATE0void main(){unsignedinttime;unsignedfloat uSxTick =1.0;unsignedfloat f;unsignedfloat st=0.0;
ADCON1=0XFF;
TRISBbits.TRISB3=1;
TRISEbits.TRISE0=0;// Configure Timer1
OpenTimer3( TIMER_INT_OFF &
T3_SOURCE_INT &
T3_PS_1_8);while(PORTBbits.RB3==1);//Wait for end of previous pulsewhile(PORTBbits.RB3==0);//Wait for start of new pulse
WriteTimer3(0);//Clear Timerwhile(PORTBbits.RB3==1);//Wait for end of new pulsewhile(PORTBbits.RB3==0);//Wait for start of next pulsetime=ReadTimer3();//read timer
st = uSxTick *time;
f =1/(st/1000000);}
You're right, it could be done this way, however it is compiler dependent(some will allow you to do it others don't). If you don't specify you want the calculation made using float you will end up with let's say 430 / 1000000 = 0 in integer math. Don't forget that float arithmetic in MCU is code space expensive.