Port manipulation and #define macro

Hello!

I am thinking to write a sketch where an arduino mega 2560 will pull several output pins low and high depending on the state of several input pins. I want to use port manipulation to do that. The code will probably be like 150 to 200 lines long and for that reason i fear that eventually i will make some mistake like confuse DDR, PORT or some bitwise operation and ruin the sketch.
I have very very poor knowledge of programing in general. That said, here is what i thought:
Maybe i can use the #define in order to avoid mistakes and "simulate" the digitalRead and digitalWrite functions.

I wrote a small test code that will turn off the built in led (on an uno board) if pin 2 goes low.
Arduino IDE does not generate any error and the code seems to work all right but i thought to ask because some times things are ment to be used in different ways. Maybe #define isn’t met to work like that?

Thank you !

//TURN OFF THE BUILT IN LED WHEN ARDUINO PIN 2 GOES LOW
//USING PORT MANIPULATION AND #define

#define PB5HIGH (PORTB = (1 << 5) | PORTB) // Turn built in led on
#define PB5LOW (PORTB = ~ (1 << 5) & PORTB) // Turn built in led off
#define CHECK_BTN_STATE ( (PIND & (1 << 2)) >> 2 )

void setup()
{
  DDRB = (1 << 5) | DDRB; // Seting PORTB 5th bit as output.
  DDRD = ~ (1 << 2) & DDRD; //Seting PORTD 2nd bit as input. Arduino pin 2.
  PORTD = (1 << 2) | PORTD;// Enable pullup on Arduino pin 2.
  PORTB = ~ (1 << 5) & PORTD; // Seting PORTB 5th bit low,
}

void loop()
{
  if (!CHECK_BTN_STATE) 
  {
  PB5HIGH;
  }
  else
  {
  PB5LOW;
  }
}

All seems reasonable.

If it works then you are wrong with this, “I have very very poor knowledge of programing in general”, turning into an expert. :wink:


I sometimes use similar to measure timing.

#define pulse13 PINB = 0x20; PINB = 0x20 //65ns pulse on pin 13

ex:
Here we prove it is 800ns faster to place flag checking at the front of a compound if() statement rather than at the end.

ex:
Measuring the tan() timing.

It may seem a trite thing to say, but learning to not make such mistakes is a part of programming,
in other words:

Reread and check everything carefully.

Figure out ways to test that will give confidence. (Perhaps adopt a TDD approach, ie write the tests
first)

Get someone else to review your code (reward them per bug found).

Structure the code so that reading it and testing it is easier.

ex:
Here we prove it is 800ns faster to place flag checking at the front of a compound if() statement rather than at the end.

you do know that it is because the the first condition is 'false' and therefore the second condition after the '&&' never gets evaluated (as if the first is 'true' and the logical '||' . Of course just checking a flag is quicker than doing a calculation on the result of a call to millis(), but if the flag is 'false' there is no difference in time (i hope !)

#define PB5HIGH (PORTB = (1 << 5) | PORTB) // Turn built in led on
#define PB5LOW (PORTB = ~ (1 << 5) & PORTB) // Turn built in led off
#define CHECK_BTN_STATE ( (PIND & (1 << 2)) >> 2 )

I think this is a very elegant solution. Note the braces around the whole definition. This assures that any operation within them is done by the compiler.

“ you do know that it is because the the first condition is 'false' ”

Both conditions are/were ‘false’


“ but if the flag is 'false' there is no difference in time (i hope !)”

When in front, testing the ‘flag’, when it gives a false result, terminates the statement.

Both conditions are/were 'false'

i thought i explained that if the first condition is false, the second does not get evaluated. (in case of &&), that it is quicker to evaluate a 1 Byte than a 4 Byte calculation which includes the result from a call to a function seems obvious.

I am getting a little bit off topic but i guess it is all right since i have my answer for the initial question.

I mentiond my very poor knowledge on programming not because i am beign modest but because it is a fact.

I first discovered the world of programming including embedded systems about a year ago and since then i try to learn as much as i can any way i can. It is not that i dont have the mental capacity to learn. what i lack is someone to teach/guide me. I can definetly not cafford to pay for classes and i struggle to learn from youtube or any other way possible. I have also tried a couple of books like (Pic microcontroller projects in C by ibrahim dogan) but it is nearly impossible for me to understand... Not to mention that the closest thing to a mentor for me when it comes to programming are you people here.

Anyways Thank you all. You made me a happy man today with your kind words.

getting a little bit off topic

Sorry 'bout that.

I have also tried a couple of books like (Pic microcontroller projects in C by ibrahim dogan) but it is nearly impossible for me to understand.

Have a look at C++ programming for dummies

Deva_Rishi:
Sorry 'bout that.

Dont worry about that. I was actually referring to me not anyone else :slight_smile:

Deva_Rishi:
Have a look at C++ programming for dummies

Awesome Thanks !

I wish there was something similarly easy to understand for embedded systems.