2.9) Programming of AVR in C: Operators

posted by Hamid Sayyed • November 11, 2025 0 Comments

In the world of embedded C programming for AVR microcontrollers, operators play a very important role in performing calculations, logical comparisons, and bit-level manipulations. Every instruction we write in C is built upon some form of operator usage. Whether you are turning an LED ON or reading a sensor value, you are using operators at every stage, directly or indirectly. In simple words, operators are symbols that tell the compiler to perform specific mathematical, logical, or bitwise operations. For AVR developers, a strong command of operators helps in optimizing programs, managing registers, and controlling hardware efficiently. In this detailed post, we will study all categories of operators used in AVR C programming, their syntax, purpose, and practical examples relevant to embedded systems.

1. What Are Operators?

Operators are special symbols that represent computations or actions. They act on variables or constants (known as operands) and produce a result. The C language supports a wide variety of operators, and all of them are available in AVR-GCC compiler used for AVR microcontrollers. Understanding these is essential for handling arithmetic, logic, and hardware-level tasks.

Example:
int a = 10, b = 5;
int result = a + b; // '+' is the arithmetic addition operator

2. Categories of Operators in AVR C

Operators in C (and thus in AVR programming) are divided into several categories as shown below:

Category Examples Description
Arithmetic Operators +, -, *, /, % Used for mathematical operations like addition, subtraction, etc.
Relational Operators ==, !=, <, >, <=, >= Compare two values and return true or false.
Logical Operators &&, ||, ! Used to combine multiple conditions in decision making.
Assignment Operators =, +=, -=, *=, /=, %= Assign or update values to variables.
Increment / Decrement ++, -- Increase or decrease variable value by one.
Bitwise Operators &, |, ^, ~, <<, >> Perform bit-level operations, very important in AVR hardware control.
Conditional (Ternary) ?: Used for compact conditional statements.
Special Operators sizeof, &, * Used for pointers, memory access, and measuring data size.

3. Arithmetic Operators

These are used for mathematical calculations like addition, subtraction, multiplication, division, and modulus. In AVR programs, they are commonly used for timing calculations, sensor scaling, or numeric operations.

Example:
int x = 20, y = 10;
int sum = x + y; // 30
int diff = x - y; // 10
int mul = x * y; // 200
int div = x / y; // 2
int mod = x % y; // 0

4. Relational and Logical Operators

These operators are used in if or while statements for decision-making. They check conditions like greater than, equal to, or less than, and return Boolean values (true or false). Logical operators allow combining multiple relational conditions together.

Example:
if ((temp > 50) && (speed < 100)) {
  PORTB = 0xFF; // Turn ON all LEDs
}

5. Bitwise Operators

Bitwise operators are the most powerful tools in AVR programming. They allow direct manipulation of bits, which is crucial when controlling hardware registers, ports, or flags. With these operators, we can easily turn ON or OFF a specific pin, shift data, or mask unwanted bits.

Operator Meaning Example
& Bitwise AND PORTB = PORTB & 0xF0;
| Bitwise OR PORTB = PORTB | 0x01;
^ Bitwise XOR PORTB = PORTB ^ 0xFF;
~ Bitwise NOT PORTB = ~PORTB;
<< Left Shift PORTB = 1 << 3; // Turns ON PB3
>> Right Shift data = data >> 2;
Bitwise Operation Visualization

6. Assignment and Increment/Decrement Operators

Assignment operators are used to store computed results in variables, whereas increment/decrement operators are handy in loops and repetitive tasks like delay generation or counter operations.

Example:
int i = 0;
i += 5; // same as i = i + 5
i++; // increments i by 1
i--; // decrements i by 1

7. Conditional (Ternary) Operator

The conditional operator is a compact form of the if-else statement. It is widely used in embedded code where decision-making needs to be written in a single line.

Example:
int ledState = (sensorValue > 100) ? 1 : 0;

8. Special Operators

The C language also supports special operators such as sizeof, &, and * which are very useful in embedded development. The sizeof operator is used to determine the size of a data type or variable in bytes, while the & and * operators are used with pointers to access memory directly.

Example:
int a = 10;
int *ptr = &a; // pointer points to memory address of a
printf("%d", sizeof(a)); // prints 2

9. Practical Usage in AVR Programming

  • Use bitwise operations to set or clear specific port pins.
  • Use logical operators for conditional sensor-based decisions.
  • Use arithmetic operators for timing and analog calculations.
  • Combine assignment and increment operators for counters or delays.

Conclusion

Operators are the heart of AVR C programming. They make our code compact, readable, and efficient. By mastering each operator and understanding its purpose, a student can easily control ports, handle conditions, and perform complex operations with simple expressions. Practice is key – try implementing arithmetic, bitwise, and logical operators in small LED or sensor-based projects to strengthen your programming logic and command over AVR architecture.

Comments

Post a Comment

Subscribe to Post Comments [Atom]