Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Capture input frequency via CCP module

Status
Not open for further replies.

Neyolight

Full Member level 5
Full Member level 5
Joined
Aug 29, 2011
Messages
306
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
World
Activity points
3,624
Hi all

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:

Thanks

Code Listing Removed at Original Posters Request


Thanks
 
Last edited by a moderator:

You need to enable the interrupt of the CCP module with PIE1bits.CCP1IE = 1; in your PICInit();
 

Well I want to capture the first edge and then enable the interrupt for continuous frequency reading !

---------- Post added at 09:41 ---------- Previous post was at 09:40 ----------

But I will give what you said a go and see what I get !
 

Still not working ! I dont even go into the interrupt ! Or sometimes I get 2 interrupts but the frequency values are way off !

---------- Post added at 12:44 ---------- Previous post was at 11:54 ----------

I just realised while debugging that my CCPR1 data register increments with Timer 1- what is that happening?

CCPR1 should capture the value of Timer1 ONLY during the interrupt !

Oh gosh! PIC programming is such a TORTURE ! :|
 

CCP1CON = 0x05; <- This is to set your timer to output compare.
Change the value of 5 to 4 to be in input capture.
 

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.
 

here is Code I found which is said by the author to be working:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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.LATE0
 
void main(){
 
unsigned int time;
unsigned float uSxTick = 1.0;
unsigned float f;
unsigned float 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 pulse
while(PORTBbits.RB3==0); //Wait for start of new pulse
WriteTimer3(0); //Clear Timer
while(PORTBbits.RB3==1); //Wait for end of new pulse
while(PORTBbits.RB3==0); //Wait for start of next pulse
time=ReadTimer3(); //read timer
st = uSxTick * time;
f = 1 / (st/1000000);
 
 
}

 
Last edited by a moderator:
here is Code I found which is said by the author to be working:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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.LATE0
 
void main(){
 
unsigned int time;
unsigned float uSxTick = 1.0;
unsigned float f;
unsigned float 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 pulse
while(PORTBbits.RB3==0); //Wait for start of new pulse
WriteTimer3(0); //Clear Timer
while(PORTBbits.RB3==1); //Wait for end of new pulse
while(PORTBbits.RB3==0); //Wait for start of next pulse
time=ReadTimer3(); //read timer
st = uSxTick * time;
f = 1 / (st/1000000);
 
 
}

Whats uSxTick here? Could it be the time period of Timer 3?
 
Last edited:

Makes sense now ! Thanks !

Is 'float' essential in the equation you presented? The variable 'time' is an int and can be used directly to get frequency.
 

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.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top