Using Port C as digital output

It took me a bit of time to extract this info from the docs and the board itself, so I thought I'd post it here so any searchers in the future will get there quicker.

This is for the Duemilanove

//Using Port C as digital output

In your setup: // Turn analog inputs to digital outputs DDRC=0xFF ; // These will be pin numbers 14 .. 19

In your loop: digitalWrite (14, HIGH); // ...etc... digitalWrite (19, LOW);

I don’t think there’s any need for anything in setup, it’s all already done by digitalRead/digitalWrite when you specify the correct pin numbers.

That’s true as long as you initialize the pins to OUTPUT before you use digitalWrite().

** pinMode(14, OUTPUT);**
** pinMode(19, OUTPUT);**

That’s traditionally done in setup().

// Turn analog inputs to digital outputs DDRC=0xFF ; // These will be pin numbers 14 .. 19

The DDRC register determines data direction (input or output) for port-C. Setting all bits to one (as in your code) will configure the pins for output. It is not related to switching between analog/digital (no command is needed for this).

Port C includes the RESET pin on PC6 and an undefined pin on PC7. PC0 thru PC5 map to Arduino A0 (14) thru to A5 (19). Forcing RESET to become an output pin (as would be the case with your statement) is not something you want to do unless the ATmega chip is refused to disable/ignore RESET. Also it is not good practice to force an undefined pin (A7) to any value - rather you should leave it as is (DDRC|=0x3f would be ok).

So, I should actually do:

// Turn analog inputs to digital outputs DDRC=0x3F ; // These will be pin numbers 14 .. 19

is that right?

I posted this because I could not find any clear examples of using these pins.

And you say:

"PC0 thru PC5 map to Arduino A0 (14) thru to A5 (19)."

which I understand, but what is the advised way of naming these pins. 14 ... 19. I can't find the C define for "A0" or PC0.

The Arduino way would be as follows:

  pinMode(14,OUTPUT); // configure A0 for output
  digitalWrite(14,HIGH); // set PORTC pin 0 high

Direct PORT IO is as follows:

  DDRC |= _BV(PORTC0); // configure A0 for output
  PORTC |= _BV(PORTC0); // set PORTC pin 0 high