2 Replies Latest reply on Jun 24, 2020 11:10 PM by AashitaR_11

    Measure the duration of pulse with counter PSoC4200L

    SoEL_4655151

      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: