IN is a 2-byte instruction and LDS is 4-bytes long.IN executes twice as fast as LDS (one machine cycle vs.
LDS?” IN has the following advantages over LDS: This may lead you to ask, “why use IN vs. IN performs virtually the same function as LDS (we covered LDS earlier here, if you would like to refresh your memory, pun intended). Remember, the IN and OUT instructions can only access the IO Registers between 0x00-0x3f. IN and its antipode, OUT are also used to load and store I/O locations to and from registers. Again, this instruction operates on the lower 32 I/O registers. CBI clears a specified bit in an I/O register. And keep in mind, the CBI and SBI instructions work with registers 0x00 to 0x1F only.ĬBI is the mnemonic for Clear Bit in I/o register, and is the antipode to SBI. This is not to be confused with the registers r0-r31. This instruction operates on the lower 32 MMIO addresses. SBI sets a specified bit in the register. Here are two key bit instructions: SBI is the mnemonic for Set Bit in I/o register. The Read-Write-Modify procedure is covered in our next tutorial which discusses bit manipulation. A Read-Modify-Write strategy properly preserves the unaffected bits of the register.
Since port registers contain bits controlling several pins (typically 0 through 7), simply writing to the port without regard to the other bits would overwrite the status of the other pins too. The typical method for addressing MMIO is through the use of a Read-Modify-Write strategy. The fact that the Standard IO Registers are addressable by bit is a noteworthy feature of our Arduino. Notice how the bits of the port registers compose all of the individual pins: Hopefully, we can clear up some of this confusion.Īddr Name Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0Ġx03 PINB PINB7 PINB6 PINB5 PINB4 PINB3 PINB2 PINB1 PINB0Ġx04 DDRB DDB7 DDB6 DDB5 DDB4 DDB3 DDB2 DDB1 DDB0Ġx05 PORTB PORTB7 PORTB6 PORTB5 PORTB4 PORTB3 PORTB2 PORTB1 PORTB0Ġx06 PINC x PINC6 PINC5 PINC4 PINC3 PINC2 PINC1 PINC0Ġx07 DDRC x DDC6 DDC5 DDC4 DDC3 DDC2 DDC1 DDC0Ġx08 PORTC x PORTC6 PORTC5 PORTC4 PORTC3 PORTC2 PORTC1 PORTC0Ġx09 PIND PIND7 PIND6 PIND5 PIND4 PIND3 PIND2 PIND1 PIND0Ġx0A DDRD DDD7 DDD6 DDD5 DDD4 DDD3 DDD2 DDD1 DDD0Ġx0B PORTD PORTD7 PORTD6 PORTD5 PORTD4 PORTD3 PORTD2 PORTD1 PORTD0Ġx1E GPIOR0 General Purpose IO Register 0Ġx1F EECR x x EEPM1 EEPM0 EERIE EEMPE EEPE EEREĠx21 EEARL EEPROM Address Register Low ByteĠx22 EEARH EEPROM Address Register High ByteĠx24 TCCR0A COM0A1 COM0A0 COM0B1 COM0B0 x x WGM01 WGM00Ġx25 TCCR0B FOC0A FOC0B x x WGM02 CS02 CS01 CS00Ġx27 OCR0A Timer/Counter 0 Output Compare Register AĠx28 OCR0B Timer/Counter 0 Output Compare Register BĠx2A GPIOR1 General Purpose IO Register 1Ġx2B GPIOR2 General Purpose IO Register 2Ġx2C SPCR SPIE SPE DORD MSTR CPOL CPHA SPR1 SPR0Ġx30 ACSR ACD ACBG ACO ACI ACIE ACIC ACIS1 ACIS0Ġx35 MCUCR x BODS BODSE PUD x x IVSEL IVCEĠx37 SPMCSR SPMIE RWWSB SIGRD RWWSRE BLBSET PGWRT PGERS SPMEN To further confuse matters, when using the LD and ST instructions, 0x20 must be added to the IO Register number. The 160 bytes of Extended IO registers are located at 96-255 ( 0圆0 – 0xff). The IN and OUT instructions can only access the Standard IO registers below 64 ( 0x3f and below). Above this area, IO registers are addressable only by byte value. The lower IO register portion of the SRAM is 32 bytes long (addresses 0 through 31, or 0x1f hexadecimal) and is further distinguished by the fact that it is bit-accessible using the SBI, CBI, SBIS and SBIC instructions. Actual SRAM is available behind the IO register area, starting at address 0x100 (otherwise known as RAMSTART). The actual locations can be found towards the rear of the data sheet for the arduino’s ATmega microcontroller. The second section is for the Extended IO Registers, which includes a mishmash of peripherals. The first section is for Standard IO Registers (primarily the ports, but includes various other peripherals as well). The IO register space is mapped into the SRAM just above these 32 GPRs and is divided into two sections. We have seen that the bottom of the SRAM address space is reserved for direct access to the general purpose registers ( r0-r31). A port typically has 8 pins assigned to it, which means a port maps nicely to a byte memory location. Changing the content of the memory in this area changes the value controlling a peripheral.Ī pin is a single addressable input/output line on a port.
MMIO means that a part of the SRAM space is reserved for registers that control peripherals (i.e., ports, timers, SPI, USART, etc.). Again, here we are interested in SRAM data memory, which the arduino uses as memory-mapped IO. The 3 basic types of memory in the arduino were discussed in tutorial #1.