14 396 Direct Paddle Pin Reads Uno Nano Mega2560
VK2EFL edytuje tę stronę 2022-02-16 22:16:57 +11:00

This option can be enabled to expedite the time taken to read the paddle pins. The option works on implementations using a Uno, Nano or ATMEGA2560.

For this option to work on a Uno or Nano the paddles must be connected to pins in the range 2-7. On a ATMega2560 board the paddles can be connected to pins in the range 2-13. The default values in the file keyer_pin_settings.h are pins 2 and 5 and of course this configuration will work on Arduino Unos, Nanos and ATMega2560 boards

Two different options are available and the relevant one should be enabled depending on the board that is in use -

On a Uno or Nano uncomment #define OPTION_DIRECT_PADDLE_PIN_READS_UNO

On an ATMega2560 uncomment #define OPTION_DIRECT_PADDLE_PIN_READS_MEGA

The use of the direct register read option is only done inside the function paddle_pin_read() since the largest benefit was to be gained by making that often called function more efficient. The function paddle_pin_read() is called each time the program cycles around in the main loop. By default the paddle pins are read using digitalRead(pin_to_read) which takes more than 100 instructions to complete. Direct reads of the registers that the paddle pins are connected to takes just 2 instructions.

Sensing the state of the paddles is most efficiently done by directly examining the port registers that are connected to the pins. Using this technique results in code that is only a few instructions long. The code to do the direct read of the registers uses a macro bitRead(port, bit). In the K3NG keyer code the actual port and bit that is in use is hard coded into these macro calls, depending on the processor type (determined by the option that is selected).

The benefit of directly reading the port registers is smaller code that is faster executing. With all the other functions that are being called in the main loop() it is impossible to quantify what the performance improvement really is, but the resultant code size is smaller by some 340 bytes, and that is real. The reduced code size could permit the inclusion of a feature that might not otherwise fit into a Uno or Nano with their restricted memory sizes.

If the option is not enabled then the code defaults to the former method of using digitalRead(pin_to_read) to determine the state of the paddle pins.