Digital IO
|
The DigitalIO library has a number of classes and functions for fast digital I/O for AVR ports plus fast software I2C and SPI.
There are pin toggle functions in the DigitalPin and PinIO classes and a fastDigitalToggle() function.
These functions use the fast AVR pin toggle feature.
When a "one bit" is written to an AVR port's PIN register, the value of the same bit in the port's PORT register is toggled. This provides a fast hardware assisted toggle function.
The DigitalPin.h file contains the fastest pin I/O class and functions. These functions require a constant pin number.
These function execute in two cycle with constant arguments for low address ports. On an Uno, two cycles is 0.125 usec.
Port H and higher on the Mega are much slower since bit set and bit clear instructions are not supported on high address ports.
See Fast Pin I/O for detailed documentation.
The class PinIO is four to five times faster than the standard digitalRead() and digitalWrite() functions. For pin 13 on an Uno, write() executes in about 0.8 usec and toggle() is faster. The standard Arduino digitalWrite() function takes about 4 usec.
This class saves overhead by not disabling PWM mode on each read/write call and by storing the port register addresses and pin bit mask in private variables.
The template class SoftSPI uses fast pin I/O to implement a software SPI bus at about 2 MHz.
The two classes FastI2cMaster and SoftI2cMaster implement software master mode I2C.
FastI2cMaster is a template class and runs at about 400 kHz.
SoftI2cMaster uses runtime pin numbers and runs at about 100 kHz.