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 :
# include <p33FJ32GP204.h>#include "delay.h"unsignedint i;unsignedint timePeriod=0;unsignedint freq=0;unsignedint 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