Vergleich zwischen digitalWrite und direkten Zugriff auch das Portregister
Code
digitalWrite(2, LOW);
digitalWrite(2, HIGH);
digitalWrite(2, LOW);
digitalWrite(2, HIGH);
digitalWrite(2, LOW);
digitalWrite(2, HIGH);
digitalWrite(2, LOW);
digitalWrite(2, HIGH);
digitalWrite(2, LOW);
digitalWrite(2, HIGH);
digitalWrite(2, LOW);
digitalWrite(2, HIGH);
PORTD |= 1<<2;
PORTD &= !(1<<2);
PORTD |= 1<<2;
PORTD &= !(1<<2);
PORTD |= 1<<2;
PORTD &= !(1<<2);
PORTD |= 1<<2;
PORTD &= !(1<<2);
PORTD |= 1<<2;
PORTD &= !(1<<2);
PORTD |= 1<<2;
PORTD &= !(1<<2);
PORTD |= 1<<2;
PORTD &= !(1<<2);
PORTD |= 1<<2;
PORTD &= !(1<<2);
PORTD = 4;
PORTD = 0;
PORTD = 4;
PORTD = 0;
PORTD = 4;
PORTD = 0;
PORTD = 4;
PORTD = 0;
PORTD = 4;
PORTD = 0;
PORTD = 4;
PORTD = 0;
PORTD = 4;
PORTD = 0;
PORTD = 4;
PORTD = 0;




digitalWrite: ~ 110 kHz
Port Register mit vergleich: ~ 4 MHz (Faktor: ~36)
Port Register ohne vergleich: ~ 8 MHz (Faktor: ~72)
Der Code, der beim aufrufen von digitalWrite ausgeführt wird:
Code
#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
void digitalWrite(uint8_t pin, uint8_t val)
{
uint8_t timer = digitalPinToTimer(pin);
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *out;
if (port == NOT_A_PIN) return;
// If the pin that support PWM output, we need to turn it off
// before doing a digital write.
if (timer != NOT_ON_TIMER) turnOffPWM(timer);
out = portOutputRegister(port);
uint8_t oldSREG = SREG;
cli();
if (val == LOW) {
*out &= ~bit;
} else {
*out |= bit;
}
SREG = oldSREG;
}