DEFINE function not Defining???

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

Hi again.

 

Just wondering if anyone can help me with a problem?

 

I'm picking up where I left off a few years back, this time using Studio 7.

 

I'm trying to use the #DEFINE function in this type of format: -

 

#define EW_RED_LIGHT PORTB0

 

and later in my code, I'm using this instruction: - 

 

EW_RED_LIGHT = 1;

 

Upon compiling, I get the following error: - "Value required as left operand of assignment"

 

What am I doing wrong? These seem to me to be acceptable commands?

 

Thanks in advance for any help.

 

 

This topic has a solution.
Last Edited: Fri. Sep 29, 2017 - 08:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You probably need:

PORTB |= (1 << EW_RED_LIGHT);    // Set bit B0 in PORTB
PORTB &= ~(1 << EW_RED_LIGHT);    // Clear bit B0 in PORTB

David (aka frog_jr)

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

#define is correctly known as a preprocessor directive.

 

jezhoyland wrote:
I'm using this instruction: -   

 

EW_RED_LIGHT = 1;  

 

Upon compiling, I get the following error: - "Value required as left operand of assignment"

 

Remember that the preprocessor is merely a text substitution engine; so what the compiler actually sees is

 

PORTB0 = 1;

 

What do you get if you do that directly?

 

If you look-up the definition of PORTB0 - does it make sense on the left-hand side of an assignment ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You probably need:

Or:

#define EW_RED_LIGHT_PORT PORTB
#define EW_RED_LIGHT_BIT      0
.
.
.
EW_RED_LIGHT_PORT |=  (1 << EW_RED_LIGHT_BIT);    // Set bit B0 in PORTB
EW_RED_LIGHT_PORT &= ~(1 << EW_RED_LIGHT_BIT);    // Clear bit B0 in PORTB

The moral is that #define is not a 'function'.  It is a directive which tells the preprocessor to substitute text in place of the identifier.

 

In the OP's code, the compiler would see:

PORTB0 = 1;

... and since PORTB0 is itself #define'd as a constant (0, in fact), the compiler really sees:

0 = 1;

... which is (obviously) not a valid expression in C.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Thu. Sep 28, 2017 - 04:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Instructions for posting source code: https://www.avrfreaks.net/comment...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, That's completely error free now, appreciate the help, thanks again.

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

Excellent!

 

So please mark the solution: https://www.avrfreaks.net/comment...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...