But when I received my Atmega32u4 Breakout Board+ I was a bit confused on how to get started programming it. The product page mainly covers hooking it up with the Teensydunio IDE but has little info on using programming in C and using gcc-avr, make and avrdude.
I presume a basic familiarity with using the Linux command line and how to install packages on your distribution. Some experience with programming C using make and basic electronics knowledge will might help but you can probably get through this tutorial without it. This tutorial is written for Linux users but it's probably not too different from people using WinAVR etc but I have never tried these so I cannot adapt it for it. Maybe a helpful forum user would be able to post amendments to make this work for other platforms.
This basic tutorial will cover getting all the right tools in place and hooking up an LED and writing, compiling and flashing a simple program that will flash the LED.
Requirements:
- Atmega32u4 Breakout Board+
- Mini-USB to USB cable to hook up the Breakout Board
- Breadboard
- LED and appropriate current limiting resistor*
- A computer running Linux
(5V - LED forward voltage drop) / LED forward current = resistance
Firstly, follow the product page up to the point where it mentions Teensyduino. You should have now have a Breakout Board with headers soldered and you can place it onto a breadboard and hook it up with your USB cable to your PC. You should see the red PWR LED light up.
Now on your computer make sure you have all the appropriate packages installed. You will need packages that contain:
- GNU Make
- avr-gcc
- avr-libc
- avrdude
If you have all of those then you can proceed. Make a new folder and cd into it in your terminal. Make a new file "main.c" and edit it to contain:
Code: Select all
int main(void) {
return 0;
}
To compile it we use gcc-avr:
Code: Select all
$ avr-gcc -mmcu=atmega32u4 -Wall -c main.c -o main.out
We will now determine what the serial interface on our Breakout Board. With the board still connected press the reset button on it. This should enable the bootloader on the Breakout Board and the green "Boot" LED should be dimming and lighting up (breathing). Now type "dmesg" in your terminal. You may have to do "sudo dmesg" depending on what privileges your distribution gives you. On the last few lines you should see something like this:
Code: Select all
[ 208.440047] usb 5-1: new full speed USB device number 2 using uhci_hcd
[ 208.836738] cdc_acm 5-1:1.0: ttyACM0: USB ACM device
[ 208.839901] usbcore: registered new interface driver cdc_acm
[ 208.839906] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapte
Now we will use avrdude to upload your compiled file to the Breakout Board. Press the reset button again to make sure the bootloader is enabled and then invoke avrdude while the Boot LED is still green:
Code: Select all
$ avrdude -p m32u4 -P /dev/ttyACM0 -c avr109 main.out
As we will be recompiling and re-flashing quite often, it makes sense to write a Makefile which will automate the task. Write a file called "makefile" that contains the commands we are using:
Code: Select all
all:
avr-gcc -mmcu=atmega32u4 -Wall -c main.c -o main.out
writeflash:
avrdude -p m32u4 -P /dev/ttyACM0 -c avr109 main.out
Code: Select all
$ rm -f main.out
Code: Select all
$ make
Code: Select all
$ make writeflash
Code: Select all
all:
avr-gcc -mmcu=atmega32u4 -Wall -c main.c -o main.out
writeflash:
avrdude -p m32u4 -P /dev/ttyACM0 -c avr109 main.out
clean:
rm -f main.out
Now, this hasn't been very impressive as the code does absolutely nothing. Let's now hook up the LED to pin B0. The long, positive end of the LED goes into the port, then to the resistor and the resistor goes to ground.
Now let's change the code in main.c to blink the LED:
Code: Select all
#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>
int main (void)
{
DDRB = 0x01; //set up pin 0 on port B
while (1) //loop forever
{
PORTB = 0x01; //set pin 0 on port B high
_delay_ms(500);
PORTB = 0x00; //set pin 0 on port B low
_delay_ms(500);
}
return 0;
}
We have to modify our makefile slightly to include compiler optimizations which the delay library needs. Change:
Code: Select all
avr-gcc -mmcu=atmega32u4 -Wall -c main.c -o main.out
Code: Select all
avr-gcc -mmcu=atmega32u4 -Wall -Os -c main.c -o main.out
There are people out there that are much better at writing makefiles than we are and it might be worth using a pre-written one instead. I recommend the one from psychogenic and with the following modifications it is usable for this project:
Code: Select all
MCU=atmega32u4
PROGRAMMER_MCU=m32u4
PROJECTNAME=main
PRJSRC=main.c
AVRDUDE_PROGRAMMERID=avr109
AVRDUDE_PORT=/dev/ttyACM0