AVR C Programming - Atmel Studio



UpdatedUpdatedOctober 06 - 2017October 06 - 2017

Summary

In this tutorial we'll write a small program in Atmel Studio.
Then we'll create a .hex file and later we'll burn the .hex file into the microcontroller.

Find the connected port

  1. Connect the myAVR board to your computer via USB cable.
  2. Open device manager.
  3. Check Ports (COM & LPT) and find out the connected port. [In my case, I'll use COM3 Port.]

findPortWin.PNG


Create our first project in Atmel Studio 7

Open Atmel Studio 7 and click 'New Project...' to open the following window:

AtmelStudio7.PNG


  1. Select 'GCC C Executable Project'.
  2. It's better to organize all our files in directories. So I created a directory in E:\AVR\Studio where I'll save all my future project files.
  3. Name and Solution name will be your project name.
  4. If you select 'Create directory for solution', it will create separate directory "Debug" inside your master directory where all files (Makefile, .d, .o, .mk, .eep, .elf, .hex, .lss, .map, .srec) will be created.
  5. Click 'Ok'.
  6. Now select your device family (in our case it is ATmega8A).
  7. Click 'Ok' again. It will start creating your project.
  8. There is a default main.c file. We'll always work on this file for all the projects.

avr C code

Now copy-paste the following codes.



Create .hex file

Select Build >> Build Solution (F7). If there is no mistake, you'll see 'Build succeeded' message.

Flash the .hex file

Run avrdudeGui.exe. In 'Setup' tab, select Programmer: avr911 and Port: COM3 (if you use the same board like mine, programmer is avr911, for port find out which one you are using).

avrdudeGUI.PNG


  1. Don't do anything in 'Lock Bits', 'Fuse Bits' and 'Calibration'. If you don't know what they actually do, you might damage your microcontroller permanently!
  2. In 'Programming' tab, Traget Device: ATMEGA8, File Format to use: Intel Hex.
  3. Select the flash file (you can find the Hex file in your project directory, in my case it is E:\AVR\Studio\lab1\lab1\Debug\lab1.hex)
  4. Now click 'Program' (in flash section).
  5. When it's done, you'll see a success message in a separate window.

Now we'll connect any pin of PORT-D with any of the three ACTIVE-HIGH LEDs (Red or Green or Yellow) and connect any pin of PORT-C with the ACTIVE-LOW LED (Blue).

If you have done everything perfectly, you will see the result like the following image (the photo is underexposed so you can see the blue and red LEDs).

lab1.JPG