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:
- An AVR Atmega-328 microcontroller (commonly found in Arduino Uno board)
- AVR programmer (e.g., USBasp)
- AVR Toolchain (GCC compiler, avr-libc, avrdude)
- AVRdude GUI (optional but recommended)
Setting up the Development Environment
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.
Connect the AVR Programmer: Connect your AVR programmer (e.g., USBasp) to the computer via USB.
Connect the Microcontroller: Connect the AVR Atmega-328 microcontroller to the AVR programmer using the appropriate connections (MISO, MOSI, SCK, RESET, and VCC/GND).
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.
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.
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.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 theusbasp
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!