After much searching and reading of data sheets I have been having a lot of trouble getting input capture to work. This is on the fusion V7 board with a pic32mx795F512L using RD8 (ic1). The actual input signal that will be used is a 30KHz 25% duty cycle square wave but everything from 1 Hz to 150 KHz has been tried. The timers have been tested from 1us to 25 ms with no success. Attempts have been made with both a ISR procedure and simply identifying the interrupt flag and acting upon it. The last attempt was to combine timer2 and 3 for a 32 bit timer and setup the capture to use that but it did not work either. Running the timers without the interrupt enabled was tried but no go.
There are commented procedures which were left in to show some of the testing that has been done.
At the moment it seems the ISR procedure constantly triggers and it's showing small numbers on the screen that do not vary with the input signal. The code is based on many examples found around the net but it seems that something is missing or configured incorrectly and I'm at a loss.
Any help or suggestions are appreciated greatly.
Thanks,
Colin
Code: Select all
/*
* Project name:
CaptureTest.mcp32
* Generated by:
Visual TFT
* Date of creation
2021-09-23
* Test configuration:
MCU: P32MX795F512L
http://www.microchip.com/wwwproducts/Devices.aspx?product=PIC32MX795F512L
Dev.Board: EasyPIC_Fusion_v7_for_PIC32_9A
Oscillator: 80000000 Hz
SW: mikroC PRO for PIC32
http://www.mikroe.com/mikroc/pic32/
*/
#include "CaptureTest_objects.h"
int cap_flag = 0;
unsigned long data1, data2, t3count, t2count;
char txt1[12];
char txt2[12];
//Timer2
//Prescaler 1:16; PR2 Preload = 50000; Actual Interrupt Time = 10 ms
void InitTimer2(){
T2CON = 0x8040;
//T2CONbits.T32 = 1;
T2IP0_bit = 1;
T2IP1_bit = 1;
T2IP2_bit = 1;
T2IF_bit = 0;
T2IE_bit = 1;
PR2 = 50000;
TMR2 = 0;
}
void Timer2_interrupt() iv IVT_TIMER_2 ilevel 7 ics ICS_SRS {
T2IF_bit = 0;
t2count++;
if (t2count == 100) {
PORTGINV = 0b0000000001000000; // heartbeat on pin G6
t2count = 0;
}
}
//Timer3
//Prescaler 1:16; PR3 Preload = 50000; Actual Interrupt Time = 10 ms
void InitTimer3(){
T3CON = 0x8040;
T3IP0_bit = 1;
T3IP1_bit = 1;
T3IP2_bit = 1;
T3IF_bit = 0;
T3IE_bit = 1; //=0; tried both
PR3 = 50000;
TMR3 = 0;
}
void Timer3_interrupt() iv IVT_TIMER_3 ilevel 7 ics ICS_SRS {
T3IF_bit = 0;
/*t3count++;
if (t3count == 100) {
PORTGINV = 0b0000000001000000; // heartbeat on pin G6
t3count = 0;
}*/
}
void Init_Capture() { // pin RD8 injecting 30 KHz 3V square wave 25% duty cycle offset 2V
//IC1CON = 0x8364; // 0b00000000000000001000001101100100; ON/1, 14/0, SIDL/0, 12-10/0, FEDGE/1, C32/1, ICTMR/0, ICI<1:0>/11, ICOV/0, ICBNE/0, ICM<2:0>/100
IC1CONbits.ICTMR = 0; // use timer 3
//IC1CONbits.C32 = 1;
IC1CONbits.ICM0 = 1; // capture every 4th rising edge
IC1CONbits.ICM1 = 0;
IC1CONbits.ICM2 = 0;
IC1CONbits.FEDGE = 1; // capture rising edge
IC1CONbits.ICI0 = 1; // interrupt every 4th capture event
IC1CONbits.ICI1 = 1;
IEC0bits.IC1IE = 1; // enable IC1 interrupt
IC1CONbits.ON = 1; // turn IC1 on
}
/*void IMU_interrupt() iv IVT_INPUT_CAPTURE_1 ilevel 7 ics ICS_SRS { // ICS_OFF {
PORTGINV = 0b0000000010000000;
cap_flag = 1;
//IFS0bits.IC1IF = 0; // clear interrupt flag
IFS0.IC1IF = 0;
}*/
void main() {
AD1PCFG = 1; // digital mode
//CM1CON = 0; // disable Compare module 1
//CM2CON = 0; // disable Compare module 2
TRISD = 0b0000000100000000; // PORTD8 as input
TRISG = 0b0000000000000000; // PORTG as output
LATG = 0b0000000000000000; // PORTG off
InitTimer2(); // timers init
InitTimer3();
Init_Capture(); // capture init
EnableInterrupts(); // Enable interrupts
Start_TP();
while (1) {
//Check_TP();
while(!IFS0.IC1IF); // wait for interrupt
if(!(!ICBNE_bit)) // if capture buffer is not empty
{
IFS0.IC1IF = 0; // clear interrupt flag
//PORTGINV = 0b0000000010000000; // some indication on g7 led
data1 = IC1BUF; // read value from IC1BUF
}
while(!IFS0.IC1IF); //
if(!(!ICBNE_bit)) //
{
IFS0.IC1IF = 0; //
//PORTGINV = 0b0000000010000000; // some indication on g7 led
data2 = IC1BUF;
}
/*if (cap_flag = 2) { // alternate method, used with interrupt procedure
data2 = IC1BUF;
cap_flag = 0;
}
if (cap_flag = 1) {
data1 = IC1BUF;
cap_flag = 2;
}*/
LongToStr(data1,txt1);
LongToStr(data2,txt2);
strcpy(Button1.caption, ltrim(txt1));
strcpy(Button2.caption, ltrim(txt2));
DrawButton(&Button1);
DrawButton(&Button2);
delay_ms(50);
}
}