If a activity will get really complicated, this methodology of routing execution to the proper part of the task each time it is known as might get a bit bushy if you actually wanted to avoid all pointless delays. I usually use a CASE statement (called “Select CASE” in Basic and “change” in C, but I named lots of my structure macros after Forth structures) to steer the execution to the precise part of the routine depending on the state. There isn’t any need for key-up debouncing, LDA KEY ; as state zero will do the same factor. LDA KEY ; Here we have reached the top of the debounce period with the key nonetheless pressed. LDA VIA1T1CL ; Clear VIA1 interrupt. The tasks I had had been all dependent to some extent on time, or on other things having completed (like a byte being shifted in or out on a slowish hardware serial interface), or on a flag being set to point whether or not the duty was active on the time, or something like that; so each might: 1. Check to see if it is even presupposed to be energetic in the intervening time. Such a task might first test a flag variable to see if it is even imagined to be energetic in the intervening time, and if not, return instantly.
For much less runtime overhead, it is likely to be better to have the alarm set up routine see if there’s already an alarm with the identical due time, and if so, increment the subsequent one by a tick (or even more if there’s any risk that a single tick won’t give the last earlier alarm time to finish executing first). SCAN ; See if it remains to be being pressed. Have the real-time clock ISR look ahead to the following alarm being previous due, rather than limiting it to a precise match. LOOP. The state quantity is held within the variable, and depends upon what the duty was doing final, and should get changed (“transitioned” to a different state) when it has finished something, or when an occasion or clock time that it was looking forward to arrives, dictating that we’re shifting on to the subsequent step, or perhaps to begin over. The translation shouldn’t be examined, baby bottle however will provide you with a superb head start when you need to jot down code for this type of factor. I’ve done an analogous thing with an LED the place the primary indication comes when there’s one-third of the battery life left, and the blink fee will increase because the battery voltage continues to drop, giving a reasonably good idea of how much life is left.
5×7 dot matrix. A custom character of a battery works out properly. As you may imagine, there are many ways to perform alarm strategies. A transition often consists of incrementing the state to the subsequent increased quantity; but there are a number of exceptions the place it may get modified to one thing else. If there is not less than one alarm pending, it will normally take three more directions. If there is a risk of getting multiple alarms with the very same due time, a couple of possible ways to deal with it are: – Have the actual-time clock ISR test for extra presumably due alarms after executing the first one. Comparison is completed on the low byte first and high byte final to avoid wasting cycles, because the high bytes usually tend to be the same. You’ll be able to choose which bytes of the RTC to make use of, e.g., bytes zero and 1, 1 and 2, or 2 and 3.) The target value should be initialized within the reset routine to prevent super lengthy delays before the first blink, if the icon must blink.
Take for example a key-scanning routine. Many tasks may have little or nothing to do many of the times they’re called, so that they take little or no time. If there is not any alarm pending, the RTC NMI ISR will usually take 8 directions, together with the RTI. The complete time it takes then is only the JSR, BIT, BMI, RTS, solely 18 clocks, together with the JSR and RTS. 32 ; alarm. If it does not match (as will nearly at all times be BNE CkAlmEnd ; the case), then simply exit. 1 ; If there’s no alarm pending (shown by a zero within the excessive BEQ CkAlmEnd ; byte of the addr of its routine, then just exit. Then I was ready to make use of the workbench laptop for something else at the identical time, with my other program not having to understand it wasn’t the only one running. You might need routines to: – set an alarm with an absolute time; – another to set an alarm with a time relative to proper now; – kind alarms by chronological order (which could be utilized by the two above) and go away the next-due one at the top of the queue; – delete an alarm, utilizing its execute deal with to search out the appropriate one, and shut up the hole in the alarm checklist; – delete the alarm at the head of the queue (used particularly by that alarm when it executes); – learn how lengthy earlier than the next alarm is due; and – do conversions from HH:MM:SS to centiseconds (or milliseconds, or no matter you employ) and again.