- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
So, you suggest a timer is best component for a One Shot , 1 second timer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Must be for another thread.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Orbitcoms,
I posted examples of 1-shot timers collected from many authors in this thread:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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());
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
As far as I can remember, I tested all ~6 examples OK. Did you try only one example?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content