chipKIT® Development Platform

Inspired by Arduino™

Input Capture Problem/Question Uno32

Created Fri, 12 Oct 2012 18:21:17 +0000 by uno_decoder


uno_decoder

Fri, 12 Oct 2012 18:21:17 +0000

I've read the current posts about how to use the input capture module but still am having problems. I'm using the uno32 with incap.h If anyone could help I would greatly appreciate it.

I have one input coming in to the IC4 pin that I'm trying to measure the high and low times. With the below code I can measure a signal with a high time of 6ms and low time of 2ms, but can't measure a signal with a high time of 2ms and a low time of 600 us. I know the uno32 is plenty fast enough I must just be missing something. Do I need to call mIC4ReadCapture() from an interrupt?

#include <incap.h>
#include <timer.h>

// Variables
int index_num;
unsigned int Captures[200];

////////////
// Setup //
////////////
void setup() 
{
  // Init Vars
  index_num = 0;
  
  // Debug Port
  Serial.begin(115200);
    
  // Open Timer2 for Input Capture
  OpenTimer2(T2_ON | T2_PS_1_64, 0xFFFF);

  // Enable Input Capture Module 4
  // - Capture Every edge
  // - Use Timer 2 source
  // - Capture rising edge first
  OpenCapture4(IC_SP_EVERY_EDGE | IC_TIMER2_SRC | IC_FEDGE_RISE | IC_ON);  
}

/////////////////
// Main Loop //
/////////////////
void loop() 
{
  int x;
  unsigned long result;
  
  while (!mIC4CaptureReady());
  
  //Now Read the captured timer value
  while (mIC4CaptureReady())
  {
    Captures[index_num++] = mIC4ReadCapture();
  }

  // Print after so many edges are seen for a test as
  // to not interfere with the capture. 
  if (index_num >= 100)
  {
     for (x=0;x<index_num;x++)
     {
       if (x % 2)
         Serial.print("Low  ");
       else
         Serial.print("High ");
       
       // Account for rollover
       if (Captures[x+1] < Captures[x])
       {
         result = (0xFFFF + Captures[x+1]);
         result -= Captures[x];   
       } 
       else
         result = (Captures[x+1] - Captures[x]);
       
       Serial.println(result); 
     }   
     index_num = 0;
  }
}

uno_decoder

Fri, 12 Oct 2012 19:58:20 +0000

Disregard. This was my bad, input is going through optocoupler and signal does not look so good :D