ENع
→Bare-metal blink
الدرس 33 · Registers and the memory map

Blink with registers, no library

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

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

digitalWrite(13, HIGH) feels like one step, but it is a library function that looks up a port and a bit, then sets that bit in a register. On an Arduino Uno R3 pin 13 is PB5, bit 5 of port B, and port B is three bytes in the ATmega328P's data memory: PINB at 0x23, DDRB at 0x24 and PORTB at 0x25. Here you blink the board's LED by writing those registers yourself in plain C, with avr-libc and no Arduino functions. The lesson "Peripheral registers and bit masks" explains masks in general and the C++ lesson "Pointers" explains what *(volatile uint8_t *)0x25 points at; here we use both on one real pin.

ستتعلّم

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

شكل 33 · تفاعلي
01 / 10

Inside digitalWrite(13, HIGH)

digitalWrite(13, HIGH) first looks up pin 13 in tables stored in flash: port B, bit 5. It checks whether a timer drives the pin, pauses interrupts, sets that one bit in a register, then restores the interrupt setting it saved.

digitalWrite(13, HIGH)   Arduino core, wiring_digital.c
  timer = table[13]   ->  none on pin 13
  bit   = table[13]   ->  _BV(5) = 0x20
  port  = table[13]   ->  port B
  save SREG, cli()        pause interrupts
  *out |= bit             PORTB |= 0x20
  restore SREG            interrupts back on
all that work, for one bit
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Inside digitalWrite(13, HIGH)

    digitalWrite(13, HIGH) first looks up pin 13 in tables stored in flash: port B, bit 5. It checks whether a timer drives the pin, pauses interrupts, sets that one bit in a register, then restores the interrupt setting it saved.

    digitalWrite(13, HIGH)   Arduino core, wiring_digital.c
      timer = table[13]   ->  none on pin 13
      bit   = table[13]   ->  _BV(5) = 0x20
      port  = table[13]   ->  port B
      save SREG, cli()        pause interrupts
      *out |= bit             PORTB |= 0x20
      restore SREG            interrupts back on
    all that work, for one bit
  2. Pin 13 is really PB5

    The Uno's LED, marked L, sits on pin 13. Inside the ATmega328P that pin is PB5: bit 5 of port B. A port is a group of up to eight pins, and each pin owns one bit in three registers.

    board pin 13  =  LED_BUILTIN, the L LED
    chip pin   PB5  =  port B, bit 5
    port B     8 bits: PB7 .. PB0
    each pin   one bit in PINB, DDRB and PORTB
  3. Registers live at addresses

    Port B's three registers are bytes in the chip's data memory: PINB at 0x23, DDRB at 0x24, PORTB at 0x25. avr-libc turns PORTB into *(volatile uint8_t *)0x25, a pointer to that byte. volatile makes every read and write really happen.

    data memory map (ATmega328P)
      0x0000-0x001F  32 registers
      0x0020-0x005F  64 I/O registers
      0x0060-0x00FF  160 ext I/O
      0x0100-0x08FF  SRAM 2 KB
    
      PINB  0x23  (I/O 0x03 + 0x20)  reads the pins; writing a 1 toggles PORTB
      DDRB  0x24  (I/O 0x04 + 0x20)  direction: 1 = output, 0 = input
      PORTB 0x25  (I/O 0x05 + 0x20)  output level: 1 = high, 0 = low
    
    #define PORTB  _SFR_IO8(0x05)
      ->  (*(volatile uint8_t *)0x25)
  4. Line 1: make PB5 an output

    1 << 5 shifts a 1 five places left: the mask 0b00100000, or 0x20. DDRB |= mask reads DDRB, ORs in the mask and writes it back. Bit 5 becomes 1, so PB5 is an output. The other seven bits keep their values.

    line 1   DDRB |= 1 << 5;
    mask     1 << 5  =  0b00100000  =  0x20
    read     DDRB    =  0b00000000
    OR       result  =  0b00100000
    write    DDRB bit 5 = 1  ->  PB5 is an output
  5. Line 3: LED on

    PORTB |= 1 << 5 sets bit 5 of PORTB the same way. PB5 is an output, so the pin is driven high, to 5 volts, and the LED lights. PINB bit 5 reads the pin, so it shows 1 too.

    line 3   PORTB |= 1 << 5;
    PORTB    0b00000000  |  0b00100000  =  0b00100000
    PB5      output + 1  ->  driven high, 5 V
    LED      on
    PINB     bit 5 reads the pin: 1
  6. Line 4: wait half a second

    _delay_ms(500) from avr-libc busy-waits: it just burns clock cycles. It must know the clock, F_CPU, which is 16 MHz on the Uno, so 500 ms is 8,000,000 cycles. Meanwhile PORTB keeps its value and the LED stays on.

    line 4   _delay_ms(500);
    F_CPU    16,000,000 Hz  (#define F_CPU 16000000UL)
    cycles   16,000,000 / 1000 * 500  =  8,000,000
    PORTB    unchanged: the LED stays on
  7. Line 5: LED off

    ~ flips every bit of the mask: ~0x20 is 0xDF, 0b11011111. PORTB &= 0xDF keeps every other bit and forces bit 5 to 0. The pin drops to 0 volts, the LED goes dark, and PINB bit 5 reads 0 again.

    line 5   PORTB &= ~(1 << 5);
    mask     ~0b00100000  =  0b11011111  =  0xDF
    PORTB    0b00100000  &  0b11011111  =  0b00000000
    PB5      output + 0  ->  driven low, 0 V
    LED      off
    PINB     bit 5 reads the pin: 0
  8. Lines 6 and 7: forever

    Another 500 ms wait, then while (1) jumps back to line 3: on, wait, off, wait, forever. That is one blink a second with no Arduino functions at all: one register sets the direction, one sets the level.

    line 6   _delay_ms(500);
    line 7   }  ->  back to line 3
    on 500 ms + off 500 ms  =  1 blink per second (1 Hz)
    
    // Arduino Uno R3 (ATmega328P, 16 MHz): blink the L LED
    // avr-gcc + avr-libc, no Arduino core
    #define F_CPU 16000000UL
    #include <avr/io.h>
    #include <util/delay.h>
    
    int main(void) {
      DDRB |= 1 << 5;
      while (1) {
        PORTB |= 1 << 5;
        _delay_ms(500);
        PORTB &= ~(1 << 5);
        _delay_ms(500);
      }
    }
  9. Shortcut: write PINB to toggle

    The datasheet adds a shortcut: writing a 1 to a bit of PINB flips that bit of PORTB. PINB = 1 << 5; toggles the LED with one write and no read first. The 0 bits in the value change nothing.

    PINB = 1 << 5;     one write, no read
      PORTB bit 5   0 -> 1 -> 0 -> ...
      bits written as 0: unchanged
    
    a shorter loop:
      while (1) {
        PINB = 1 << 5;
        _delay_ms(500);
      }
  10. Faster, but tied to one chip

    With optimisation on, avr-gcc can turn PORTB |= 1 << 5 into one sbi instruction: 2 clock cycles, 125 ns at 16 MHz. digitalWrite repeats its lookups on every call. The price: this code only runs on chips with this port B.

    PORTB |= 1 << 5;   ->   sbi 0x05, 5
    sbi                    2 cycles = 125 ns at 16 MHz
    digitalWrite(13, HIGH)  table lookups, timer check,
                            save SREG, cli(), restore
    
    registers: fast and small, but tied to the ATmega328P
    digitalWrite: slower, but the same code on many boards

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