I have a programming issue I would like to handle with multi threading based on interrupts, here is the topic. I a m currently building an Arduino based sound generation engine, which basically receives incomming Midi messages and generates the associated sound on a DAC.
The code today has a simple Main loop in the one Midi data is handled, and an interrupt vector trigged every 50us in the one each sample is processed and brought to the DAC. I use Timer2 to trigger this 50us interrupt and it's working fine up to now.
The main loop is making two actions :
1 - listens to serial in and process input commands. 2 - every 1ms, it updates what I call "low speed parameters" of the synth, like enveloppe and so on.
Because those two tasks will some day interfere, I would like to switch to a different programming scheme but I don't know if it is feasible.
Can I generate a second interrupt with another Timer, and make it happen every 1ms to do that "low speed task" I need to do every 1ms? I don't want it to interfer with the hight speed interrupt that generates the sample : can I prioritise the whole interrupt thing so that when the Timer2 sample interrupt is triggered, no matter if it is running the main loop or the low speed interrupt, it is executed. And on the other hand, can I configure the high speed interrupt so that the lowest priority interrupt can be handled right after it has completed its task when it is triggered?
Thanks in advance for your help.
If this can ease the understanding, this is the configuration of the Timer2 for my current high speed task:
// Configuration du Timer 2 pour la fréquence d'échantillonnage // No output (COM2A0 cleared, set = Toggle on Match) // Compteur en FAST PWM, Clear on Match OCR2A, Update at BOTTOM // Clock /8 (CS21 set, si set CS20 en plus /32) TCCR2A = _BV(COM2A0) | _BV(WGM21) | _BV(WGM20); TCCR2B = _BV(WGM22) | _BV(CS21); TIMSK2 = _BV(TOIE2); // Enable timer overflow interrupt, once every 1/22039Hz = 45.375us. OCR2A = SMP_REG;
Moderator: edited for legibility. AWOL