Using the port F as digital I/O in Teensy++

I wanted to use the port F in Teensy++ board as ordinary digital inputs/outputs and it turned out that there is an extra step involved to ordinary DDR stuff. I’m coding using the Teensy port of core Arduino libraries to AT90USB646 chip. After checking the chip datasheet, I found that the proper port initialization includes setting the DIDR0 register (setting desired pins of port F to digital instead of analogue). So the full initialization should look like

#define IO_MASK B00000111  // three pins used (0, 1 and 2)
#define IO_DDR  B00000100    // pins 0 and 1 inputs, 2 output
#define IO_PINS B11111100

// Setting the three pins as digital:
DIDR0  &=   (~IO_MASK);

// Setting the direction of pins:
DDRF   |=   (IO_MASK  & IO_DDR);
DDRF   &=   (~IO_MASK | IO_DDR);

// Setting the initial state of the pins:
PORTF  |=   (IO_MASK  & IO_PINS);
PORTF  &=   (~IO_MASK | IO_PINS);

Now the three pins on port F can be used as normal digital inputs/outputs with usual Arduino digitalRead/digitalWrite functions or PORTF/PINF registers. Perhaps I explain the DIDR0 register more in details in the future. Until then, don’t fear to check the datasheet.

2 comments for “Using the port F as digital I/O in Teensy++

  1. July 7, 2014 at 5:56 pm

    I have the same problem too, cannot use PORTF as digital outputs 🙁
    I tried your code, but it didn’t work for me 🙁

    On datasheet DIDR0 refers to (optional) power saving and not to enabling digital input. This register should default to 0, and therefore digital inputs for portF be enabled by default.

    So maybe the issue is elsewhere? I checked the datasheet, but it doens’t mention explicity instructing chip to use ADC or Digital, they should both be enabled and you can use one or the other without any initialization routine.

  2. July 8, 2014 at 5:29 am

    My issue was the JTAGEN fuse that prevented using PortF as digital I/O (they default to JTAG). After programming JTAGEN bit in fuses to 1, PORTF worked. Default hFuse is 0x99, instead i programmed hFuse 0xD9 to use PORTF as I/O instead of as JTAG.

    Regarding setting DIDR0, for me that’s done automatically by teensyduino core and i did not need to do myself. I’m not sure that’s needed, tough, on datasheet DIDR0 seems related to power saving, and not to enabling the port.

Leave a Reply

Your email address will not be published. Required fields are marked *