Measure the duration of pulse with counter PSoC4200L

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
SoEL_4655151
Level 1
Level 1

Hello everybody

I am currently developing an application that measures the pulse duration, using a PSoC counter, I am using a 4200L development card.

On the rising edge, I reload the value of the counter, and on the falling edge I use the interrupts, I read the value of the counter

PS: the duration of the out_pulse pulse is of value 2.04us.

can someone help me correct and move on the code, I read the value of the counter and I expect to get 91 or 92, but I get a much higher value around 130

I attach to this message the .zip project

Thanks in advance,

Soukayna,

Code:

/*******************************************************************************

* Function Name: main

********************************************************************************

*

*  The main function performs the following actions:

*   1. Counter, PWM_2s, PWM_Pulse. and UART are started.

*   2. At the InPulse rising edge the Counter is reloaded (set to 0 value)

*   3. At the InPulse falling edge the Counter value is saved and the width

*      of the input pulse InPulse is calculated

*   4. The UART sends the calculated width value to the PC terminal display

*

*******************************************************************************/

volatile int capture_flag = 0;

volatile uint16_t Counter_value = 0;

CY_ISR(isr_Counter)

{

    uint32_t source;

    isr_Counter_Disable();

    source = Counter_GetInterruptSource();

  

    if (source & Counter_INTR_MASK_CC_MATCH) {

        capture_flag = 1 ;

        Counter_value = Counter_ReadCounter();

    }

    Counter_ClearInterrupt(source);

}

int main(void)

{

    /* Values used for calculation and captures are initialized */

    //int pulseCaptureCount = 0; 

    //uint32 pulseCaptureCount = 0u; 

       

    char snum[32]; /* for string conversion */

   

    /* Enable the Global Interrupt */

    CyGlobalIntEnable;

    /* Initialize the components */

    PWM_2s_Start();

    PWM_Pulse_Start();

   

    isr_Counter_ClearPending();

    isr_Counter_StartEx(isr_Counter);

    Counter_Start();

   

    UART_Start();

    UART_UartPutString("Pulse duration measurement counter started\r\n");   

   

      

    for(;;)

    {

       

        /* Captures the counter value at the InPulse falling edge */

        //pulseCaptureCount = Counter_ReadCapture();

       

       

        /* Delay for stability... */

        //CyDelay(100);

       

        if (capture_flag) {

            capture_flag = 0;

            /* The counter captured value is printed */

            sprintf(snum, "Measured: %d\n\r", Counter_value);

            UART_UartPutString(snum);

           

            Counter_WriteCounter(0);

            isr_Counter_Enable();

        }

               

    }

     

  } /* [] END OF FILE */

Schematic:

0 Likes
2 Replies
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

How about using

Counter_ReadCapture()

instead of

Counter_ReadCounter()

?

moto

0 Likes
Aashita_R
Moderator
Moderator
Moderator
50 likes received 100 solutions authored 250 replies posted

Hi SoEL_4655151​,

We apologize for our delayed response. Can you please refer to this particular thread of pulse width measurement code - Pulse Width Measurement code ? This will help you modify your project accordingly.

Please go through the thread and let us know if you are facing the issue again.

Best Regards,

Aashita

0 Likes