| View previous topic :: View next topic |
| Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1076 Location: Panama
|
| "Simultaneous" Multiple variable update |
Posted: Mon Dec 01, 2025 11:44 am |
|
|
Hi all, its been a while.
Im writing a multiple timer software - to control flow of code on a machine that drives pumps, valves etc... EI: after 35 seconds, start the pump, after 45 seconds, drive the valve, etc...
My idea is to build a structure like such:
{
id
Seconds
Minutes
Status
}
make an array of these structures and do code to start and stop these timers - accuracy is not a priority. assume upto 10 elements in the array.
each timer would get an id from the code that started it and they would just enter and exit the array as needed... ive got a decent idea of what i want.
i have a hardware RTC with 1hz interrupt which can drive the seconds on these structures. (I have real hardware)
im running a very very improvised RTOS-ish "OS" (has worked well for years).
MY QUESTION IS: how do i update ALL the Seconds variables at the same time across the array of structures?
a while loop in the ISR? (assume 10 elements)
or
S.A=S.B=S.C=S.D=ISR_SECONDS?
wrong approach? - im open to suggestions.
Thank you! _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
 |
gaugeguy
Joined: 05 Apr 2011 Posts: 352
|
|
Posted: Mon Dec 01, 2025 2:45 pm |
|
|
| Only have one seconds variable and just save the time you want the event to occur. Somewhere you will do a comparison to see if the event should be triggered. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19991
|
|
Posted: Tue Dec 02, 2025 12:52 am |
|
|
As Gaugeguy is saying, run one timer. Even simpler, just have this be a
count in seconds. Use a 32bit integer for this. Then when you want to start
a particular 'timer', read the current count from the master timer, and add
the number of seconds this timer is to run for. So if you have a timer wanting
(say) 25 minutes, just load this with current_timer+(25*60).
In the individual routines, you can subtract the current_timer value from
this stored value to know how long the 'timer' has still to run. Converting
this integer to 'hours, minutes and seconds, is simple division.
There is a standard C library 'time.h, which if you want allows conversions
a 'time' structure, into a counter like this, and converts a counter back to
a time structure. It includes things like handling time zones, and diff time
to give the difference between two timer values. Probably more complex than
you need, but shows how times are normally handled.
Importantly, you can use disable_interrupts before reading this master
timer value, to avoid the possibility of it updating during the read, and then
re-enable once this is done. |
|
 |
Gabriel
Joined: 03 Aug 2009 Posts: 1076 Location: Panama
|
|
Posted: Tue Dec 02, 2025 6:46 am |
|
|
Thanks guys.
That makes sense.
Pre calculated trigger value and just 1 counter.
I can still use my above structure!
Thank you! _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9598 Location: Greensville,Ontario
|
|
Posted: Tue Dec 02, 2025 8:44 am |
|
|
Since you only need '1 second resolution' you could do all the compares inside the 1HZ ISR ,similar to this.
.... https://www.ccsinfo.com/forum/viewtopic.php?t=26177...
I did similar code decades ago using QuickBasic4.5 for sending pages from an answering service computer. |
|
 |
|