[Solved] Writing to port input registers

Hello,

I thought PINx registers were read-only, yet I see that in optiboot.c

#define LED_PIN     PINB
...
LED_PIN |= _BV(LED);

How is this possible?

Thinking something is less reliable than reading the datasheet!

writing to PINx registers toggles those pins for which a 1 was written, regardless of the state of the corresponding PORTx register. This allows fast and compact bit-bashing as in the example (bootloader needs to be compact)

SilentMusic: Hello,

I thought PINx registers were read-only, yet I see that in optiboot.c

#define LED_PIN     PINB
...
LED_PIN |= _BV(LED);

How is this possible?

I believe it only applies if the I/O pin is defined to be an output pin (corresponding bit in the DDRx register set to 1), then writing a one bit in the PINx register will cause the corresponding bit in the PORTx register to toggle, that is if it was a one it will change to 0 and visa versa. Some older AVR micros did not support this added 'feature'.

Lefty

Thanks! Everything I read from googling "avr port input registers said they were read only... Should've read the datasheet too.

retrolefty: I believe it only applies if the I/O pin is defined to be an output pin (corresponding bit in the DDRx register set to 1), then writing a one bit in the PINx register will cause the corresponding bit in the PORTx register to toggle, that is if it was a one it will change to 0 and visa versa. Some older AVR micros did not support this added 'feature'.

Lefty

Again the datasheet shows that the the mechanism using PORTx and PINx is independent of the DDRx register - there is a detailed circuit diagram for an I/O pin (its pretty hairy!). The DDRx flip-flop only controls the tri-state output, doesn't affect the PORTx/PINx state.