2 Interrupt Based Timing
Jan Gromeš edytuje tę stronę 2024-03-31 09:12:07 +02:00

Under some conditions, it may be beneficial to use a custom timer to control timing of protocols and ham radio digital modes, such as RTTY or SSTV. By default, timing of tone lengths is controlled by the Arduino micros() function. However, if the precision of this function is insufficient, a custom timing based on interrupt may be provided.

  1. Define the RADIOLIB_INTERRUPT_TIMING macro in your build system and set it to 1. For standard RadioLib installations, this is most easily achieved by adding #define RADIOLIB_INTERRUPT_TIMING (1) to BuildOptUser.h

  2. Create the interrupt service routine. This is the function that will be called by the interrupt, the only thing it has to do is to call the method setTimerFlag(), which will set the timing flag.

// this function MUST be 'void' type
// and MUST NOT have any arguments
void setFlag(void) {
  radio.setTimerFlag();
}
  1. Set up your timing interrupt and attach the interrupt service routine. This step depends on your platform, the following code snippet assumes the code is running on Arduino Uno and is using the TimerOne library:
#include <TimerOne.h>

// interrupt setup function
// this function MUST be 'void' type
// and MUST have exactly one 'uint32_t' argument
void interruptSetup(uint32_t len) {
  Timer1.initialize(len);
  Timer1.attachInterrupt(setFlag);
}
  1. Finally, provide this setup function to the library. The interruptSetup function will be called by the library to setup the actual pulse length. For example, when using with 45-baud RTTY, RadioLib will call the function as interruptSetup(22222), to configure the interrupt for 22.222 ms (45 baud). Note that this function will get called repeatedly for changing tone lengths!
radio.setInterruptSetup(interruptSetup);
  1. Done! The timer will now trigger interrupt, which will control the timing of pulse lengths.