Like basic C language I tried making a define as follows

#define RED PA0
#define ON 1  
#define OFF 0

void main(void){

    DDRA = 0xFF;


        RED = ON;

I get an error which I havn't seen before:

lvalue required as left operand of assignment

What does this mean?

  • 1
    \$\begingroup\$ What AVR Compiler are you using? Have you included the specific AVR header files? \$\endgroup\$ – Dean Jun 18 '12 at 10:07
  • \$\begingroup\$ yeah i'm using AVR studio 6.0, debugger is AVR dragon and the header files I have used are #include <avr/io.h> #include <util/delay.h> #include "iom16.h" \$\endgroup\$ – David Norman Jun 18 '12 at 10:12
  • \$\begingroup\$ Whats the compiler? Winavr? Some of the headers are suggesting your using code for different compilers. The error is because it can't find the name of either the port or data directory register. This might be of some use \$\endgroup\$ – Dean Jun 18 '12 at 10:37

'L-value' and 'R-Value' describe attributes of the Left-hand side and Right-hand side, respectively, of an assignment. An R-value is a quantity of some kind (not necessarily numerical), usually the result of evaluating an expression, but also a constant, such as '5' or the string "Hello, World!\n". An L-value is a something that a program can assign an R-value to, such as a named variable, or a raw memory or port address. This WikiPedia article has a more complete description.

"lvalue required as left operand of assignment" simply means that the left side of your assignment isn't an assignable address or convertible to one. PA0 is #defined as a simple constant without l-value properties.

You're on the right track - here is an Arduino article about ports and the I/O registers associated with them, and direct manipulation of those registers. Despite its caveats, direct port manipulation is a useful technique for reducing both program size and timing-skew in some situations.

These statements:

#define RED_ON (PORTA | 0x01)
#define RED_OFF (PORTA & ~0x01)



will both define an l-value you can assign to (but note: it's a whole port, not just a pin) and change its value to turn on pin-0. The RED_ON expression reads: read PORTA, OR that with '1' (so you won't change any other bits in the port); the assignment writes the new value back to the port.

(Do note that your code as written - even assuming the assignment statement was correct - will repetitively turn on the LED, which is no different from turning it on once).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.