How to setup defines in AVR Studio 6.0

avrcprogramming

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;

    while(1)
    {

        RED = ON;
    }
}

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

lvalue required as left operand of assignment

What does this mean?

Best Answer

'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 LED_PORT PORTA
#define RED_ON (PORTA | 0x01)
#define RED_OFF (PORTA & ~0x01)

...

LED_PORT = RED_ON;

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).