dspic30f4011 input capture problem

Discussion in 'Embedded Systems and Microcontrollers' started by bassplayer142, Jun 18, 2010.

  1. bassplayer142

    Thread Starter Active Member

    Jan 2, 2007
    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

    new = IC1BUF;
    if(new>last) {
    diff = new - last;
    diff = (58590 - last) + new;


    IFS0bits.IC1IF = 0; //Clear bit IC1IF (IFS<1>)

    thanks for any help!
  2. bassplayer142

    Thread Starter Active Member

    Jan 2, 2007
    So I finally found out how to measure this frequency at 331 hz. When I start cranking up the frequency the accuracy starts diminishing fairly high at say 5khz. I am going to need to read frequencies at roughly 5khz-10khz with as good as accuracy as I can with what I got. What is the best way to improve accuracy? Also, if anyone is interested i can post the source code that works.
  3. retched

    AAC Fanatic!

    Dec 5, 2009
  4. bassplayer142

    Thread Starter Active Member

    Jan 2, 2007
    Thanks! I'll definitely take a look. I'm using C but maybe I can pull out some info.