Use crystal pins for DIO?

I've been using my 168 and 328 chips with the internal oscillator now for a while. Change fuses and Upload programs using tinyisp in-cicuit serial programmer.
So now I have an application where I need one more DIO pin and I'm looking at those two unused crystal pins. They are labeled as part of port B so I would think I could use them somehow.
How to do?

Thanks,
Rich

have you tried using direct port addressing? I think that would have to work. Arduino Playground - HomePage

Port manipulation?
No, not yet. I was hoping to continue to use the Arduino pin numbering scheme.
But I'll take a look.

I added a couple lines to the arrays in pins_arduino.c
This makes PB6 digital pin 20 and PB7 digital pin 21. I did a quick test using digitalRead() and digitalWrite() and they work so far.
Since I did not take the time to read all the code I suspect this will bite me in the you know where at some inconvenient time.

const uint8_t PROGMEM digital_pin_to_port_PGM[] = {
        PD, /* 0 */
        PD,
        PD,
        PD,
        PD,
        PD,
        PD,
        PD,
        PB, /* 8 */
        PB,
        PB,
        PB,
        PB,
        PB,
        PC, /* 14 */
        PC,
        PC,
        PC,
        PC,
        PC,
        PB, /* 20   */
        PB  /* 21   */
};

const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] = {
        _BV(0), /* 0, port D */
        _BV(1),
        _BV(2),
        _BV(3),
        _BV(4),
        _BV(5),
        _BV(6),
        _BV(7),
        _BV(0), /* 8, port B */
        _BV(1),
        _BV(2),
        _BV(3),
        _BV(4),
        _BV(5),
        _BV(0), /* 14, port C */
        _BV(1),
        _BV(2),
        _BV(3),
        _BV(4),
        _BV(5),
        _BV(6), /* 20 */
        _BV(7), /* 21 */
};

Nice, that could come in handy at some point

Mowcius

This is somewhat related...

Does anybody know the consequences, if any, of fiddling with the XTAL pins while actually using the external clock? I'm not interested in ditching the crystal, I'm just wondering what could possibly go wrong if I don't avoid changing bits 6 & 7 of DDRB and PORTB.

Have you changed the fuses to use the internal clock?

No, I do not want to change any behavior of the clock. I've looked through the atmega328p datasheet a little, but I haven't yet determined for sure what would happen if, say, I set the XTAL pins to low impedance ( DDRB |= 0xc0 ).

I'm guessing ( only guessing ) that changing bits 6 & 7 of PORTB will have no effect as long as 6 & 7 of DDRB are 0, but I suspect setting those pins to low impedance will screw up the oscillator regardless of the value of PORTB.

Alright, page 84 of the datasheet, the umpteen signals that control the port are laid out. The pull-up resistors for both pins should be overridden and disabled because there isn't an external clock or internal RC in use. So there's no harm in writing to PORTB as long as DDRB[6:7] are 0. Further, the output drivers should be overridden and disabled by the same logic. So the values of DDRB[6:7] are irrelevant anyway. I think reading the value is also disabled.

So, in conclusion, with the crystal all set up by the CKSEL fuses, port B pins 6 and 7 should be totally disabled. It should be impossible to harm anything by writing DDRB[6:7] and PORTB[6:7] (they are ignored by the port circuit).

So I'm gonna try it.

DDRB = 0;
PORTB = 0xff;
uC still works fine. PORTB reads back 0xff.

DDRB = 0xff;
PORTB = 0;
uC still works fine. PORTB reads back 0.

DDRB = 0xff;
PORTB = 0xff;
uC still works fine. PORTB reads back 0xff.

So there. There's no consequence.