Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Getting Started with C Programming for AVR Atmega-328

February 3, 2024

Getting Started with C Programming for AVR Atmega-328

Welcome to this beginner’s guide on C programming for AVR Atmega-328 microcontrollers! In this post, we will cover the basics of setting up the development environment, writing a simple C program, and uploading it to the microcontroller.

Prerequisites

Before diving into AVR Atmega-328 programming, make sure you have the following:

Setting up the Development Environment

  1. Install the AVR Toolchain: Download and install the AVR-GCC compiler, avr-libc, and avrdude. These tools are required for compiling C code and uploading it to the microcontroller.

  2. Connect the AVR Programmer: Connect your AVR programmer (e.g., USBasp) to the computer via USB.

  3. Connect the Microcontroller: Connect the AVR Atmega-328 microcontroller to the AVR programmer using the appropriate connections (MISO, MOSI, SCK, RESET, and VCC/GND).

  4. Choose an Integrated Development Environment (IDE): Several IDEs are available for AVR programming, such as Atmel Studio, Code::Blocks, or simply using a text editor and command-line tools.

Writing Your First C Program

Let’s start by writing a simple program that blinks an LED connected to pin 13 of the microcontroller.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    // Set pin 13 as output
    DDRB |= (1 << DDB5);

    while (1) {
        // Toggle pin 13
        PORTB ^= (1 << PORTB5);
        _delay_ms(500);
    }

    return 0;
}

In this program, we include the necessary AVR header files (<avr/io.h> and <util/delay.h>) for handling I/O pins and delay functions. The main() function is the entry point of the program.

We set pin 13 (mapped to the PORTB5 pin) as an output pin using the Data Direction Register (DDRB). Inside the infinite while loop, we toggle the state of pin 13 using the PORTB register and delay for 500 milliseconds using _delay_ms() function from <util/delay.h>.

Compiling and Uploading the Program

Now, let’s compile and upload our program to the microcontroller.

  1. Compile the C Program: Open a terminal or command prompt and navigate to the folder containing the C program. Use the following command to compile the program:

    1
    
    avr-gcc -mmcu=atmega328p -Wall -Os -o output.elf program.c
    

    This command compiles the program (program.c) and generates an output file (output.elf) targeting the Atmega-328 microcontroller.

  2. Upload the Program: Use the following command to upload the program to the microcontroller:

    1
    
    avrdude -c usbasp -p m328p -U flash:w:output.elf
    

    This command uses avrdude with the usbasp programmer (-c usbasp) to program the Atmega-328 (-p m328p) with the compiled program (-U flash:w:output.elf).

Once the upload finishes successfully, you should see the LED connected to pin 13 blinking at a 500ms interval.

Conclusion

Congratulations! You have successfully written, compiled, and uploaded your first C program to an AVR Atmega-328 microcontroller. This post covered the necessary steps to set up your development environment, write a simple C program, and upload it to the microcontroller. Now you can start exploring more advanced features and functionalities of AVR programming.

Remember to refer to the AVR Atmega-328 datasheet and documentation for detailed information on pin configurations, registers, and other features specific to the microcontroller. Happy coding!


➡️ Understanding Python stack trace


⬅️ List comprehension in Python


Go back to Posts.