When possible why isn't digitalWrite and read also pinMode replaced?

I think it would make more sense if when possible (if the pin is a constant) the arduino IDE's preprocesser replaced digitalWrite,digitalRead and pinmode with direct port manipulation. It would combine the ease of use with the speed of port manipulation. It would also save abit of flash memory. for example digitalWrite(13.HIGH) get replaced with PORTD|=B100000; unsigned char x; //do something with x digitalWrite(13,x); gets replaced with if (x) PORTD|=B100000; else PORTD&=~B100000;

What about this?

int somePin = 13;
int someValue = HIGH;
digitalWrite(somePin, someValue);

Arrch: What about this?

int somePin = 13;
int someValue = HIGH;
digitalWrite(somePin, someValue);

Woops almost forgot about that the pin may be determined on execution. I will change my post to reflect that.

Keep in mind that the port assignments are different for 328 Vs the other avr chips the IDE supports so there is some portability given up when you use direct port addressing in your sketch.

Lefty

retrolefty: Keep in mind that the port assignments are different for 328 Vs the other avr chips the IDE supports so there is some portability given up when you use direct port addressing in your sketch.

Lefty

That is why you select if you are using the arudino mega or arduino uno or other board. Then the pre-processor will determine with bit in which port to set or clear.

http://code.google.com/p/digitalwritefast/

It would not be possible to do it.

digitalRead, digitalWrite, analogWrite, analogRead, etc all do far more than just setting a bit, or reading a value.

They manipulate the port settings to ensure that it is in the right state to perform the operation. If you wanted it to reduce it down to single SFR manipulations, then the preprocessor would need to parse and understand the whole flow of all of your program to know what state the ports are in at any particular time. Given the nature of the Arduino, and the amount of external stimulus that the precompiler cannot ever know about, this is impossible to do.

There have actually been several implementations of a "fast" digitalWrite() (and etc) function that generate as little as a single instruction for the case where the arguments are constants (and yet still handled the case where they weren't; there are several "interesting" ways of doing this.) In the end, none of them were accepted for inclusion in the Arduino core, for various reasons. I think the big problem turned out to be that most people agreed that having the performance of the function vary over a couple orders of magnitude, depending on the types of the arguments, was not a very intuitive thing to spring on beginners.