12 Replies Latest reply on Jan 28, 2016 6:42 PM by user_343349849

    Ideas for One-Shot

    user_343349849

      Hi,

         

      I am wanting to implement a one-shot timer where I can "Kick" it and it will run for say 1 second and stop. If I continue to "kick" it, it restarts the 1 second and continues. Output does not need to be interrupt.

         

      The application is that a software signals a relay to turn on and it only times out if no signals given over a 1 second period.

         

      Note sure whether a Timer or maybe PWM component would be best or whether there might be another component that may suit better than these.

         

       

         

      Thanks

        • 1. Re: Ideas for One-Shot
          user_1377889

          As usual, a combination of soft- and hardware is best. Use a timer component to generate a fixed frequency of your smallest needed resolution. Count that interrupts and when expired act accordingly. This has the advantage that you need only one timer for all timings.

             

           

             

          Bob

          • 2. Re: Ideas for One-Shot
            user_343349849

            So, you suggest a timer is best component for a One Shot , 1 second timer

            • 3. Re: Ideas for One-Shot
              user_1377889

              No, not for the one-shot you requested. I am talking about a continually running timer which at tc interrupt decrements a variable. When at zero it is timed out signaling the situation by setting a variable. Timer keeps on running. One-shot is a bit complicated because the timer/counter needs a hardware signal of a defined length to reset or restart the one-shot.

                 

               

                 

              Bob

              • 4. Re: Ideas for One-Shot
                user_343349849

                Must be for another thread.

                • 5. Re: Ideas for One-Shot
                  user_343349849

                  Anyone have some ideas or examples for a one shot timer. The timer module datasheet seems to look as though it can be started and restarted ok in software (maybe using a control reg to control the reset pin?).

                     

                  I simply want to turn on a relay and start a 1 second timer. If the timer elapses then in software I will turn off the relay. If an event occurs before the 1 second is up, I want to restart the timer for 1 second so it does not timeout (and my code on timer timeout turns off the relay).

                     

                  The time is not being used to control the relay, just to flag when I want to turn it off if no event occurs within a 1 second period.

                  • 6. Re: Ideas for One-Shot
                    user_342122993

                    Orbitcoms,

                       

                    I posted examples of 1-shot timers collected from many authors in this thread:

                       

                    http://www.cypress.com/forum/psoc-4-architecture/one-shot-timer-delayed-code-execution#comment-286506

                    1 of 1 people found this helpful
                    • 7. Re: Ideas for One-Shot
                      user_343349849

                      Thanks odissey1, I downloaded the example and set up one timer but whatever I do with the clock speed and period in the timer component, I get 500ms delay when I set the control reg.

                         

                      static volatile CYBIT flag_Timer2 = 0 ;

                         

                      CY_ISR(isr_Timer2) // ISR Timer to report temperature at interval
                      {
                          flag_Timer2 = 1;
                      }

                         

                      int main()
                      {
                          CyGlobalIntEnable;
                          isr_Timer2_StartEx(isr_Timer2);
                          Timer2_Start();
                            
                          for(;;)
                          {
                             flag_Timer2 = 0;
                             Control_Reg_TRIG_2_Write(1); //trigger one-shot Timer2  
                             while(!flag_Timer2){}
                             TestPin_Write(!TestPin_Read());
                          }
                      }

                      • 8. Re: Ideas for One-Shot
                        user_342122993

                        As far as I can remember, I tested all ~6 examples OK. Did you try only one example?

                        • 9. Re: Ideas for One-Shot
                          user_342122993

                          I corrected your code below. 

                             

                           

                             

                          static volatile CYBIT flag_Timer2 = 0 ;

                             

                          CY_ISR(isr_Timer2) // ISR Timer to report temperature at interval
                          {
                              flag_Timer2 = 1;
                          }

                             

                          int main()
                          {

                             

                              //Init section
                              CyGlobalIntEnable;

                             

                              isr_Timer2_StartEx(isr_Timer2);
                             Timer2_Start();

                             

                                   
                              for(;;)
                              {

                             

                                    //this is some event that triggers 1-shot Timer2 (could be a button press etc.)
                                    if(flag_ReportTimer) //intervals
                                   {
                                        flag_ReportTimer = 0;

                                       //Timer2_Start(); //start timer (can start here or in Init section)
                                       Control_Reg_TRIG_2_Write(1); //trigger one-shot Timer2
                                   }

                             

                           

                             

                                  //we come here when Timer2 expires....
                                 if(flag_Timer2) //one-stot using pulsed Control_REG
                                {      

                             

                                      flag_Timer2 = 0;
                                      Control_Reg_TRIG_2_Write(1); //trigger one-shot Timer2  
                                      TestPin_Write(!TestPin_Read());

                             

                                 }

                             


                              } //for
                          }

                          • 10. Re: Ideas for One-Shot
                            user_343349849

                            I cannot see where you have corrected the code. It looks like you have just used an external event (from Report Timer) to start the timer and then wait for the flag to process and start again. Isn't this what my code does? Starts the timer and waits for timeout, toggles IO pin and then starts timer again.

                               

                            One thing I am thinking, doesn't the Control Reg need to have a "0" written to it before next event so it provides "pulsed" activation of the timer?

                            • 11. Re: Ideas for One-Shot
                              user_343349849

                              I have attached the sample project below. I am using the Timer 1 example but the interrupt on TC seems to never occur.

                              • 12. Re: Ideas for One-Shot
                                user_342122993

                                1. your code is blocking, it will sit at line "while(!flag_Timer2){}" not allowing to execute anything else. You can use CyDelay(...) instead of 1-shot timer with same success. Hopefully, the interrupt will be able to pass thru. The code above is non-blocking, CPU can work on other tasks while waiting for interrupt. 

                                   

                                    for(;;)
                                    {
                                       flag_Timer2 = 0;
                                       Control_Reg_TRIG_2_Write(1); //trigger one-shot Timer2  
                                       while(!flag_Timer2){}
                                       TestPin_Write(!TestPin_Read());
                                    }

                                   

                                 

                                   

                                2. Control_Reg_TRIG_2_Write(0);  is not necessary. Again check and run my project, posted above. All examples are tested and work OK. I will be able to look at your project on weekend.