chipKIT® Development Platform

Inspired by Arduino™

Input Capture Problem

Created Tue, 04 Dec 2012 04:29:46 +0000 by markoshh


markoshh

Tue, 04 Dec 2012 04:29:46 +0000

Hi good morning, wrote in this forum, to see if I could help with input capture module of my chipKIT Max32. I need to make the DC motor control by PWM, for that, I need to repower my system, with a reading of the speed of the motor, I have decided to use encoders on the wheels of the vehicle. The problem is that the encoder, when placed in the input capture module, these readings are wrong. I throw random numbers, and it makes me understand that is the problem. The code I used is based on the mplabx help file. The shortest cycle period is: Tmin = 0.0166 Seconds And the maximum time period is: Tmax = 1 Second Which is understood or should be inferred that the car is stopped, and need not feed back the function of PWM. In the tests I've done, I returned random values​​, I suppose, is due to poor scaling, so I guess that must timer overflow.

Apologize for my English, I live in Argentina and I used Google Translator. Below attached the code:

/Programa de prueba para PWM/

/Carga de Librerias/

#include <stdlib.h> #include <plib.h> #include <LCD.h> /Configuracion del pic, y set para bootloader/ #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_8 #pragma config ICESEL = ICS_PGx2

/SET Timer Encoder/ #define FOSC 80E6 #define PB_DIV 8 #define PRESCALE 256 #define MSEC 10E-3 #define T1_TICK (500 * MSEC * FOSC)/(PB_DIV * PRESCALE)

/Frecuencia/ #define SYS_FREQ (80000000L)

//Variables Globales// unsigned int CaptureTime; unsigned int analog1; unsigned int analog2; unsigned int cuenta1; unsigned int cuenta2; unsigned int tiempo; /Variables para Convertir int a char para LCD/ char buffer[10]; int n;

//////////////// //Funcion Main// //////////////// int main(void) { /Configuracion del sistema/ SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

    /////////////////////////CONFIGURACION PWM//////////////////////////////
    ////////////////////////////////////////////////////////////////////////
                    ///COMPARADORES DE PWM DE SALIDA///
                    ///////////////////////////////////
    /*Inicializacion del modulo Comparador OC1 en el pin PWM --&gt;3 De la placa*/
    OpenOC1( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0);
    /*Inicializacion del modulo Comparador OC2 en el pin PWM --&gt;5 De la placa*/
    OpenOC2( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0);
                    ///////////////////////////////////
                    
                       ///COMPARADORES DE ENTRADA///
                    ///////////////////////////////////
    //OpenTimer3(T3_ON | T1_PS_1_256, T1_TICK);
    OpenTimer3( T3_ON | T3_PS_1_1 | IC_CAP_32BIT | T3_SOURCE_INT, 0xFFFFFFFF);
          OpenCapture1( IC_EVERY_16_RISE_EDGE| IC_INT_1CAPTURE | IC_TIMER3_SRC | IC_FEDGE_RISE | IC_ON );
   
    ///TIMER DE REFERENCIA PARA LOS COMPARADORES///
                ////////////////////////////////////////////////
    //ON-OFF//PREESCALER//FUENTE DE CLOCK//CAPACIDAD DEL TIMMER PARA 4kHz//
    OpenTimer2( T2_ON | T2_PS_1_1 | T2_SOURCE_INT, 0x4E20);
                ////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////
   

    /**********************************************************************/
    //////////////////CONFIGURACION DE ENTRADAS ANALOGICAS//////////////////
    ////////////////////////////////////////////////////////////////////////

    /*Cierre del modulo AD antes de configurarlo*/
    CloseADC10();
    /*SET DE PARAMETROS PARA CONFIGURARLO*/
    #define PARAM1  ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
    #define PARAM2  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_2 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF
    #define PARAM3  ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15
    #define PARAM4	ENABLE_AN0_ANA | ENABLE_AN1_ANA
    #define PARAM5	SKIP_SCAN_AN2 | SKIP_SCAN_AN3 |SKIP_SCAN_AN4 |SKIP_SCAN_AN5 | SKIP_SCAN_AN6 | SKIP_SCAN_AN7 | SKIP_SCAN_AN8 | SKIP_SCAN_AN9 | SKIP_SCAN_AN10 | SKIP_SCAN_AN11 | SKIP_SCAN_AN12 | SKIP_SCAN_AN13 | SKIP_SCAN_AN14 | SKIP_SCAN_AN15

    /*Set de tension de referencia (INTERNA--&gt;GND)*/
    SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF);
    /*Carga los 5 parametros de configuracion al set de ADC10*/
    OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 );
    /*Encendido del conversor A/D*/
    EnableADC10();







    ////////////////LCD/////////
    lcd_init();
    putsLCD("Prueba Input Capture");
    lcd_gotoxy(1, 2);
    putsLCD("Tiempo");
    
    Delayms(1000);
    putsLCD("\f");

   
    
    while(1)
{
          

while( !mIC1CaptureReady() ) ;

while( mIC1CaptureReady() ) n=sprintf(buffer, "%d", tiempo); putsLCD(buffer); Delayms(1000); putsLCD("\f"); }

}

////////////////////////////////////////////////////////////