I'm trying to write a digital word of 8 bits long to the PORTC of an Arduino Nano.
I plan to write on the 8 analog inputs configured as outputs with DDRC=0xFF;
The problem I have is that I only get output on the first four bits of PORTC. I have also tried configuring each pin individually with pinMode(14,OUTPUT); and then blinking with digitalWrite(14,HIGH); and it also goes up only to pin17.
I guess I should work with the file on /hardware/cores/arduino/pins_arduino.c since it's written for boards like Diecimila that don't have access to the analog pins 6 and 7.
The standard arduino pin mappings don't support analog pins 6 & 7.
You can use eitther of these macros to write to the pins. The first one takes a pin number from 14 to 21 as follows:
fastWriteNano(21,HIGH); // set the most significant bit of port c high
// the macro sets or clears the appropriate bit in port C for pins 14-21
#define fastWriteNano(_pin, _state_) (_state_ ? PORTC |= 1 << (_pin - 14) : PORTC &= ~(1 << (_pin -14) ))
This macro is similar but it takes analog pin numbers
fastWriteA(0,HIGH); // set the most significant bit of port c high
// this macro does fast digital write on pins shared with the analog port, (pin 0 is digital pin 14)
#define fastWriteA(_pin_,_state_) (_state_ ? PORTC |= 1 << (_pin_ ) : PORTC &= ~(1 << (_pin_ ) ))
You still need to set the data direction as per your earlier post
Did that, with both macros and still can only get data out from A0 to A5. Do I need to change something else to get A6 and A7 to work as digital outputs?
Here's my code:
// the macro sets or clears the appropriate bit in port C for pins 14-21
#define fastWriteNano(_pin, _state_) (_state_ ? PORTC |= 1 << (_pin - 14) : PORTC &= ~(1 << (_pin -14) ))
int i;
void setup()
{
DDRC=B11111111;
}
void loop()
{
for (i=14;i<22;i++){
fastWriteNano(i,HIGH);
delay(200);
fastWriteNano(i,LOW);
delay(200);
}
}
I just had a look at a the datasheet and PC6 is shared with the reset pin, and although you can program it to replace the external reset function with IO, this will make it difficult to use the normal Arduino upload facility. It doesn't look like PC7 is available on the nano.
It looks like that macro is not really very useful for the nano.
Are you expecting the extra bits to show up on the extra analog pins on the Nano? I don't think it works that way; the extra pins are ONLY connected to the analog input multiplexer, and don't have (as far as I can tell from the datasheet) the normal digital pin circuitry.