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.

Completely wrong input capture calculations

Status
Not open for further replies.

electronicsman

Full Member level 5
Joined
May 4, 2012
Messages
291
Helped
11
Reputation
22
Reaction score
12
Trophy points
1,298
Activity points
3,737
I have completely messed up the input capture calculations. This is my program and really do not know how to proceed, this is the code
Code:
int main(void)
{
    Init_Timer3();
    inputcapture_init();
    while(1);
}
void Init_Timer3(void)
{
 IFS0bits.T3IF		= 0;		
 IEC0bits.T3IE		= 1;		
 T3CONbits.TON		= 1;		
 T3CONbits.TCKPS    = 1;      
}

void inputcapture_init(void)
{ 
 ANSELCbits.ANSC0=0;
 _TRISC0=1;
 RPINR7 = 0x30; // MAP_TO_RP48; 
 IC1CON1bits.ICM = 3; /* every rising edge */
 IC1CON1bits.ICI = 0; /* every capture event */
 IC1CON1bits.ICTSEL = 0; /*TIMER3_IC1;*/
 IFS0bits.IC1IF = 0; //RESET_IC1_STATUS_FLAG;	 		// Clear Input Capture1 Flag request
 IEC0bits.IC1IE = 1;//ENABLE_IC1_INTERRUPT;		// Enable Input Capture1 Interrupt request
}

void __attribute__((__interrupt__,__no_auto_psv__)) _IC1Interrupt(void)
{
    previous_capturedata= present_capturedata; /*copy the present value to previous value */
    present_capturedata = IC1BUF; /*Load the register value */
    if(timer3overflowcounter > 0)
   {
     period =   (65535 - previous_capturedata) + present_capturedata + (timer3overflowcounter -1)*65535;
   }
   else
   {
      if(present_capturedata > previous_capturedata)
      {
         period = present_capturedata -  previous_capturedata; 
      }
      else if(present_capturedata < previous_capturedata)
      {
        period = 65535 -  previous_capturedata + present_capturedata;
      }
   }
    timer3overflowcounter=0;
    IFS0bits.IC1IF=0;
}

void __attribute__ ((__interrupt__, no_auto_psv)) _T3Interrupt(void)
{
   timer3overflowcounter++;
    IFS0bits.T3IF = 0;    
}
i have completely lost the sync of timer3overflowcounter. When should i start the Timer3 timer and stop? The period calculations is wrong. The controller is dspic33ev256gm106. Please help.
 

Hi,

For capture mode you don't need to start/stop the timer. It often causes more problems.
Maybe you should start with a high value prescaler

Klaus
 

Small clarification let us say my clock frequency is 20 mhz and to the input capture module the clock source is clock frequency divided by 8. So the input is 2.5mhz. Each tick corresponds to 1/2.5mhz or 0.4 microseconds. The total time it can measure will be 65535*0.4microseconds = 0.026214 seconds or 3.82 hz. Now my doubt is if i give 1hz, how should i know it? Please help me. My requirement is from 50hz to 1khz. My only fear is the frequency starts from 1 hz. I should not assume this 1hz or unmeasurable range to be in valid range.
 

Hi,
The total time it can measure will be 65535*0.4microseconds = 0.026214 seconds or 3.82 hz.
--> 38.147Hz (about ten times what you wrote)
**

please use "M" for mega
and "m" for milli

****
Now my doubt is if i give 1hz, how should i know it? Please help me. My requirement is from 50hz to 1khz.
If your valid range is 50Hz...1kHz, then 1Hz is not valid.
How to know:
usually when the timer overflows, then it may generate an interrupt. This is where you can know it.
If there is one overflow, then set a flag.
When there is a second overflow, then set measuremtn as invalid.
Clear flags when you start measurement and/or after you safed the capture value in the capture_ISR.

Klaus
 
Thank you now i understand. Just thinking why input capture module does not provide a feature to identify this overflow without writing additional code. Also i am running a parallel timer of 4 seconds if the frequency input is available or not. Is it possible to know immediately that frequency signal is not coming?
 
Last edited:

Hi,

Immediate?
No, because it can not look into the future to see if there will be an edge.

You specified input freuency from 50Hz...
Then you need at least 20ms. For sure you may set a 20ms "compare" value from the first edge..
But now you said 1Hz --- it takes at least 1000ms
But maybe you ask "what about 0.02 Hz" ... then you need to wait 50s...
and so on.

Klaus
 

I am able to read the frequency if it is constant. But i feel that whenever the frequency is changing very fast i feel that i am reading a huge invalid value of frequency. Just need one clarification if the frequency is changing from let us say 100Hz to 125Hz or something else how does it change? I mean to say it transitions normally? Please help. I am not sure if the question makes any sense.
 

Hi,

us say 100Hz to 125Hz or something else how does it change?
it changes according the input signal

Therefore I assume your input signal is not clean.

Klaus
 

My problems do not seem to end. This is the code
Code:
void __attribute__((__interrupt__,__no_auto_psv__)) _IC1Interrupt(void)
{
g_interrupt_Count++;					//Number Input Capture counts
if(g_interrupt_Count == 1)
{
    g_timer_first_edge_u16 = IC1BUF;
    g_timer3overflowcounter_u8=0;
}
else if(g_interrupt_Count == 2)
{
 g_timer_second_edge_u16  = IC1BUF;
 if(g_timer3overflowcounter_u8 < 2)
 {
  g_period_u16 = g_timer_second_edge_u16 - g_timer_first_edge_u16;
 }
 g_interrupt_Count=0;
}		
  IFS0bits.IC1IF=0;
}

void Init_Timer3(void)
{
 IFS0bits.T3IF		= 0;		
 IEC0bits.T3IE		= 1;	
 T3CONbits.TCKPS    = 1;  
 T3CONbits.TON		= 1;		
}

void __attribute__ ((__interrupt__, no_auto_psv)) _T3Interrupt(void)
{
    g_timer3overflowcounter_u8++;
    IFS0bits.T3IF = 0;    
}
Is there is any flaw in this code? The T3 interrupt is the overflow interrupt. I am operating at 20MHz (Fcy).
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top