Firstly , when working with a microcontroller, as a rule, there is a way to indicate that the value of 8 contacts belongs to the same port at the same time.
You should try to figure this out from the Microcontroller (uC) manual / table by searching for a keyword ! Otherwise, you will find hellish time (indeed, I am NOT joking! At least I did it!) To find it, scattered in , but several of a hundred / thousand pages of manual / technical description.
Try it first and it will be much easier for you.
Secondly , in uC, when assigning high and low output, you usually do not need to take different functions, since they simply assign a value (high or low) to the same output. Thus, when possible, you should combine the high and low functions into a single function (since I do not know what is high and low inside, I can not help in the future, but you should mention this feature to facilitate your task)
Finally , however, if it cannot be executed for one reason or another, you can simplify your C code like this,
void assignPort(char val, char portBase){ int i = 0; for (i = 0; i < 8; ++i) if (val & (1 << i))
And to call the function just do
assignPort(0x38, 20); //notice the 38 using 0x for hex indicator while 20 is written as it is (assuming 20 is not hex)
Please note that if your high and low functions can be combined
void highLowCombined(char pinAddress, char highOrLow) {
your task is much simpler in calling high / low value functions and the val variable in assignPort above, since you just need to do it like this
void assignPort(char val, char portBase){ int i = 0; for (i = 0; i < 8; ++i) highLowCombined(portBase + i, val & (1 << i));
Now you only need one line in the for-loop ! Give it a try!