Ideas for One-Shot

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

cross mob
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

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

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

View solution in original post

0 Likes
12 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

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

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

Must be for another thread.

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

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.

0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

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-28650...

DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

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());
    }
}

0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

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

0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

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
}

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

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?

0 Likes

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.

0 Likes
lock attach
Attachments are accessible only for community members.
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

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

0 Likes