Creating a function using to PORT manipulation to mimic digitalWrite

So the project I have is to write a function in C (using ‘PORT style’ programming) called, ‘my_digitalWrite ’ that takes as its first argument, the number of an Arduino pin, and as its second argument, either the value HIGH or LOW, so that when your function is invoked, the corresponding bit in the PORTn register will be set or cleared.

So if I choose lets say pin 3 that is set as an OUTPUT and I want to make it go HIGH, I would call my function like:

my_digitalWrite(3,HIGH);

I figure I will be using if statements to turn on the right port.

I have is to write a function in C

Or use:
http://arduino.cc/en/Reference/SwitchCase

See image:

So have you had a look at the data sheet for the atmega328 ? Smart | Connected | Secure | Microchip Technology

Read all of section 13 but pay particular attention to sections 13.2 and section 13.4

Can you identify which 3 register bits need to be set/cleared to make pin 3 an output and high?

I award 10/10 for the thirst for knowledge and the willingness to learn AVR internals. But I feel I would be remiss if I failed to mention this:

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

Perhaps you can “cheat” and peek at the source code if you get stuck. :slight_smile:

is to write a function in C

Do you know how to write a "function" ?

LarryD:

is to write a function in C

Do you know how to write a "function" ?

Not really. I think I need to put like, this is just a guess since I'm not sure
int my_digitalWrite (pin, state);

bakeineer:
int my_digitalWrite (pin, state);

That looks like a declaration. It also says the function will return an "int". I can't imagine what you would want to return. If there is no need to return a value, you should declare it asvoid my_digitalWrite(pin,state);
But you really only need to declare it if your creating a library. When your function is in the project source code like this you can skip the declaration and get on with the implementation.

To implement the function you would have curly braces and statements like

void my_digitalWrite(pin,state)
{
   do stuff;
   do more stuff;
}

And then you already know how to call it

bakeineer:

my_digitalWrite(3,HIGH);
void my_digitalWrite(pin,state)
{
   do stuff;
   do more stuff;
}

Just add your ifs or switch cases in the suggested function offered above.

Take some time to review these two links:

http://playground.arduino.cc/Code/Function

Rather than duplicate alreadu existing functions may I suggest that you write a function to work with a specific Arduino port and to write and (in another function) read all the available pins at the same time. My objective is to make it easier to do what can be done with PORTx and PINx.

The write function could take two values a port number (or letter) and a byte value of which the low n bits would be written to the Port. N may need to be different for different ports. Even better (if it works) would be to identify the port by a string representing the range of pin numbers eg "0813" would refer to port B on an Uno.

Just a suggestion ...

...R