Dspic input captur Problem

Status
Not open for further replies.

anonymosss

Newbie level 1
Joined
May 11, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,306
Hello every one..
Am kind of new at this but i'm supposed to calculate the frequency of an input signal usig DSpic but it didn't work out well for me ...:S..This is my code :


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# include <p33FJ32GP204.h>
#include "delay.h"
unsigned int i;
unsigned int timePeriod= 0; 
unsigned int freq= 0;
unsigned int current_value=0,previous_value=0;
 
#define FCY 7372800
 
 
/***INPUT CAPTURE_SETUP***/
void IC1_SETUP (void)
{
    RPINR7bits.IC1R=0;          /* pin RB0 is chosen as input pin*/
    IC1CONbits.ICM=1;           /*Capture every edge, rising and falling*/
    IC1CONbits.ICTMR = 1;   // Select Timer2 as the IC1 Time base
    IC1CONbits.ICBNE=0;     /*Input capture buffer is empty*/
    IC1CONbits.ICI=0;           /*Interrupt on every capture event*/
    //IC1CONbits.ICTSEL=1;      /*TMR2 contents are captured on captured event*/
    IC1CONbits.ICSIDL=0;        /*Input capture module will continue to operate in CPU Idle mode*/
    IPC0bits.IC1IP = 1;         // Setup IC1 interrupt priority level
    IFS0bits.IC1IF = 0;         /*Interrupt bit is cleared*/
    IEC0bits.IC1IE = 1;         /*Set the IC1 interrupt enable bit */
}
 
/***TIMER_SETUP***/
void TIMER2_SETUP (void)
{
    
    T2CONbits.TON = 0;          //Stops the Timer2 and reset control reg. 
    T2CONbits.TCS=0;            /*Using INTERNAL Clock */
    T2CONbits.T32=0;            /*TMRx and TMRy form a 16-bit timer*/
    T2CONbits.TCKPS=0;          /*Using 1:1 prescale value*/
    T2CONbits.TGATE=0;          /*Timer Gate Accumulation Disabled*/
    T2CONbits.TSIDL=0;          /*Continue in Idle Mode*/
    TMR2 = 0x00;                //Clear contents of the timer register
//  PR2 = 9;                    //Load the Period register with the value 0xFFFF
    IFS0bits.T2IF = 0;          //Clear the Timer2 interrupt status flag
    IEC0bits.T2IE = 1;          //Enable Timer2 interrupts
    T2CONbits.TON = 1;          //Start Timer2
 
}
 
 
/****INTERRUPT FOR IC1****/
// Capture Interrupt Service Routine 
void __attribute__((__interrupt__)) _IC1Interrupt(void)
{   
    //T2CONbits.TON = 1; 
 
    current_value=IC1BUF;
    IFS0bits.IC1IF = 0;
    if(current_value>previous_value)
    { 
        timePeriod =2* (current_value-previous_value); 
    }
    else
    { 
        timePeriod = 2*((PR2 - previous_value) + current_value) ;
    } 
    freq=1000000L/timePeriod;
        previous_value=current_value;
}
 
void delay (void)
{
    int i;
 
    for (i = 0; i < 10000; i++)
    {
    };
 
}
 
 
void main (void)
{
//  AD1PCFGL = 0xFFFF; 
    TRISC = 0b11110100 ;//sets the RB0 & RB1 pin to an output 
    PORTC = 0b00000001 ;//sets the RB0 pin to high 
    delay();
    TRISCbits.TRISC3 = 0;   // RC3 output - U1TX pin
    TRISCbits.TRISC4 = 1;   // RC4 input - U1RX pin 
 
 
 
    TIMER2_SETUP();         /*Calling the Timer Setup Function*/
 
    IC1_SETUP();            /*Calling the Input Setup Function*/
 
    while(1)
    {
 
        
        if(freq==50)
        {
            delay();
            PORTC = 0b00000010 ;//sets the RB1 pin to high          
            delay();
            PORTC=0;
        }
    }
}



I've done a small isis schematic to test it ...the problem is that when running ....the code get stuck in "if(freq==50)" which means that it didn't enter to the IC1 interrupt at all ....how can i solve this ...pleaase help ..:S.. View attachment sds.rar
 
Last edited by a moderator:

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…