ENع
→Registers and bit masks
الدرس 32 · Registers and the memory map

Peripheral registers and bit masks

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة9 خطوات5 أسئلةIntermediate
عن هذا الدرس

A peripheral register is a byte at a fixed address whose bits are wired to hardware. On an Arduino Uno R3, PORTB sits at data address 0x25 and each bit drives a pin. Follow one byte as a mask built with 1 << 5 sets, clears, flips and tests bit 5 (pin 13) while pin 8 on bit 0 stays untouched. Then see why PORTB = m is a bug and what digitalWrite() really does underneath. The lesson “The memory map: flash, RAM and peripherals” shows where these addresses live, and the lesson “Pointers” explains the * that turns an address into a variable.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 32 · تفاعلي
01 / 09

A register has an address

Inside the Uno's ATmega328P, PORTB is a register: one byte at data address 0x25. Unlike RAM, each bit is wired to a pin: bit 0 drives pin 8, bit 5 drives pin 13. Now PORTB holds 0x01, so only pin 8's LED glows.

PORTB   I/O 0x05 + 0x20 = data address 0x25
bits    b7 b6 b5 b4 b3 b2 b1 b0
now     0  0  0  0  0  0  0  1   = 0x01
b0      pin 8   (PB0)  LED on
b5      pin 13  (PB5)  LED off
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A register has an address

    Inside the Uno's ATmega328P, PORTB is a register: one byte at data address 0x25. Unlike RAM, each bit is wired to a pin: bit 0 drives pin 8, bit 5 drives pin 13. Now PORTB holds 0x01, so only pin 8's LED glows.

    PORTB   I/O 0x05 + 0x20 = data address 0x25
    bits    b7 b6 b5 b4 b3 b2 b1 b0
    now     0  0  0  0  0  0  0  1   = 0x01
    b0      pin 8   (PB0)  LED on
    b5      pin 13  (PB5)  LED off
  2. PORTB is a pointer

    In C, PORTB is a macro (a name swapped for code) for *(volatile uint8_t *)0x25: the byte at address 0x25. Reading it reads the register; writing it changes the pins. volatile makes the compiler do every read and write for real.

    #define PORTB _SFR_IO8(0x05)            // avr/iom328p.h
    _SFR_IO8(a)  = _MMIO_BYTE(a + 0x20)
    _MMIO_BYTE(a) = (*(volatile uint8_t *)(a))
    so    PORTB = *(volatile uint8_t *)0x25
  3. Build a mask

    Line 1 builds a mask: a byte with a 1 only in the bits you want to touch. 1 << 5 shifts a 1 five places left, giving 0b00100000, which is 0x20. Bit 5 is pin 13.

    line 1   uint8_t m = 1 << 5;
    1        0b00000001
    1 << 5   0b00100000  = 0x20
    same as  _BV(5) in avr-libc
  4. OR sets a bit

    PORTB |= m reads PORTB, ORs it with the mask and writes the result back. x OR 0 keeps x; x OR 1 gives 1. Only bit 5 changes: 0x01 becomes 0x21. Pin 13 lights, pin 8 stays on.

    line 2   PORTB |= m;
    PORTB    0b00000001
    m        0b00100000   OR
    result   0b00100001  = 0x21
  5. AND NOT clears a bit

    ~m flips every bit of the mask: 0b11011111. PORTB &= ~m ANDs with it. x AND 1 keeps x; x AND 0 gives 0. Bit 5 clears, 0x21 becomes 0x01: pin 13 goes dark, pin 8 stays lit.

    line 3   PORTB &= ~m;
    PORTB    0b00100001
    ~m       0b11011111   AND
    result   0b00000001  = 0x01
  6. XOR flips a bit

    PORTB ^= m flips bit 5. x XOR 0 keeps x; x XOR 1 flips it. 0x01 becomes 0x21 and pin 13 lights. Run the same line again and it goes dark: a blink in one line.

    line 4   PORTB ^= m;
    PORTB    0b00000001
    m        0b00100000   XOR
    result   0b00100001  = 0x21
    again    0b00000001  = 0x01
  7. AND tests a bit

    PORTB & m keeps only bit 5 and writes nothing back. The result is 0x20, not zero, so on is true. Had bit 5 been 0, the result would be 0, so false. To read input pins, you read PINB instead.

    line 5   bool on = PORTB & m;
    PORTB    0b00100001
    m        0b00100000   AND
    result   0b00100000  = 0x20  -> true
    PORTB    unchanged (nothing written)
  8. Why not PORTB = m?

    A plain write puts all eight mask bits into PORTB at once. Bit 5 stays 1, but bit 0 becomes 0 too, so pin 8's LED goes out by accident. |=, &= and ^= change only the bits you mean.

    line 6   PORTB = m;
    before   0b00100001
    after    0b00100000  = 0x20
    b0       1 -> 0   pin 8 off: a bug
  9. What digitalWrite really does

    digitalWrite(13, LOW) looks up port B and mask 0x20 in tables, checks for PWM (pin 13 has none), pauses interrupts (events that jump in mid-code), then runs *out &= ~bit: the same AND NOT. 0x20 becomes 0x00.

    line 7   digitalWrite(13, LOW);
    bit      digitalPinToBitMask(13) = _BV(5) = 0x20
    out      portOutputRegister(PB)  = &PORTB
    PWM      turnOffPWM(timer) if the pin has a timer; pin 13 has none
    oldSREG = SREG; cli();  *out &= ~bit;  SREG = oldSREG;

اختبار يتاح بعد الخطوة الأخيرة.