Is it OK to use macros like this?

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is it OK to use macros like this? Does it violate some golden rules of programming?

#define inputPin ((PINA >> 0) & 0x01)
#define HIGH 1
#define LOW 0

// *SETUP PORTA*
// Pin40, PA0: Input, InputPin
// Pin39, PA1: Output, OutPutPin
// Pin38, PA2: OutPut for LED
// Pins:   76543210
// Values: 00000110  0x06
DDRA = 0x06

while(inputPin == LOW)
{
    //Blink LED
    // LED ON
    PORTA |= (1 << PA2);
    _delay_ms(200);
    //LED OFF
    PORTA &= ~(1 << PA2);
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It doesn't violate any rule that I'm aware of.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Does it violate some golden rules of programming?
While your macro may not, your code does. Let me guess, the led does not flash as expected? Always on? (it's a common problem with led flashing programs) :wink:

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
(PINA >> 0)

I'm not sure why you would do this. PINA shifted 0 places is just PINA.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I'm not sure why you would do this. PINA shifted 0 places is just PINA.
I see your point and agree. I just did this to keep the format the same as in the rest or my code for the other pins on that port, ie in the places where I do (PINA >> 1) or (PINA >> 2).

Quote:
Let me guess, the led does not flash as expected? Always on? (it's a common problem with led flashing programs)
I have not tested this exact program with hardware yet. However I cut-paste from another of my programs that does blink the LED and then stay off. However, in that program I was not using a macro. Maybe that is the issue? Where exactly are you thinking there is an issue?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You set the led on, wait a long time, turn the led off, and immediately turn it on again. You won't have time to see it is off.

Another thing is that it is more optimal to say

PINA & (1<<5)

than

(PINA>>5) & 0x01

because of compiler optimizations.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
while(inputPin == LOW) 
{ 
    //Blink LED 
    // LED ON 
    PORTA |= (1 << PA2); 
    _delay_ms(200); 
    //LED OFF 
    PORTA &= ~(1 << PA2);
    _delay_ms(200);  
} 

Notice the difference? :)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You might also want to read [TUT] [C] [GCC] How to define a pin as a variable

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Notice the difference?
I see what you are saying. The LED part of this code was put in just as an example, from another project where this code was only called once. My real questions was about the macro. Thanks for your suggestion.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Another thing is that it is more optimal to say

Code:

PINA & (1<<5)


OK, this is good to know. Thanks.