Hi all,
At one time or another, most of us have needed to toggle an output pin on and off. Usually, it's done by writing a high or low to the output pin.
But, did you know that writing to an INPUT toggles the output? Check out this code example:
DDRB |= _BV(6); // enable PORTB, bit 6 as an output
while (1) {
PINB |= _BV(6); // toggle port b, bit 6
delay (100); // just a delay
}
I've searched Google for this info and didn't run into anything about it. But, I noticed it in an AVR datasheet, tried it and for sure it works!!!!!!
See page 75 of the ATMega328p datasheet:
Three I/O memory address locations are allocated for each port, one each for the Data Register – PORTx, Data Direction Register – DDRx, and the Port Input Pins – PINx. The Port Input Pins I/O location is read only, while the Data Register and the Data Direction Register are read/write. However, writing a logic one to a bit in the PINx Register, will result in a toggle in the corresponding bit in the Data Register. In addition, the Pull-up Disable – PUD bit in MCUCR disables the pull-up function for all pins in all ports when set.
Think about it... a few lines of code to add an Arduino function. Along with digitalWrite() and digitalRead() we could also have something like digitalToggle()!
For what it's worth......