Arduino: Understanding the Internals


/* pin = Arduino Mapped Pin Name, ex. Digital pin 22: pin = 22 */
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;
    out = portOutputRegister(port);

    uint8_t oldSREG = SREG;
    cli();

    if (val == LOW) {
        *out &= ~bit;
    } else {
        *out |= bit;
    }
    
    SREG = oldSREG;
}

SREG (Status Register) und Globale Interrupts aktivieren: SREG -Register

extern const uint8_t PROGMEM digital_pin_to_port_PGM[];

#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
#define pgm_read_byte(address_short)    pgm_read_byte_near(address_short)
#define pgm_read_byte_near(address_short) __LPM((uint16_t)(address_short))

Was pgm_read_byte() macht bzw. wie es in Assembler √ľbersetzt wird: Source von pgm_read_byte

C:\arduino-1.0.4\hardware\tools\avr\avr\include
C:\arduino-1.0.4\hardware\arduino\cores\arduino
const uint16_t PROGMEM port_to_mode_PGM[] = {
    NOT_A_PORT,
    &DDRA,
    &DDRB,
    &DDRC,
    &DDRD,
    &DDRE,
    &DDRF,
    &DDRG,
    &DDRH,
    NOT_A_PORT,
    &DDRJ,
    &DDRK,
    &DDRL,
};

/arduino/variants/mega/pins_arduino.h
Accessing data past 64k boundary on atmega1280

Leave a Reply

Your email address will not be published. Required fields are marked *