I am trying to calculate the frequency of an incoming signal on the dspic30f4011 micro. I have setup a functioning pwm signal to test the incoming signal on. I believe my code is picking up the difference between the time captures of the signal, but I'm not sure how to calculate the frequency from there. I am running off the 120MHz primary FRC clock X16 multiplier which is futhur divided by 4 for the real clock. Any help is greatly appreciated.
I have found that PR2 of 58590 gives a period of timer 2 of 500ms which I verified with switching a simple led on and off from the timer2 interrupt.
(Please be aware that this code was copied and pasted below out of mutliple c files)
For the output on my lcd scree (diff) I am getting 5273.
The Pwm frequency was checked with pickit2 logic tool at 331Hz.
TRISD = 1; //Select pin IC1 (RD0) as input
IPC0 = IPC0 | 0x0010; //Interrupt priority level IC1IP<2:0> = 1
IEC0 = IEC0 | 0x0002; //Interrupt Input Compare module enable
PR2 = 58590; //ffffPR2 register at maximum, timer2 free-running
T2CON = 0x8030; //8030Timer 2 operates with prescaler 1:256 and internal clock
IC1CON = 0x0083; //2 orig Configuration of Input Capture module 1, selected TMR2,
unsigned int new,last,freq,diff = 0;
void _ISR _IC1Interrupt(){ //read freq
last=new;
new = IC1BUF;
if(new>last) {
diff = new - last;
}
else{
diff = (58590 - last) + new;
}
lcd_line1(0);
lcd_int(diff,5);
IFS0bits.IC1IF = 0; //Clear bit IC1IF (IFS<1>)
}
thanks for any help!
I have found that PR2 of 58590 gives a period of timer 2 of 500ms which I verified with switching a simple led on and off from the timer2 interrupt.
(Please be aware that this code was copied and pasted below out of mutliple c files)
For the output on my lcd scree (diff) I am getting 5273.
The Pwm frequency was checked with pickit2 logic tool at 331Hz.
TRISD = 1; //Select pin IC1 (RD0) as input
IPC0 = IPC0 | 0x0010; //Interrupt priority level IC1IP<2:0> = 1
IEC0 = IEC0 | 0x0002; //Interrupt Input Compare module enable
PR2 = 58590; //ffffPR2 register at maximum, timer2 free-running
T2CON = 0x8030; //8030Timer 2 operates with prescaler 1:256 and internal clock
IC1CON = 0x0083; //2 orig Configuration of Input Capture module 1, selected TMR2,
unsigned int new,last,freq,diff = 0;
void _ISR _IC1Interrupt(){ //read freq
last=new;
new = IC1BUF;
if(new>last) {
diff = new - last;
}
else{
diff = (58590 - last) + new;
}
lcd_line1(0);
lcd_int(diff,5);
IFS0bits.IC1IF = 0; //Clear bit IC1IF (IFS<1>)
}
thanks for any help!