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.
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
To upload you firmware, first connect the USBASP Programmer to the USB port and breadboard circuit as shown in the photo below.
In the command prompt enter the following command then press enter
- make program
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.
great. thanks
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??
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
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?
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.
I love you XD
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?
Same here, I’m using a cheapo programmer from eBay.
ignore that I got it working
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.
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
Is the firmware getting written to the device?
not yet. i can’t even write the program to the device
Are you using a breadboard, a protostack AVR board or something else?
If you want, send me a message via our contact us page http://www.protostack.com/index.php?main_page=contact_us and I’ll help you through the issue.
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.
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?
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.
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)
@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.
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
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.
Same here…
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.
Can this be programmed in Atmel Studio 6?
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.