2.12) Simple C Programs: Data Transfer Operation

posted by Hamid Sayyed • November 11, 2025 0 Comments

Data transfer is one of the most essential operations in programming a microcontroller. In an embedded system, almost everything revolves around transferring data — whether it’s moving a value from one variable to another, sending a byte to an output port, reading an input pin, or copying data from memory to a register. Understanding how these transfers happen at both the C language and hardware levels helps students develop efficient and reliable code. In simple terms, a data transfer operation moves information from a source to a destination. The source can be a constant, variable, register, or peripheral device, while the destination is where that value is stored or used. In AVR C programming, these transfers are performed using assignment operators, increment/decrement operators, bitwise operations, and port manipulation instructions. These form the foundation of every embedded program — from blinking an LED to reading sensor data. In this post, we will explore each type of data transfer operation, understand how it works, and write simple C programs to demonstrate their behavior on an AVR microcontroller.

1. What is a Data Transfer Operation?

A data transfer operation allows the movement of information from one location to another within the system. This could mean transferring data between:

  • Registers and memory
  • Memory and input/output ports
  • Constants and variables
  • Between two variables in memory

In C language for AVR, these operations are represented by various assignment and arithmetic expressions. Every time a variable receives a value, the compiler generates instructions that transfer this data within the microcontroller.

Example: When you write x = y; in C, the compiler generates assembly code to move the contents of memory location y into x. This is a simple data transfer operation.

2. Basic Assignment Operation

The most fundamental data transfer operator in C is the = assignment operator. It assigns the value on the right-hand side to the variable on the left-hand side.

OperatorMeaningExample
=Assign valuea = 10;
+=Add and assigna += 2; // same as a = a + 2
-=Subtract and assigna -= 3;
*=Multiply and assigna *= 4;
/=Divide and assigna /= 2;
#include <avr/io.h>

int main(void)
{
    uint8_t a = 5, b = 10;
    a = b;        // Transfer value of b into a
    a += 2;       // Add 2 and assign to a
    b -= 4;       // Subtract 4 from b
    while(1);
}

In the above code, the statements move data between registers and memory. The AVR compiler automatically generates MOV or LD/ST instructions for these transfers.

3. Increment and Decrement Operators

Another simple but important form of data transfer happens during increment (++) and decrement (--) operations. These operators increase or decrease the value of a variable by one and then store it back — effectively reading, modifying, and writing data.

uint8_t count = 0;

count++;  // Increment value (count = count + 1)
count--;  // Decrement value (count = count - 1)
In AVR assembly, these are performed using INC and DEC instructions. Such small operations are highly optimized and execute in just one CPU cycle.

4. Data Transfer Between Registers and I/O Ports

Microcontrollers like AVR have several Input/Output (I/O) ports. Each port is controlled by three registers — DDRx (Direction), PORTx (Output), and PINx (Input). Data transfer between CPU and these ports allows communication with the external world such as LEDs, sensors, switches, etc.

#include <avr/io.h>

int main(void)
{
    DDRB = 0xFF;   // Configure Port B as output
    PORTB = 0xAA;  // Send data pattern 10101010 to Port B

    while(1);
}

Here, the pattern 0xAA (binary 10101010) is transferred from CPU register to PORTB register, and physically appears as HIGH and LOW voltages on the corresponding pins.

5. Input Data Transfer from Pins

Reading the state of switches or sensors involves transferring data from I/O pins to CPU registers. This is done using the PINx register.

#include <avr/io.h>

int main(void)
{
    DDRC = 0x00;  // Configure Port C as input
    uint8_t data;

    while(1)
    {
        data = PINC;   // Read logic levels of all Port C pins
    }
}

The values read from the input pins are stored in the variable data, allowing the program to make decisions based on external signals.

6. Bitwise Data Transfer Operations

Often, we need to transfer or modify specific bits without affecting others. This is done using bitwise operators like &, |, ^, and ~.

// Set bit 0 of PORTB
PORTB |= (1 << 0);

// Clear bit 3 of PORTB
PORTB &= ~(1 << 3);

// Toggle bit 2
PORTB ^= (1 << 2);
Bitwise operations are the backbone of embedded data transfers. They directly manipulate control bits that drive LEDs, motors, or read digital inputs efficiently.

7. Data Transfer Using Arrays and Pointers

Arrays and pointers are also frequently used for transferring multiple data items. For example, transferring sensor readings into a buffer, or sending strings to UART.

uint8_t buffer[4] = {1, 2, 3, 4};
uint8_t i;

for(i = 0; i < 4; i++)
{
    PORTB = buffer[i];  // Transfer each element to output port
}

8. Summary of Data Transfer Instructions

OperationC ExpressionAssembly Equivalent
Assignmenta = b;MOV / LD / ST
Incrementa++;INC
Decrementa--;DEC
Port OutputPORTB = data;OUT
Port Inputdata = PINB;IN
Bit ManipulationPORTB |= (1<<PB0);SBI / CBI

Conclusion

Data transfer operations are the foundation of microcontroller programming. They define how information moves between the CPU, memory, and peripherals. By mastering assignment, bitwise, and port operations in C, students can control hardware precisely and efficiently. Every larger task — such as serial communication, sensor reading, or display driving — ultimately depends on these basic transfers. Therefore, a clear conceptual understanding of data transfer operations helps build a strong base for all future embedded programming work.

Comments

Post a Comment

Subscribe to Post Comments [Atom]