Input Capture on PIC32MX

General discussion on mikroBasic PRO for PIC32.
Post Reply
Author
Message
feuerwolf
Posts: 95
Joined: 09 Nov 2012 16:00
Location: Switzerland

Input Capture on PIC32MX

#1 Post by feuerwolf » 28 Jan 2015 20:04

Input Capture on PIC32MX

Hi all,
for a quadcopter project i work with your IMU Click
http://www.mikroe.com/click/mpu-imu/
In order to be able to calculate the correct Angle from the agyroscope, I need the most exact time difference between two measurements. Every time the IMU has a new Value in its Buffer, it shows a short High peak on its Interrupt output.
So far so good.
Now I wanted to use the Input Capture Module 1 of the PIC32 to get an Interrupt every time there is a new value in the IMU. Further I want to get the time (cycles of the CPU) to be able to calculate the time difference.

I found a example on Mikroe Page, which I followed pretty close.
http://www.mikroe.com/chapters/view/53/ ... t-capture/

Here come the Problem:
For some reason I can’t get the module work properly. The interrupt routine works, but I don’t get a proper value in the Cycle count Register IC1BUF. I tried all Prescalers in the timer config register, But Everytime the CPU jumps into the Interrupt routine the IC1BUF shows random values, the ICOV (Input Capture Overflow Status Flag bit ) is always 1 , and I cant reset the IC1IF_bit.
According to the manual, reading the IC1BUF should reset the Input Capture Module for a new cyle.

I Know the output Rate of the IMU is 0.020 sec / 50Hz . That’s what I want to measure. Therefor a prescaler of 256 should be far enough, but I get an overflow in the input capture for any prescaler I use in Timer 3
Could somebody please have a look at my testcode and give me a hint?
Or does somebody have a working example code for the PIC32?

tried pretty hard to make it run and I am pretty sure that i am really close, it might just only a bit in a register i don't recognise .... :?

I found a workaround for pic16 in this project:
http://www.libstock.com/projects/view/1 ... ency-meter

But it doesn't use the Input capture as it is meant to be and as it is described in the PIC32 manual in order of using the Timer and reading the Buffer. At least as far as i understand the manual....

Thanks a lot

Code: Select all


//Timer3
void InitTimer3(){

  /*TCKPS<2:0>: Timer Input Clock Prescale Select bits
111 = 1:256 prescale value
110 = 1:64 prescale value
101 = 1:32 prescale value
100 = 1:16 prescale value
011 = 1:8 prescale value
010 = 1:4 prescale value
001 = 1:2 prescale value
000 = 1:1 prescale value
*/



  T3CON             = 0x8070;;    // Prescaler 1:256;
  T3IE_bit          = 0;
  T3IF_bit          = 0;
  T3IP0_bit         = 1;
  T3IP1_bit         = 1;
  T3IP2_bit         = 1;
  PR3               = 0xffff;
  TMR3              = 1;   // Time enable , Interrupt disabled
  ON__T3CON_bit     = 1;
}



void initIMU_input_capture_interrupt(){

  // IC1CON bits
  SIDL_IC1CON_bit = 1;  // 1 = Halt in CPU IDLE mode
  C32_bit   = 0;    // 0 = 16-Bit timer resource capture
  ICTMR_bit = 0;    // 0 = Timer3 is the counter source for capture

  ICI1_bit = 0;     // interrupts generated for every event
  ICI0_bit = 0;     // When ICI = 00 interrupt events occur regardless of FIFO overrun

  ICM2_bit = 0;     // 011 = Simple Capture Event mode – every rising edge
  ICM1_bit = 1;
  ICM0_bit = 1;

  IC1IP2_bit = 1;   // Input Capture 1 Interrupt Priority bits
  IC1IP1_bit = 1;
  IC1IP0_bit = 1;

  ON__IC1CON_bit = 1;  // Interrupt enable
  IC1IE_bit  = 1;      //Interrupt Input Capture module enable

}


float dt_sec_float;
void IMU_interrupt() iv IVT_INPUT_CAPTURE_1 ilevel 7 ics ICS_SRS {  // RD8 Pin
  volatile unsigned long dt_buffer = 0;
  volatile unsigned long counts = 0;
  volatile const unsigned long CPU_frequency = 80000000;
  dt_buffer = 0;
  counts = 0;
  
  counts = IC1BUF;
  dt_sec_float = ((float)counts * 256) / (float) CPU_frequency ;    //  256 == prescaler

  IC1IF_bit = 0;
  IFS0.IC1IF = 0;
}


void main() {

  AD1PCFG = 0xFFFF;             // Configure AN pins as digital I/O
  TRISD = 0x0100;               //Select pin IC1 (RD8) as input

  InitTimer3();
  initIMU_input_capture_interrupt();   // input capture Timer

  EnableInterrupts();                // Enable interruts as previously set

  while(1){
  }
}

Post Reply

Return to “mikroBasic PRO for PIC32 General”