Awesome
<div align = center>digitalWriteFast
Arduino library for faster digitalWrite()
using direct port manipulation and macro for ease in pin assignments for constant pin numbers.
It also provides faster pinMode()
and digitalRead()
functions as well as it adds a digitalToggleFast()
function.
Available as Arduino library "digitalWriteFast".
</div>If you find this library useful, please give it a star.
<br/>This is a fork of the subtree https://github.com/watterott/Arduino-Libs/tree/master/digitalWriteFast.
If pin number is no compile time constant, or the platform is not supported, then the regular Arduino functions digitalWrite()
etc. are called.
- Use '#define pinNum 10' instead of
int pinNum = 10;
- Use 'const int pinNum 10' instead of
int pinNum = 10;
Usage
Include the library:
#include <digitalWriteFast.h>
Macro definitions:
digitalWriteFast(pinNum, state)
(sets or clears pin/port faster)pinModeFast(pinNum, mode)
(sets pin/port as input or output faster)digitalReadFast(pinNum)
(reads the state of pin/port faster)digitalToggleFast(pinNum)
(toggles the state of pin/port faster)
Parameters:
pinNum
is the number written on the Arduino board.state
is weather pin is to be setHIGH
orLOW
mode
is weather pin is to be setINPUT
orOUTPUT
orINPUT_PULLUP
Correct usage detection
Defining THROW_ERROR_IF_NOT_FAST
would cause the macro to return an error during compilation, if the parameter is a variable and thus regular digital* functions must be called.
This notifies the programmer the specific line where code is still slow.
Speed
For a 16 MHz Uno digitalWrite() is speed up from 5.8 µs to 125 ns, able to generate a 4 MHz signal instead of 86 kHz. More info in: /NOTES/NOTES.md
This is the waveform generated with the Timing example.
Compatibility
- Arduino Uno, Nano etc.
- Arduino Leonardo
- Arduino Mega
- Arduino with ATmega644 or Atmega644P chip
- Arduino with ATmega4809 chip
- Arduino with ATtiny chip
If you use megaTinyCore, you cannot use "#include digitalWriteFast.h" because megaTinyCore has it own digitalWriteFast function set, except digitalToggleFast(). <br/>
Revision History
Version 1.2.1
- Fixed preprocessor bug for ATtiny84.
- Support for megaTinyCore.
Version 1.2.0
- Removed warning of non existent PORTA etc.
- Added ATtiny1614.
- Fix attiny x4 pinout
- Added __digitalPinToBit for ATmega16.
Version 1.1.0
- Added TEENSYDUINO support.
- Added THROW_ERROR_IF_NOT_FAST functionality.
- Removed unnecessary compare
> 0
. - Added Guard
&& defined(__digitalPinToPINReg)
etc. - Changed
ifndef
toif !defined()
.
Reference
This documentation is based on https://github.com/NicksonYap/digitalWriteFast/