Direct Port & Register Manipulation

Hey guys
I have a pro mini and what i want is turn State of A1 and A0 simultaneously

OLD WAY
if Something
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
else
digitalWrite(A1, LOW);
digitalWrite(A0, HIGH);
end

New way
if Something
PORTC = (PORTC & B11111101) | B00000001;
else
PORTC = (PORTC & B11111110) | B00000010;
end

Is this Correct? if considering the used platform(arduino pro mini.)

No, you messed up the bit positions.

You know the next question already. :)

Does anybody know how to resolve this

Look at the Register Summary in your datasheet, you will find PORTC, PINC and DDRC.

1/ Declare pin A0 and A1 as output without changing the other ones:
DDRC |= 1<< DDC0 | 1 << DDC1;

2/ If you want to set PORTC0 to 1 and PORTC1 to 0 without changing to other ones:

PORTC &= ~(1<< PORTC1);
PORTC |= 1 << PORTC0;

You could usePINC =  (1 << PORTC0) | (1<< PORTC1);
to toggle both pins with a single write instruction.

The A0 and A1 is at Port C? And do I have to declare them as outputs?

gc9n: The A0 and A1 is at Port C?

You claimed so in your opening post.

gc9n: And do I have to declare them as outputs?

Do you want to use the pins as outputs? If so, you obviously have to make them outputs.

Yes sorry. What I mean. The declaration will be deferent? The usual way it's for example Define xxx A0 Pinmode(xxx, OUTPUT)

A0 to Ax (x depending on your Arduino) are predefined and should not be redefined.

pinMode(A0, OUTPUT);

is one way to set A0 as output

DDRC |= 1<< DDC0;

should be the equivalent operation.

PINC = 1 << PORTC0;

toggles A0.

ok
i got an error at both declarations
DDRC |= 1<< DDC0;
DDRC |= 1<< DDC1;

or even if i use DDRC |= 1<< DDC0 | 1 << DDC1;
expected unqualified-id before ‘volatile’
am using nano now , with atmega328

void setup() {
  DDRC |= (1 << DDC0) | (1 << DDC1);
  PORTC |= 1 << DDC0;
}
void loop() {
  static unsigned long lastTime;
  unsigned long topLoop = millis();
  if (millis() - lastTime >= 1000) {
    lastTime = topLoop;
    PINC =  (1 << PORTC0) | (1 << PORTC1);
  }
}

Compiles and works like a treat on a Nano.

gc9n:
ok
i got an error at both declarations
DDRC |= 1<< DDC0;
DDRC |= 1<< DDC1;

or even if i use DDRC |= 1<< DDC0 | 1 << DDC1;
expected unqualified-id before ‘volatile’
am using nano now , with atmega328

Forget about it . i found it

Ok it works ! but they open and close together, they v got to work deferent .when the A0 is HIGH the A1 have to be LOW. and the oposite... Thanks in advance for the help

Look at my code.

It does exactly that. Switching both at the same time, one being LOW and one being HIGH.

Because both switch at the same time, on of them has to be set to HIGH on startup, I chose A0.

I think you have to include <avr/io.h> which brings in the correct *.h file for your chip.
for example I am using a uno and avr/io.h has this line in it:

#elif defined (AVR_ATmega328P)

include <avr/iom328p.h>

iom328p.h has all the ports,registers and pins defined so you can use DDRC,PORTA, PINC etc… without getting an error. You should get familiar with that include file.

noweare:
I think you have to include <avr/io.h> which brings in the correct *.h file for your chip.
for example I am using a uno and avr/io.h has this line in it:

#elif defined (AVR_ATmega328P)

include <avr/iom328p.h>

iom328p.h has all the ports,registers and pins defined so you can use DDRC,PORTA, PINC etc… without getting an error. You should get familiar with that include file.

the error was that i didnt include the declaration inside the setup

Whandall:
Look at my code.

It does exactly that. Switching both at the same time, one being LOW and one being HIGH.

Because both switch at the same time, on of them has to be set to HIGH on startup, I chose A0.

ok i tested yes , it does exactly that thanks.
understood what you telling me , i will start to play with it , just not need the toggle technique , i have to change them adhok .

I have a function that opens A and Closes B if the parameter is A
if its B closes A opens B

So you need to code those lines, providing you don't use any other PORTCx pins:

// A0 set to 1, A1 set to 0 PORTC = PORTC0;

//A0 set to 0, A1 set to 1 PORTC = PORTC1;

If you use PINC instead of PORTC, you will toggle your pins. You could, e.g. in your setup write this line: PINC |= PINC0; // Set A0 to 1 (and A1 is still set to 0)

Then in your loop(), each time your parameter changes, run this line: PINC |= PINC0 | PINC1; //toggle A0 and toggle A1

ard_newbie: PINC |= PINC0;

PINC |= PINC0 | PINC1;

That is dangerous and inefficient way to use the PINx toggle feature, it is designed to be written and not read, modified, written.

Whandall ok i am toggling the values and is working Great! Thanks.

Whandall just out of curiosity

Before this change

----Declaration-------
DDRC |= (1 << DDC0) | (1 << DDC1);

PINC = (1 << PORTC0) | (1 << PORTC1); Wich is defenetelly faster approach

i used this

----Declaration-------
#define MYVARIABLE A0
#define MYVARIABLE2 A1
pinMode(MYVARIABLE , OUTPUT);
pinMode(MYVARIABLE2 , OUTPUT);

if myCase
PORTC = (PORTC & B11111101) | B00000001; // turns off A1 and turns on A0
else
PORTC = (PORTC & B11111110) | B00000010;// turns off A0 and turns on A1

It does exactly the same thing (i talk for the result, i dont know other info)

is there any significant deference ? without any doubt i want to be lighting fast change