Examples for using events, timers and interrupts together #563
Replies: 8 comments 10 replies
-
As part of my own learning curve with these features. I played with capturing a servopulse from an RC receiver and print that. (compile with millis disabled)
|
Beta Was this translation helpful? Give feedback.
-
Then I did more or less the same to learn about using TimerD
|
Beta Was this translation helpful? Give feedback.
-
And an example playing with the Real Time Clock (RTC) and the Programmable Interruupt Timer (PIT)
|
Beta Was this translation helpful? Give feedback.
-
Are you folks aware of the Event.h library? It's now actually usable, and equipped with functions that will find an unused channel for many generators, and give you generators if you specify just pass it the periperhal (ie, you pass gen_from_peripheral(CCL,1) you get the generator 1 from the CCL (that is, CCLLUT1OUT.) Works with most of the peripherals you're likel;y to care about. I also had code that would make a tiny 412 act to split WS2812 datastreams (ie first 100 LEDs go down this wire, then the rest go down that wire), but I was havign problems with it and since Im in the midst of a move what was forced on us (we're being evicted, a great thanks from the landlord for paying our rent on time and in full throughout the pandemic - we've got a new place that I need to move a heroic amount of stuff tyo in the next 14 days.... and my roommmates just told me that they've LOST THE FUICKING KEYS TO THE NEW PLACE/.....anyway.... I haven'y ad a cance to work on that further. It was pretty cooll tjough And with a tiny424, you could do up to 4 strings the use case is the fairy lghts, which are usuially not chainable, and even if they were the power injection wires would loook likeshit. |
Beta Was this translation helpful? Give feedback.
-
I know about the Event.h library, but have not used it yet. Good luck with your move LOL. |
Beta Was this translation helpful? Give feedback.
-
@hmeijdam those examples look very useful, much obliged! @SpenceKonde do you plan a new release of megaTinyCore after you move? I think a lot of work went into the event library since 2.4.2 |
Beta Was this translation helpful? Give feedback.
-
I would love to setup a timer to run an ISR periodically. |
Beta Was this translation helpful? Give feedback.
-
There's actually a library that does that, the guy maintaining it made a thread at Arduino forums. it will never be particularly performant, because of the nature of how any form of "attachInterrupt() like function works (the compiler has to respect both the ABI (which says that a function may freely clobber r0, find 0 in r1, and freely clobber r18-r27, and r30/r31) and the rules about behavior in interrupts (all working registers must have their state restored) - in other words "save and restore an extra 14 registers that we may not even need". Also because of the code is , and the inlining that would normally save you from this can't occur because it's using an indirect call (icall) with the net effect of wasting like 50 clocks and 30 bytes, plus the overhead of the library itself, but whether you care will depend on your use case. (in a normal function, the compiler plans ahead by storing results that are not referenced again in those registers, so while it might need to make room elsewhere within the working registers to store stuff it will need to know after a function, it doesn't have to save and restore a register that it doesn't care what the value of is. In an ISR all working registers have tobe assumed to contain vital data and need to be saved and restored (2 words and 3 clocks per register). If the interrupt didn't have to make an actual function call, only registers it needed would be saved and restored, but icalls are a barrier to that optimization, since they could point to any function. And the overhead of the library slows things down further... But it does more for you. Minimal periodic int demo is something like
Periodic interrupt mode is very simple. The others are much more complicated to use. That said, give thought to whether a periodic interrupt is the correct approach - frequently it is not, and you;d be better served by using millis and blink-without-delay tactics. In short, you want to only do in an ISR somethingthat can't be done any other way. |
Beta Was this translation helpful? Give feedback.
-
Arguably the most compelling feature of those newer chips is the event system and the more powerful timers. Unfortunately there is not much on how to actually use them together. The documentation in PWMandTimers.md mentions that the type B timers are great as utility timers but no examples are provided (I only found examples for TCA and PWM stuff).
I know that there is a plan to wrap this functionality in a more convenient library eventually (#481) but in the meantime some examples for simple stuff (how do I configure a timer? how do I connect an event to a timer? how do I get the timer to generate an interrupt when it expires? how can I measure the length of a pulse?) would be a tremendous help. I can take a look at examples provided by Nick Gammon 10 years ago for the classic Atmel parts such as this one and be up and running in minutes. Something like that but updated for the new features in the 0/1/2 series would be very useful.
I've tried to start with the example for DxCore in MCUdude/MegaCoreX#126 (comment) but I guess some parts of the event library were added after the 2.4.2 release of megaTinyCore.
Beta Was this translation helpful? Give feedback.
All reactions