1 2 Previous Next 16 Replies Latest reply on Feb 18, 2020 6:11 AM by eeEn_4618901

    Timer psoc5

    eeEn_4618901

      I want 1 sensor to work for 5 seconds in my project.

      can i do this with the only  timer block?

        • 1. Re: Timer psoc5
          RakshithM_16

          Hi eeEn_4618901,

           

          Can you please provide more details regarding the sensor that you are using?

           

          According to my understanding you need a timer to enable a sensor every 5 seconds. Please let me know if this your requirement.

           

          Did you try referring to the Timer code example for PSoC 5LP?

           

          Thanks and Regards,

          Rakshith M B

          • 2. Re: Timer psoc5
            eeEn_4618901

            Hi Rakshith,

            I am using RF transceiver module.

            I want to receive data while counting timer for 5 seconds in my program. When the timer starts counting, I will start receiving data. I will finish receiving data when the timer has finished counting for 5 seconds.

            Is it possible to do this using only timer?

            Thanks and Regards,

            • 3. Re: Timer psoc5
              AashitaR_11

              Hi eeEn_4618901,

               

               

              Thank you for sharing the information.

              I have shared the code example for Timer using PSoC 5LP. Please refer it for more information.

               

              Also, can you please share the datasheet of the RF transceiver module you have used?

               

              This will help us know what the output of the module is. Based on this we can suggest an alternative if any.

               

               

               

              Thanks and Regards,

              Aashita

              • 4. Re: Timer psoc5
                eeEn_4618901

                Hi Aashita,

                The project you submitted is already a sample project.I have already examined this project.

                Can I determine the start time of the timer?

                For example, the timer starts working when the sw switch is pressed.

                Let the timer light up when the timer starts working. Let the led turn off when the timer is over.

                Thanks and Regards,

                • 5. Re: Timer psoc5
                  MoTa_728816

                  Hi,

                   

                  If what you want is to wait 5 sec,

                  may be you can use SysTick, without consuming more Timers.

                  (Some isr is required though)

                   

                  I tried this with CY8CKIT-059

                  Each time my_tick_timer is started, LED turns on,

                  during the counting, "." is printed every second

                  When 5 sec arrives LED turns off

                  and wait 2 sec before starting again.

                   

                  schematic

                  001-schematic.JPG

                  pins

                  002-pins.JPG

                  main.c

                  ==============

                  #include "project.h"

                  #include "stdio.h"

                   

                  #define LED_ON (1u)

                  #define LED_OFF (0u)

                   

                  #define MY_TICK_PERIOD 5000 // 5sec

                   

                  volatile uint32_t my_tick_count = 0 ;

                  volatile int      timer_5sec_flag = 0 ;

                  int               my_tick_running = 0 ;

                   

                  CY_ISR(my_tick_isr)

                  {

                      if (my_tick_running) {

                          my_tick_count++ ;

                          if (my_tick_count >= MY_TICK_PERIOD) {

                              timer_5sec_flag = 1 ;

                              my_tick_running = 0 ; /* stop my tick but not systick */

                              LED_Write(LED_OFF) ;

                          }

                      }

                  }

                   

                  void start_my_tick(void)

                  {

                      my_tick_count = 0 ;

                      my_tick_running = 1 ;

                      LED_Write(LED_ON) ;

                  }

                   

                  #define STR_LEN 64

                  char str[STR_LEN+1] ;

                  void print(char *str)

                  {

                      UART_PutString(str) ;

                  }

                   

                  void cls(void)

                  {

                      print("\033c") ; /* reset */

                      CyDelay(20) ;

                      print("\033[2J") ; /* clear screen */

                      CyDelay(20) ;

                  }

                   

                  void splash(void)

                  {

                      cls() ;

                      print("5LP Timer 5Sec Test") ;

                      snprintf(str, STR_LEN, "(%s %s)\n", __DATE__, __TIME__) ;

                      print(str) ;

                  }

                   

                  void init_hardware(void)

                  {

                      CyGlobalIntEnable; /* Enable global interrupts. */

                     

                      UART_Start() ;

                      splash() ;

                     

                      CySysTickStart() ;

                      CySysTickSetCallback(0, my_tick_isr) ;

                  }

                   

                  int main(void)

                  {

                      init_hardware() ;

                   

                      print("starting my tick\n\r") ;

                      start_my_tick() ;

                         

                      for(;;) {

                          if (timer_5sec_flag) {

                              print("5sec passed!\n\r") ;

                              CyDelay(2000) ; /* wait 2 sec */

                              print("restarting my tick\n\r") ;

                              timer_5sec_flag = 0 ;

                              start_my_tick() ;

                          }

                          print(".") ;

                          CyDelay(1000) ;

                      }

                  }

                  ==============

                   

                  Tera Term Log

                  000-TeraTerm-log.JPG

                   

                  moto

                  1 of 1 people found this helpful
                  • 6. Re: Timer psoc5
                    eeEn_4618901

                    Hi Moto,

                    Thank you

                    Isn't it possible to do these operations with the timer block?

                    Best regards

                    • 7. Re: Timer psoc5
                      MoTa_728816

                      Hi,

                       

                      > Isn't it possible to do these operations with the timer block?

                      Yes, I think it's possible.

                      But it requires a Timer.

                       

                      The schematic

                      010-schematic.JPG

                      Note:

                      I changed the timer_clock frequency to 10kHz (10000Hz)

                      And the period of Timer to 50000 so that at 5 second interrupt will be triggered.

                       

                      timer_clock Config

                      011-clock_config.JPG

                      Timer Config

                      011-Timer-Config.JPG

                       

                       

                      Pins

                      002-pins.JPG

                      main.c

                      =================

                      #include "project.h"

                      #include "stdio.h"

                       

                      #define LED_ON (1u)

                      #define LED_OFF (0u)

                       

                      volatile int      timer_5sec_flag = 0 ;

                       

                      CY_ISR(timer_exp_isr)

                      {

                          Timer_ReadStatusRegister() ;

                          timer_5sec_flag = 1 ;

                          LED_Write(LED_OFF) ;

                      }

                       

                      void start_timer(void)

                      {

                          Timer_Stop() ;

                          Timer_RestoreConfig() ;

                          LED_Write(LED_ON) ;

                          Timer_Enable() ;

                      }

                       

                      #define STR_LEN 64

                      char str[STR_LEN+1] ;

                      void print(char *str)

                      {

                          UART_PutString(str) ;

                      }

                       

                      void cls(void)

                      {

                          print("\033c") ; /* reset */

                          CyDelay(20) ;

                          print("\033[2J") ; /* clear screen */

                          CyDelay(20) ;

                      }

                       

                      void splash(void)

                      {

                          cls() ;

                          print("5LP Timer 5Sec Test (using a Timer component)") ;

                          snprintf(str, STR_LEN, "(%s %s)\n", __DATE__, __TIME__) ;

                          print(str) ;

                      }

                       

                      void init_hardware(void)

                      {

                          CyGlobalIntEnable; /* Enable global interrupts. */

                         

                          UART_Start() ;

                          splash() ;

                       

                          timer_exp_int_ClearPending() ;

                          timer_exp_int_StartEx(timer_exp_isr) ;

                         

                          Timer_Init() ;

                      }

                       

                      int main(void)

                      {

                          init_hardware() ;

                       

                          print("starting the Timer\n\r") ;

                          start_timer() ;

                             

                          for(;;) {

                              if (timer_5sec_flag) {

                                  print(" 5sec passed!\n\r") ;

                                  CyDelay(2000) ; /* wait 2 sec */

                                  print("restarting the Timer\n\r") ;

                                  timer_5sec_flag = 0 ;

                                  start_timer() ;

                              }

                              CyDelay(1000) ;

                              print(".") ;

                          }

                      }

                      =================

                       

                      Tera Term Log

                      012-TeraTerm-log.JPG

                       

                      Although I wonder why there are 6 dots, as far as I measured with my iPhone's stopwatch the LED is on about 5 second.

                       

                      moto

                      1 of 1 people found this helpful
                      • 8. Re: Timer psoc5
                        eeEn_4618901

                        Hi Moto,

                        When I want my sensor to work for 5 sec , would I just change the code as follows?

                        I want my sensor to work with the timer for 5 seconds.

                        if (timer_5sec_flag) {

                                    print(" 5sec passed!\n\r") ;

                                    CyDelay(2000) ; /* wait 2 sec */

                                    print("restarting the Timer\n\r") ;

                                    timer_5sec_flag = 0 ;

                                    start_timer() ;

                                    sensorWork();

                                }

                        Best Regards

                        • 9. Re: Timer psoc5
                          MoTa_728816

                          Hi,

                           

                          I don't know how "sensorWork()" is supposed to work.

                          If it's not a blocking function your suggested place is correct,

                          but then you need to stop the sensor at the in the "timer_exp_isr" or when timer_5sec_flag is true(non zero).

                           

                          I would suggest

                          (1) Start the sensor after sart_timer()

                          (2) Stop the sensor either in the CY_ISR(timer_exp_isr) or just after "if (timer_5sec_flag) { "

                           

                          So basically

                          The sensor should be started where "LED_Write(LED_ON)" is located.

                          Then the sensor should be stopped where "LED_Write(LED_OFF)" is located.

                           

                          To realize this, you must write your "sensorWork()" as a callback type function,

                          which returns after a  transaction and the main loop to proceed.

                           

                          For example, I would write

                          =====

                          void sensorWork(void)

                          {

                               if (timer_5sec_flag == 0) {

                                  // do single transaction (trigger and receive data from the sensor)

                               }

                          }

                          ====

                           

                          Then I would place the "sensorWork()" to parallel with the "if (timer_5sec_flag) {" block

                           

                          ====

                              for(;;) {

                                  if (timer_5sec_flag) {

                                      print(" 5sec passed!\n\r") ;

                                      CyDelay(2000) ; /* wait 2 sec */

                                      print("restarting the Timer\n\r") ;

                                      timer_5sec_flag = 0 ;

                                      start_timer() ;

                                  }

                                  sensorWork() ;

                                  CyDelay(1000) ; // if you need to wait, or specify an interval of sampling the sensor

                              }

                          ====

                           

                          moto

                          • 10. Re: Timer psoc5
                            eeEn_4618901

                            Hi Moto;

                            I have been trying to apply the code to my own project for 2 days. but I was not successful.

                            For example, in my project, I will use the timer at least 10 times. I will use it for different functions.

                            Best Regards.

                            • 11. Re: Timer psoc5
                              MoTa_728816

                              Dear eeEn-san,

                               

                              I prepared 10 dummy sensor functions, sensor_func1() ~ sensor_func10().

                               

                              In each sensor_funcX(), single data acquisition should take place at the line of  "print()"

                              then add a sampling delay() for each function, for example I wrote sensor_func1() as

                              ==============

                              void sensor_func1(void)

                              {

                                  print("Sensor 1\n\r") ;

                                  CyDelay(1001) ; /* sampling interval for the sensor 1 */

                              }

                              ==============

                               

                              main() function

                              ================

                              int main(void)

                              {

                                  int sensor_no = 0 ;

                                  int number_of_sensors = 10 ;

                                 

                                  init_hardware() ;

                               

                                  print("starting the Timer\n\r") ;

                                  start_timer() ;

                                     

                                  for(;;) {

                                      if (timer_5sec_flag) { /* timer expired */

                                          CyDelay(2000) ; /* wait 2 sec */

                                          sensor_no = (sensor_no + 1) % number_of_sensors ;

                                          timer_5sec_flag = 0 ;

                                          start_timer() ;

                                      } else {

                                          switch(sensor_no) {

                                          case  0: sensor_func1()  ; break ;

                                          case  1: sensor_func2()  ; break ;

                                          case  2: sensor_func3()  ; break ;

                                          case  3: sensor_func4()  ; break ;

                                          case  4: sensor_func5()  ; break ;           

                                          case  5: sensor_func6()  ; break ;

                                          case  6: sensor_func7()  ; break ;

                                          case  7: sensor_func8()  ; break ;

                                          case  8: sensor_func9()  ; break ;   

                                          case  9: sensor_func10() ; break ;            

                                          default: print("Error Unknown Sensor Number!\n\r") ; break ;

                                          }

                                      }

                                  }

                              }

                              ================

                               

                              Tera Term log looked like

                              021-TeraTerm-log.JPG

                               

                              main.c

                              ====================

                              #include "project.h"

                              #include "stdio.h"

                               

                              #define LED_ON (1u)

                              #define LED_OFF (0u)

                               

                              volatile int      timer_5sec_flag = 0 ;

                               

                              CY_ISR(timer_exp_isr)

                              {

                                  Timer_ReadStatusRegister() ;

                                  timer_5sec_flag = 1 ;

                                  LED_Write(LED_OFF) ;

                              }

                               

                              void start_timer(void)

                              {

                                  Timer_Stop() ;

                                  Timer_RestoreConfig() ;

                                  LED_Write(LED_ON) ;

                                  Timer_Enable() ;

                              }

                               

                              #define STR_LEN 128

                              char str[STR_LEN+1] ;

                              void print(char *str)

                              {

                                  UART_PutString(str) ;

                              }

                               

                              void cls(void)

                              {

                                  print("\033c") ; /* reset */

                                  CyDelay(20) ;

                                  print("\033[2J") ; /* clear screen */

                                  CyDelay(20) ;

                              }

                               

                              void splash(void)

                              {

                                  cls() ;

                                  print("5LP Timer 5Sec Test (using a Timer component)") ;

                                  snprintf(str, STR_LEN, "(%s %s)\n", __DATE__, __TIME__) ;

                                  print(str) ;

                              }

                               

                              void init_hardware(void)

                              {

                                  CyGlobalIntEnable; /* Enable global interrupts. */

                                 

                                  UART_Start() ;

                                  splash() ;

                               

                                  timer_exp_int_ClearPending() ;

                                  timer_exp_int_StartEx(timer_exp_isr) ;

                                 

                                  Timer_Init() ;

                              }

                               

                              void sensor_func1(void)

                              {

                                  print("Sensor 1\n\r") ;

                                  CyDelay(1001) ; /* sampling interval for the sensor 1 */

                              }

                               

                              void sensor_func2(void)

                              {

                                  print("Sensor 2\n\r") ;

                                  CyDelay(1002) ; /* sampling interval for the sensor 2 */

                              }

                               

                              void sensor_func3(void)

                              {

                                  print("Sensor 3\n\r") ;

                                  CyDelay(1003) ; /* sampling interval for the sensor 3 */

                              }

                               

                              void sensor_func4(void)

                              {

                                  print("Sensor 4\n\r") ;

                                  CyDelay(1004) ; /* sampling interval for the sensor 4 */

                              }

                               

                              void sensor_func5(void)

                              {

                                  print("Sensor 5\n\r") ;

                                  CyDelay(1005) ; /* sampling interval for the sensor 5 */   

                              }

                               

                              void sensor_func6(void)

                              {

                                  print("Sensor 6\n\r") ;

                                  CyDelay(1006) ; /* sampling interval for the sensor 6 */   

                              }

                               

                              void sensor_func7(void)

                              {

                                  print("Sensor 7\n\r") ;

                                  CyDelay(1007) ; /* sampling interval for the sensor 7 */      

                              }

                               

                              void sensor_func8(void)

                              {

                                  print("Sensor 8\n\r") ;

                                  CyDelay(1008) ; /* sampling interval for the sensor 8 */  

                              }

                               

                              void sensor_func9(void)

                              {

                                  print("Sensor 9\n\r") ;

                                  CyDelay(1009) ; /* sampling interval for the sensor 9 */  

                              }

                               

                              void sensor_func10(void)

                              {

                                  print("Sensor 10\n\r") ;

                                  CyDelay(1010) ; /* sampling interval for the sensor 10 */  

                              }

                               

                              int main(void)

                              {

                                  int sensor_no = 0 ;

                                  int number_of_sensors = 10 ;

                                 

                                  init_hardware() ;

                               

                                  print("starting the Timer\n\r") ;

                                  start_timer() ;

                                     

                                  for(;;) {

                                      if (timer_5sec_flag) { /* timer expired */

                                          CyDelay(2000) ; /* wait 2 sec */

                                          sensor_no = (sensor_no + 1) % number_of_sensors ;

                                          timer_5sec_flag = 0 ;

                                          start_timer() ;

                                      } else {

                                          switch(sensor_no) {

                                          case  0: sensor_func1()  ; break ;

                                          case  1: sensor_func2()  ; break ;

                                          case  2: sensor_func3()  ; break ;

                                          case  3: sensor_func4()  ; break ;

                                          case  4: sensor_func5()  ; break ;           

                                          case  5: sensor_func6()  ; break ;

                                          case  6: sensor_func7()  ; break ;

                                          case  7: sensor_func8()  ; break ;

                                          case  8: sensor_func9()  ; break ;   

                                          case  9: sensor_func10() ; break ;            

                                          default: print("Error Unknown Sensor Number!\n\r") ; break ;

                                          }

                                      }

                                  }

                              }

                              ====================

                               

                              Best Regards,

                              10-Feb-2020

                              Motoo Tanaka

                              • 12. Re: Timer psoc5
                                eeEn_4618901

                                Hi Moto;

                                If I want to use it in different parts of the program, should I use the codes as follows?

                                I only want 5 seconds working time.

                                Whenever I want it to work 5 seconds in TX mode.

                                Whenever I want it to work 5 seconds in RX mode.

                                Do I need to use the code "CyDelay (2000);"?

                                 

                                How should I write code for CY_ISR?

                                 

                                CY_ISR(timer_exp_isr)

                                {

                                ?????????????????

                                ?????????????????

                                ?????????????????

                                }

                                 

                                 

                                 

                                 

                                for(;;) {

                                        if (timer_5sec_flag) { /* timer expired */

                                            CyDelay(2000) ; /* wait 2 sec */

                                             timer_5sec_flag = 0 ;

                                            start_timer() ;

                                        } else {

                                    RF_TX_MODE();

                                            }

                                        }

                                code

                                .

                                .

                                .

                                code

                                        if (timer_5sec_flag) { /* timer expired */

                                            CyDelay(2000) ; /* wait 2 sec */

                                             timer_5sec_flag = 0 ;

                                            start_timer() ;

                                        } else {

                                    RF_RX_MODE();

                                            }

                                        }

                                • 13. Re: Timer psoc5
                                  MoTa_728816

                                  Dear eeEn-san,

                                   

                                  BTW, do you remember that the question of this topic was

                                  =============

                                  I want 1 sensor to work for 5 seconds in my project.

                                  can i do this with the only  timer block?

                                  =============

                                   

                                  I'm afraid that we have come far beyond the scope of the topic.

                                  To make information in the community usable for others,

                                  I think that we should keep the topic reasonably compact

                                  so that others can find the topic for their hint(s) in the future.

                                   

                                  So please make this answer the last response to this topic

                                  and if you have further question create a new topic with appropriate title.

                                   

                                  Having written that...

                                   

                                  (1) Do I need to use the code "CyDelay (2000);"?

                                    This "CyDelay(2000) ; " was supposed to be an interval between the each 5 sec sequence.

                                    So it's not mandatory.

                                   

                                  (2)

                                  Do the 10 sensors and the RF run concurrently?

                                  Or does your application have the modes such as

                                  Sensor Mode -> RF Mode -> Sensor -> Mode ...

                                   

                                  If both runs concurrently, you can just add

                                  RX/TX flag then call the function in the "else" block, such as below

                                   

                                  (sensors and rf concurrent)

                                  ===================

                                      for(;;) {

                                          if (timer_5sec_flag) { /* timer expired */

                                              sensor_no = (sensor_no + 1) % number_of_sensors ; /* sensor_no = 0, 1, .., 9, 0, 1.. */

                                              if (rf_mode == RF_TX_MODE) { /* switch RF_TX_MODE <-> RF_RX_MODE */

                                                   rf_mode = RF_RX_MODE ;

                                              } else {

                                                   rf_mode = RF_TX_MODE ;

                                              }

                                              timer_5sec_flag = 0 ;

                                              start_timer() ;

                                          } else {

                                              switch(rf_mode) { /* take care of the RF */

                                              case RF_RX_MODE: rf_rx_func() ; break ;

                                              case RF_TX_MODE: rf_tx_func() ; break ;

                                              default: print("Error unknown RF mode\r\n") ; break ;

                                              }

                                              switch(sensor_no) { /* take care of the sensors */

                                              case  0: sensor_func1()  ; break ;

                                              case  1: sensor_func2()  ; break ;

                                              case  2: sensor_func3()  ; break ;

                                              case  3: sensor_func4()  ; break ;

                                              case  4: sensor_func5()  ; break ;         

                                              case  5: sensor_func6()  ; break ;

                                              case  6: sensor_func7()  ; break ;

                                              case  7: sensor_func8()  ; break ;

                                              case  8: sensor_func9()  ; break ; 

                                              case  9: sensor_func10() ; break ;          

                                              default: print("Error Unknown Sensor Number!\n\r") ; break ;

                                              }

                                          }

                                  =========

                                   

                                  or if they are running in the different mode, you can switch loop between sensor_loop and rf_loop.

                                  Then rf_loop will look like below.

                                  =========

                                      for(;;) {

                                          if (timer_5sec_flag) { /* timer expired */

                                              if (rf_mode == RF_TX_MODE) {

                                                   rf_mode = RF_RX_MODE ;

                                              } else {

                                                   rf_mode = RF_TX_MODE ;

                                              }

                                              timer_5sec_flag = 0 ;

                                              start_timer() ;

                                          } else {

                                              switch(rf_mode) {

                                              case RF_RX_MODE: rf_rx_func() ; break ;

                                              case RF_TX_MODE: rf_tx_func() ; break ;

                                              default: print("Error unknown RF mode\r\n") ; break ;

                                              }

                                          }

                                       }

                                  =========

                                  Note1: the ISR can be shared, as far as the wait time is the same.

                                  If you need different wait time(s) for each modes, may be using "SysTick" is smarter.

                                  Note2: You need provide a mechanism to switch between these modes.

                                   

                                  In both case, all of rf_rx_func(), rf_tx_func(), sensor_func1() ... sensor_func10()

                                  must be a "callback" type function, which means non-blocking type function.

                                   

                                  Best Regards,

                                  13-Feb-2020

                                  Motoo Tanaka

                                  P.S. Attached is a test sample program for the concurrent mode.

                                  1 of 1 people found this helpful
                                  • 14. Re: Timer psoc5
                                    eeEn_4618901

                                    Hi Moto,

                                    Is it possible that tx and rx do not run in a continuous loop?

                                    Rx run for 5 seconds and then stop.

                                    In another part of the program, let tx run for 5 seconds and then stop.

                                    1 2 Previous Next