Skip to main content

ATmega8 breadboard circuit – Part 3 of 3 – The Firmware

This tutorial continues on from ATmega8 breadboard circuit Part 1 and ATmega8 breadboard circuit Part 2. So far we’ve built a power supply, added the microcontroller, added some plumbing to make it work and added the ISP interface, but it really doesn’t do anything. The next step is to add some I/O devices and upload some firmware.

A lot of ATmega8 tutorials will use a “Hello World” program which consists of an LED that blinks at 1Hz. For this tutorial we will build on this and have an LED that blinks 3 times when a button is pressed. The first thing we need to do is add the LED and button to the breadboard.

The images below shows the schematic and the components on the breadboard.

atmega8 switch and LED
atmega8 on breadboard with switch and LED

The PC5 pin will be “pulled up” and when the button is pressed, this will ground the pin and initiate the blinking of the LED. The LED is connected to PC4 via a 150 ohm resistor. I calculated the resistor size using the LED Resistor Calculator on ohmslawcalculator.com.

To write the firmware, we will be using WinAVR. This is a collection of tools that includes AVR-GCC compiler, AVR-LibC, AVRdude and more.

WinAVR has a utility called “mfile” which makes it easier to create a makefile. From the “Makefile” menu, select the following options then save the file in a folder for this project.

  • MCU Type: ATmega8
  • Port: usb

Next we want to edit the makefile and change the following entries

AVRDUDE_PROGRAMMER = usbasp
F_CPU = 1000000

We need to modify the AVRDUDE_PROGRAMMER entry because the mfile utility does not have “usbasp” in the programmers list. This of course assumes that you are using a USBASP Programmer. If you are using another programmer, you will need to modify this section of the makefile with values that are suitable for that programmer.

The F_CPU entry specifies the microcontroller clock speed and is used by the _delay_ms function. The ATmega8 out of the box runs on a 1MHz internal clock, but this can be changed if required.

Using programmers notepad (Another tool bundled with WinAVR), create main.c with the following content.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <avr/io.h>
#include <util/delay.h>
 
 
//Define functions
//======================
void ioinit(void);
void led_on(void);
void led_off(void);
//======================
 
int main (void)
{
    ioinit(); //Setup IO pins and defaults
 
   while (1)
   {
      if (bit_is_clear(PINC, 5))
      {
         for (int i=0;i<3;i++)
         {
            if (i>0)
               _delay_ms(500);
            led_on();
            _delay_ms(500);
            led_off();
         }
      }
   }
}
 
 
void ioinit (void)
{
   DDRC  = 0b11011111; //1 = output, 0 = input
   PORTC = 0b00100000; //Enable pin 5 internal pullup
}
 
void led_on(void)
{
   PORTC |= _BV(PC4);
}
 
void led_off(void)
{
   PORTC &= ~_BV(PC4);
}

To compile the firmware

  • Open a command prompt
  • Make sure you are in the folder containing the makefile and main.c
  • type “make” and press enter

compiling the firmware

To upload you firmware, first connect the USBASP Programmer to the USB port and breadboard circuit as shown in the photo below.

Connecting a USBasp programmer

In the command prompt enter the following command then press enter

  • make program

writing the compiled firmware to the atmega8 microcontroller

The system should now be ready for testing. Connect it up to your power source, press the button and watch the LED blink 3 times.

Related News

AVR Memory Architecture

AVR Memory Architecture

The AVR family of microcontrollers use a modified Harvard Architecture which uses 3 types of...

ATmega168 Pulse Width Modulation - PWM

ATmega168A Pulse Width Modulation – PWM

Dimming an incandescent bulb is easy. Simply adjust the current down using a potentiometer and...

Severed hand in a jar Halloween display

For this post I thought I'd try something a little different. I've created 3 videos...

25 Comments

  1. Karl

    Thanks for these tutorials. I found them very clear and easy to follow. Hope you continue to create more! Love to see a tutorial on C programming for embedded devices (AVR based). Especially the setting up of Ports etc. Coming from Arduino land, this is a bit of a black art in my opinion??

  2. R Bolijn

    Hi all,

    Great tutorial. I have a problem though:

    The .hex file seems to have loaded to the mcu just fine. I get the same responses from the cmd window as in this tutorial, but there’s one extra line:
    warning: cannot set sck period. please check for usbasp firmware update (after the \erasing chip\ line).
    Does aneone know what to do? The led is flashing.

    Ronald

  3. Flywheel88

    Are you sure the code is okay? When I compile it I get an error on the line 47, complaining about the ‘&=’ part.
    Perhaps I need a path to the libararies?

  4. Flywheel88

    I have found the solution to the problem I found with the code.
    If you copy/paste the code from the web page, line 47 reads as:
    47. PORTC & a m p ; = ~_BV(PC4);
    (I added extra spaces because somehow this ‘amp;’ part appears and disappears)
    The line should be:
    47. PORTC &= ~_BV(PC4);
    This works fine! The led flashes now.
    Thanks for the clear instructions.

    1. Fernando Denis Emert

      I love you XD

  5. NiveusLuxLucis

    I keep getting this:

    avrdude: warning: cannot set sck period. please check for usbasp firmware update
    .
    avrdude: error: programm enable: target doesn’t answer. 1
    avrdude: initialization failed, rc=-1
    Double check connections and try again, or use -F to override
    this check.

    When I’m using a straight off the shelf ATMEGA48A-PU. The user guide says something about an SCK option, but it doesn’t say how to set it. Can anyone offer a solution?

    1. Rival

      Same here, I’m using a cheapo programmer from eBay.

  6. NiveusLuxLucis

    ignore that I got it working

    1. Peertux

      I just love it when people find a solution for their problem and say “SOLVED” or something like that, without posting the solution for others.

  7. ibnu tohid

    hi all..

    i keep on getting this error.. can anyone show me how to correct this.

    avrdude: warning: cannot set sck period. please check for usbasp firmware update

    avrdude: error: programm enable: target doesn’t answer. 1
    avrdude: initialization failed, rc=-1
    Double check connections and try again, or use -F to override
    this check.

    i already check the connection several times and i dont find any problem with it. really appreciate your help. thanks in advance

    1. Daniel Garcia Author

      Is the firmware getting written to the device?

      1. ibnu tohid

        not yet. i can’t even write the program to the device

    2. Mike

      I had the same issue and it was caused from the USBasp not providing power to the breadboard. All i have to do was to close a jumper on the programmer to supply 5V to the board.

  8. John R. Hartmus II

    I see that the USBASP programmer supplies +5 VDC on pin 2, and in the picture which shows your breadboard connected to your laptop via your USBASP programmer, you are obviously using the programmer as your power source. Would I be safe to not connect pin 2 from the programmer, and use another 5 VDC power supply for the MCU, as long as the grounds are connected?

    1. Daniel Garcia Author

      There is a jumper on the USBASP that controls the power output on pin 2. On the 2 latest versions of the programmer that jumper is labeled as J1 or JP1. When set, this jumper allows power to go to pin 2. Unset this jumper if you want to have a different power source to the MCU.

  9. Thoquz

    I made this on my breadboard and it gave me this error:
    “avrdude: initialization failed, rc=-1”

    I fixed this by building a standalone target board that uses the 6 pin icsp connector rather than the 10 pin one used here.

    As mentioned in the comments the code in the above tutorial has a syntax error.
    Here is a link to my version of that code that works. (http://pastebin.com/jDm2maeM)

  10. wowme@wtf.com

    @Thoquz
    I had the ‘“avrdude: initialization failed, rc=-1″’ error on another project using a usbtiny programmer.

    My fix was to change the avrdude command line from -B 1 to -B 32.
    Some smaller value than 32 Might Have fixed it, but the first fix was used.

  11. Mohan

    After make programe commond screen dispys
    No rule to make target
    In note pad [WinAVR} Programe I got following massage

    > “make.exe” program
    avrdude -p atmega8 -P usb -c usbasp -U flash:w:main.hex
    avrdude: error: could not find USB device “USBasp” with vid=0x16c0 pid=0x5dc
    make.exe: *** [program] Error 1

    > Process Exit Code: 2
    > Time Taken: 00:00
    Pl reply how to execute programme

  12. shekar

    I keep getting this:

    avrdude: warning: cannot set sck period. please check for usbasp firmware update
    .
    avrdude: error: programm enable: target doesn’t answer. 1
    avrdude: initialization failed, rc=-1
    Double check connections and try again, or use -F to override
    this check.

      1. Rival

        I realized later that I had the wiring wrong on my programmer and the way it was connected to the chip.
        I gave up on using avrdude anyways and now use Khazama AVR Programmer or eXtreme Burner, had less issues using it. It’s graphical as well and easier to use.

  13. Daithi

    Can this be programmed in Atmel Studio 6?

  14. Ivan Fomin

    Really cool basic tutorial, something that I search for a long time. And it really works! Even with my very old circuit that I bought 3 years ago.

Leave a reply

Shopping Cart