BlinkTimer
Last updated
Was this helpful?
Last updated
Was this helpful?
This example shows how to use the timer and interrupt mechanism on the SwiftIO Board to make the on-board Red LED blinking at a constant frequency.
SwiftIO board
Only the SwiftIO Board itself is required here. No extra shenanigans.
Timer is precisely a part of the hardware on the SwiftIO board. It works just like an alarm clock, and it can be programmed by manipulation of some registers. You can set some parameters for the timer, which makes it trigger every so often.
An interrupt ensures that the processor responds quickly to some important events. When a interrupt signal is detected, the processor will stop its current job and perform some other codes, so that the board can react to the external signals quickly and accordingly.
In this case, the timer is set to be the internal interrupt source, which is done by using .setInterrupt(ms: )
, a method in the Timer()
class. The parameter indicates that the timer triggers an interrupt every 1000 ms, or, every 1 s.
The .toggle()
(as the name implies) method of DigitalOut class means that the output level of the specific pin is inverted. In this case, the red LED light will be switch on or off when the interruption happens.
You can also use the sleep(ms: )
function to achieve the same effect. But this function will make the processor unresponsive during this period of time, making it not do anything. Therefore, the advantage of using interrupt
is that the processor can still do other things between two toggles.
- The Timer class is used to set the occasion to raise the interrupt.
- To alternate between two voltage level.