4 Replies Latest reply on Mar 9, 2018 2:10 AM by nidh

    Timer_1_ReadCounter equivalent in PSoC Creator 4.2

    jonnymovo_3144201

      Hi I am attempting to port an example for an Ultrasonic Sensor to PSoC Creator 4.2 and the Timer component is a bit different in 4.2.  The example calls out Timer_1_ReadCounter and Timer_1_Stop, but I am unable to locate an API for these in PSoC 4.2.  Also, there is no Timer only component but instead there is a Timer/Counter component so I am trying to understand what I need to change to get the example at the link below working in PSoC Creator 4.2.

       

      This is the link where I found the example:

      https://eewiki.net/display/microcontroller/Interfacing+a+Three+Wire+Ultrasonic+Sensor+to+the+PSOC+5LP

       

      Cheers,

       

      Jon

        • 1. Re: Timer_1_ReadCounter equivalent in PSoC Creator 4.2
          nidh

          Hello,

           

          Are you trying to port that PSoC 5LP project to PSoC 6 ? In that case, please note that UDB based timer / counter components used in that PSoC 5LP project are not yet available for PSoC 6 with PSoC Creator 4.2. These will be added in a future update. Currently, you can use the TCPWM based timers and counters. However, the component settings and APIs are different for TCPWM counters / timers, compared to UDB based counters / timers.

           

          You can learn the TCPWM settings and APIs by:

          1. Drag and drop a Timer Counter (TCPWM) component to the schematic

          2. Right click and select "Open PDL Documentation" / "Open Datasheet"

          3. See the code examples below for more details:

           

          Please let me know if you have any additional questions.

           

          Regards

          Nidhin

          • 2. Re: Timer_1_ReadCounter equivalent in PSoC Creator 4.2
            jonnymovo_3144201

            Nidhin,

             

            I appreciate the reply and for your suggestions.  Yeah, I have added the Timer/Counter component from PSoC Creator 4.2 and made the adjustments to the code in the  Conventional Example, however I am still unable to get the appropriate timer value to calculate the distance from the Sensor. If I add a delay before  the 'counts' value is collected and it does indicate a difference in the timer value that was read, but this does not correspond to the time read from the Ultrasonic Sensor.

             

            This is what I added with PSoC 4.2 adjustments

             

                    CyDelayUs(100);
                   
                    //counts = 0xFFFF-ReadCounter();
                    counts = 0xFFFF-Timer_1_GetCounter();
            

             

            I'm assuming GetCounter is equivalent to ReadCounter since I could not find a ReadCounter interface.

             

            I've attached the project that I have thus far.  It seems close and all I need is to get a proper timer value to indicated the distance read from the read of the sensor.

             

            Thanks,

             

            Jon

            • 3. Re: Timer_1_ReadCounter equivalent in PSoC Creator 4.2
              jonnymovo_3144201

               

              Perhaps I am looking at this incorrectly.  If I change the delay value before performing GetCounter the value of counts as listed previously changes in line with the value of the delay.  So, I'm thinking the issue lies in the reading from the Ultrasonic Sensor where there is no delay between the two pin reads.  I've made some adjustments to the example code and decided to take a reading before the first sensor reading then again after the second reading and just use the diff in the readings to calculate the value of feet.  This seems to work and I am now able to get the red LED to light when an obstacle is within around 1 foot of the sensor.

               

              This is what the main looks like now:

               

              #include "project.h"
              
              
              uint16 counts = 0;
              float  feet   = 0;
              uint16 i      = 0;
              uint16_t led_set = 0;
              uint16_t sensorInput = 0;
              uint32 counter_high = 0;
              uint32 counter_low = 0;
              int main(void)
              {
                  __enable_irq(); /* Enable global interrupts. */
                  /* Enable CM4.  CY_CORTEX_M4_APPL_ADDR must be updated if CM4 memory layout is changed. */
                  //Cy_SysEnableCM4(CY_CORTEX_M4_APPL_ADDR); 
              
              
                  /* Place your initialization/startup code here (e.g. MyInst_Start()) */
                  //Cy_TCPWM_Counter_Init(Timer_1_HW, Timer_1_CNT_NUM, &Timer_1_config);
                  Timer_1_Init(&Timer_1_config);
              
              
                  //Cy_GPIO_Write(LED_0_PORT, LED_0_NUM, 1);
                  for(;;)
                  {
                      /* Place your application code here. */
                      
                      /* Write code */
              
              
                      Cy_GPIO_Write(Pin_1_0_PORT, Pin_1_0_NUM, 1);
                      CyDelayUs(10);
                      Cy_GPIO_Write(Pin_1_0_PORT, Pin_1_0_NUM, 0);
              
              
              
              
                      Timer_1_Start();
                      //CyPins_ReadPin(Pin_1_0_NUM);
                      //while(Pin_1_Read() != 1) //wait until return pulse starts
                      while(Cy_GPIO_Read(Pin_1_0_PORT, Pin_1_0_NUM) != 1) //wait until return pulse starts
                      //while(Cy_GPIO_Read(Pin_1_0_PORT, Pin_1_0_NUM) != 0) //wait until return pulse starts
                      {
                     
                      }
              
              
                      counter_high = Timer_1_GetCounter();
                      
                      Timer_1_Start();
                      
                      //while(Pin_1_Read() != 0) //wait unit return pulse stops  
                      while(Cy_GPIO_Read(Pin_1_0_PORT, Pin_1_0_NUM) != 0) //wait unit return pulse stops 
                      {  
                         
                      }
                      
                      counter_low = Timer_1_GetCounter();
                      
              
              
                      counts = counter_low - counter_high;
                      
              
              
                      //counts = 0xFFFF-Timer_1_GetCounter();
                      
                      feet = counts * 0.00056325; //Convert pulse width to feet, to convert to meters, multiply by 0.0001717 instead. 
                       
                      if (feet < 1.0 )
                      {
                          //LED_Write(1); 
                          Cy_GPIO_Write(LED_0_PORT, LED_0_NUM, 0);
                               
                      }
                            
                      else
                      {
                         
                          Cy_GPIO_Write(LED_0_PORT, LED_0_NUM, 1);
              
              
                      }
                           
                           
                      CyDelay(100);
              
              
                  }
              }
              
              
              /* [] END OF FILE */
              

               

              Perhaps I am looking at this incorrectly.  If I change the delay value before performing GetCounter the value of counts as listed previously changes in line with the value of the delay.  So, I'm thinking the issue lies in the reading from the Ultrasonic Sensor where there is no delay between the two pin reads.  I've made some adjustments to the example code and decided to take a reading before the first sensor reading then again after the second reading and just use the diff in the readings to calculate the value of feet.  This seems to work and I am not able to get the red LED to light when an obstacle is within around 1 foot of the sensor.

              • 4. Re: Timer_1_ReadCounter equivalent in PSoC Creator 4.2
                nidh

                Hi Jon

                 

                Alternatively, you can reset the counter using Timer_1_SetCounter(0u); after each read, instead of finding the difference of counts. This also avoids overflow issues.

                 

                Regards

                Nidhin