This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

IS this Code for timer Capture Correct?

void EF_void_InputCapture_Init(void)
{
    //give clock to it's peripheral
    SysCtlPeripheralEnable(SYSCTL_PERIPH_WTIMER3);
    SysCtlDelay(5);
    //give clock to GPIO peripheral
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    SysCtlDelay(5);
    /* use PD2 as CCP */
    GPIOPinConfigure(GPIO_PD2_WT3CCP0);
    SysCtlDelay(5);

    GPIOPinTypeTimer(GPIO_PORTD_BASE, GPIO_PIN_2);

    TimerConfigure(WTIMER3_BASE, TIMER_CFG_A_CAP_TIME_UP|TIMER_CFG_SPLIT_PAIR);

    TimerControlEvent(WTIMER3_BASE, TIMER_A, TIMER_EVENT_BOTH_EDGES);
    /* pre-scaler */
    TimerPrescaleSet (WTIMER3_BASE, TIMER_A , 64- 1 );
    TimerIntEnable(WTIMER3_BASE, TIMER_CAPA_EVENT );

}


void EF_BOOLEAN_InputCapture_GetPulse(uint32_t* Pulse_ptr)
{
    /* to save the first edge time in it */
    uint32_t Pulse_FirstEdge = 0;
    TimerEnable(WTIMER3_BASE, TIMER_A);

    /* make Input capture unstuck + Wait for the wanted Edge */
    while( (TimerIntStatus(WTIMER3_BASE , 0) != TIMER_CAPA_EVENT ) );

    /* get the time of first edge */
    Pulse_FirstEdge = TimerValueGet(WTIMER3_BASE, TIMER_A);
    /* clear flag by write one in it */
    TimerIntClear(WTIMER3_BASE, TIMER_CAPA_EVENT);

    while( (TimerIntStatus(WTIMER3_BASE,0) != TIMER_CAPA_EVENT ) );

    /* get the pulse by subtract the Second Edge from First Edge*/
    *Pulse_ptr = TimerValueGet(WTIMER3_BASE, TIMER_A);
    *Pulse_ptr = -Pulse_FirstEdge + *Pulse_ptr ;
    /* clear flag by write one in it */
    TimerIntClear(WTIMER3_BASE, TIMER_CAPA_EVENT);

}

  • Hello Ibrahem,

    I am not sure what to make out of the code. Is there anything specific that you want to comment on. Also a code can be used to 10 different things. You have not set the context by telling what the code is supposed to be for.

    Regards
    Amit
  • I used PWM as input to timer capture
    and this is the code (as the manual example)

    	SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);
    	GPIOPinConfigure(GPIO_PF1_M1PWM5);
    	GPIOPinTypePWM  (GPIO_PORTF_BASE ,GPIO_PIN_1 );
    	//prescaler = F/64
    	SysCtlPWMClockSet( SYSCTL_PWMDIV_64);
    	PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN);
    	ui32Load = ( SysCtlClockGet() )/ (PWM_FREQUENCY * PWM_PRESCALE) ;
    	PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, ui32Load );
    	//first configuration
    	// Encoded offset address of PWM5
    	PWMPulseWidthSet (PWM1_BASE , PWM_OUT_5 , ( PWM_100* ui32Load)/100);
    	// Bit-wise ID for PWM5
    	PWMOutputState(PWM1_BASE, PWM_OUT_5_BIT, true);
    	PWMGenEnable(PWM1_BASE, PWM_GEN_2);

    and there are some micros :

    #define PWM_PRESCALE 64
    #define PWM_FREQUENCY 55
    #define PWM_10 10
    #define PWM_50 50
    #define PWM_100 100

    and using it by :

    PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, (50 * ui32Load) / 100);

    the Fpwm make a pulse every 55/2 hz 

  • Hello Ibrahem,

    And how does the MCU know that the edge is a falling or rising edge?

    Regards
    Amit
  • I connected PWM pin to timer Capture pin and make:
    TimerControlEvent(WTIMER3_BASE, TIMER_A, TIMER_EVENT_BOTH_EDGES);
  • Hello Ibrahem,

    I do understand that but the MCU knows only an edge. It does not know if it is a falling followed by rising or the other way around. Since it does not know that you may have 30% Duty Cycle computed in SW when the actual DC is 70%. the correct way would be to use 2 half timers, one for rising edge and other for falling edge and then synchronized using the SYNC feature. So this way it would always been known which timer is giving rising edge time and which one is giving falling edge time count.

    Regards
    Amit
  • you are right
    but in my test I used 50% Pwm and the result is not ok , so the problem still occurred
  • Hello Ibrahem,

    So what is the value that gets computed?

    Regards
    Amit
  • I think there is wrong thing in init. Input capture..
    because I fixed a defined input to input Capture (980 *2 HZ) and changing the prescaler value for Input capture and the results are the same for these different prescalers !!
    every time gives 20480 for all prescallers!
    so Freq = (40*10^(6))/( prescaler* 20480)
    if prescaler = 64 then freq = 30 HZ !!
  • Hello Ibrahem,

    Let me rephrase: What is the PWM being expected to be generated, what is it being measured on a scope as and what is the timer measuring it as. These 3 parameters are critical. Also does every timer read out has the same issue or some of the timer readouts fail.

    Regards
    Amit
  • yes, every timer read out has the same issue . (20480 )
    it was in the scope.

    I make the input PWM 50% duty cycle from AVR kit with 490HZ for the period and 490*2 HZ for the pulse 
    and using 40MHZ system clock and wide timer 3 in Tiva to make Time Capture with prescaler 64 with Port D pin 2
    the result : 20480 counts (time Ticks)

    the right and expected time ticks is 637 count with 64 prescaller

    I noted TimerPrescaleSet didn't effect !!! , and I explored that the calculation is right when prescaler is 1

  • Hello Ibrahem

    Something strange in the calcuations: 637 * 64 / 2 = 20384

    The value read from the timer is 20480.

    Now can you change the value of the signal to say 250Hz and 750Hz and see if the timer readout changes?

    Regards
    Amit
  • all was right when considering that Timer prescaler function didn't work ==> prescaler=1
    but I don't know why this function has no effect inspect of using half width timer (spilt) !!!
  • Hello Ibrahem,

    That is because in Capture Mode the prescalar is an extension of the timer counter. Please check the data sheet section on the register description.

    Regards
    Amit