[TUT] [SOFT] Led blinking Attiny45,USBasp,AVR studio AVRdude

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In this tutorial we will do:
- Install AVR studio 6
- install AVRdude
- prepare your USBasp programmer
- connect USBasp to attiny45
- Create and add our blinking LED circuit.
- Reboot your Windows computer.
- Write program and create hex file
- Write hex file into microcontroller
- see our LED light blink

Requirements before you try this tutorial:
- USBasp programmer (clone like:
http://www.miniinthebox.com/nl/u...
3-3v-5v-avr-programmer-usb-atmega8-atmega128_p391023.html
- download the USBasp driver from
http://www.fischl.de/usbasp/
- 10 female to male thin cables like http://www.miniinthebox.com/nl/m...
- Breadboard like http://www.miniinthebox.com/nl/b...
- Breadboard cables male to males or use thin
electrical wire to connect the various components
on the breadboard.
- One piece LED any color
- One resistor 1000 ohm

After finishing this tutor you will know many basic steps to program your microcontroller.
This tutorial is not complete yet, for instance why use AVR studio 6 to create hex file and use AVRdude to program it. There might be faster ways, but this is the I found it working.
There is always time to improve the way we work or program. But this short tutorial is not mend for in depth knowledge. It's to give you a quick start and create your first AVR microcontroller
programming project successfully!

- Install AVR studio 6
Download and install AVR studio 6 from: http://www.atmel.com/tools/atmel...
Just default installation will do.

- install AVRdude
Download AVRdude (winAVR) latest version from: http://winavr.sourceforge.net/
Default installation will do.

- prepare your USBasp programmer

I bought an USBasp clone which was original from this creator: http://www.fischl.de/usbasp/

Now we install our USBasp:
"¢ Download the lastest firmware package. Unzip it somewhere you like. I placed it in:
F:\drivers\Usbasp\usbasp.2011-05-28\bin\win-driver\libusb_1.2.4.0
Go to device manager and disable any devices that have a question mark. How to: Start --> control panel --> device manager,
Or read http://technet.microsoft.com/en-... If you haven't noticed that something is not working on your
PC / laptop, then I guess you won't mind to disable that device with the question mark anyway. This step will make it more easy for you
to identify your USBasp. Because it will not be detected by Windows 7. Some people might have several yellow question marks
"¢ Insert your USBasp into any free USB port.
"¢ Windows will not find any device driver for it. You have to manually tell Windows where the driver is found.
The driver is found in: F:\drivers\Usbasp\usbasp.2011-05-28\bin\win-driver\libusb_1.2.4.0
Of course change it to your drive and folder structure.
"¢ If you did insert the USBasp and did not install the driver, you chancel the screen to install the driver, then you can install the driver again by following steps:
Start --> control panel --> device manager, right click on that device ,it has a yellow question mark, which you know is the USBasp--> update driver software
The driver is found in the directory where you unzipped the file.
"¢ Windows 7 will protest that the driver is not signed, just continue, windows 7 will then continue the installation.
"¢ Reboot windows 7 to make sure.
"¢ If you did well the USBasp is identified under lisusb-win32 devices, as the device 'USBasp'.

- connect USBasp to attiny45

Now we have to attach a few cables to your USBasp, so that you can use these loose cables onto your breadbord. I did not use the flat cable and with its 10 pin connector, because it was too long.

I photographed both sides of the USBasp. You can see which connector pin, has what function. The 10 pin flatcable connector has a triangle to indicate
pin one. I just inserted 10 male to female breadboard cables into the 10 pin connector. how the colors are connected to your USBasp is not important.
As long you know what is what. That;s why I used tape onto each cable and wrote onto them: MOSI, reset, SCK, MISO,VCC,GND

The flat cable 10 pin layout is as follows on the USBasp:
Please notice that this pin layout / usage does not reflect the way the cables is connected to the micro-controller !!

pin number usage
---------- ---------
1 MOSI
2 not used
3 reset
4 SCK
5 MISO
6 VCC
7,8,9,10 GND

The Attiny45 is an IC with legs, each leg has a number. Number one has a small round dot in the corner. The dot indicates that the leg in that corner is number 1. All the legs are numbered as follows:

1(dot) 8
2 7
3 6
4 5

Connect the cables from your USBasp to yout attiny45 as follows:
( picture with Attiny45 http://hlt.media.mit.edu/?p=1695 )

USBasp cable function Attiny45 pin number
--------------------- -------------------
RESET 1
GND (any GND is ok) 4
MOSI 5
MISO 6
SCK 7
VCC 8

Now we have our programming circuit ready. This is pure for programming the microcontroller.

- Create and add our blinking LED circuit.
The following diagram is not like any default diagram. I try to use TXT symbols to substitute for all electrical symbols:

-------- This is electrical cable

>>} An LED light, } is minus polarity, >> is
the plus polarity. The way I remember what
is plus en minus. is to look at the LED
itself. I tell myself LED big plate is
less, the minus. And the smaller plate is
more, thus Plus. The opposite.

[===] An resistor

(1) , (2) these are the ends of out circuit, these
must be connected to something else

The schematic for the blinking LED:
(1) ------- >>} ------- [===] ------- (2)

Connect (1) to microcontroller pin 4, this is GND.
Connect (2) to microcontroller pin 5

- Write program and create hex file
info about AVRdude: http://www.ladyada.net/learn/avr...

Create an new project in AVR studio 6:
- File --> New --> New project -->
- Choose GCC C executable Project
- Give your project an name and solution name.
- I just used the names GccApplication2, you can choose any other name you like.
- Leave other settings as they are , don't change them. Click ok
- Choose the chip you are going to program: I have a Attiny45 so I choose
in the 'Device Family' the option 'tinyAVR 8 bit' to limit the results
I scroll down to Attiny45.
The nice part is that AVR studio give you info about the chip (Datasheets) and which Atmel support it.
- After selecting your chip, press ok
- An default C program layout will appear. Please clear the whole screen of the following code:

/*
 * GccApplication3.c
 *
 * Created: 5-3-2013 14:21:53
 *  Author: chihwahli
 */ 


#include 

int main(void)
{
    while(1)
    {
        //TODO:: Please write your application code 
    }
}

After removing above code, please paste the following code into the same screen:


//cpu main clock frequency, needed for delay library
//check datasheet for further information
#define F_CPU 1200000 //9.6Mhz/8 = 1.2Mhz

#include 
#include 

int main(){
	DDRB = 0x01; //PB0 is set to output

	while(1){  //Infinite loop
	PORTB ^= 0x01; //Change PB0 bit, 0 to 1, 1 to 0
	_delay_ms(1000); //One second delay
}

return 0;
}

Now press F7, this will compile the C code into a HEX file, that we will use to program our microcontroller.

Locate your HEX file:
Look in AVR studio, in the part called 'Solution Explorer', open 'output files', then click onto the file
that ends with HEX. Below you will see 'Filepath' Place your mouse over it and see the popup text
and it will tell you where the HEX file is located. Write down if you need, we will need it.
I myself copied the file to a folder that is right under C:\windows. That makes it easier to work with
If you like, you can copy the HEX file to C:\tmp . As long if you don't have many sub folders....

Press The windows start button, and type CMD into the search part, press enter. CMD will then open an black command prompt.
My file is located in c:\windows , so I type: cd c:\windows followed by the ENTER keyboard button.
If you see c:\windows> with a blinking cursor. then it's good.

- Write hex file into microcontroller

In the previous step we copied our HEX file to a folder. Now we program our program into the microcontroller.
First learn what is what in AVRdude: avrdude -c usbasp -p t45 -u -U flash:w:app.hex

avrdude is the programming tool.
- c usbasp is programming tool we have and want to use.
-p t45 is the chip we want to program. t45 means Attiny45.
-U flash:w:app.hex means we want to flash with the HEX file named app.hex

Unless you have the same file name and USB programmer. You will need to change it. We will continue and program the chip.
I type into the command prompt: AVRdude: avrdude -c usbasp -p t45 -u -U flash:w:app.hex

You will see screens like: http://www.ladyada.net/learn/avr...

And our LED starts blinking! Hope you enjoyed this short but with long explanation. Didn't want to make it too short and cryptic.

Attachment(s): 

Student computer science
Location: the Netherlands
Likes: Lion of Judah

"...As my first Atiny45 blinks its LED, we all get ideas, will you let it shine or pull the plug....?"

Last Edited: Tue. Mar 5, 2013 - 02:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please also notice that USBASP gives an error about
not detecting SCK, etc. t does not matter. The programming works.

Student computer science
Location: the Netherlands
Likes: Lion of Judah

"...As my first Atiny45 blinks its LED, we all get ideas, will you let it shine or pull the plug....?"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you so much for this, this is what a beginner really needs :)! Keep up the good work!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

chihwahli wrote:

//cpu main clock frequency, needed for delay library
//check datasheet for further information
#define F_CPU 1200000 //9.6Mhz/8 = 1.2Mhz


Where did the 9.6Mhz come from?
According to the datasheet:
Quote:
6.2.7 Default Clock Source
The device is shipped with CKSEL = “0010”, SUT = “10”, and CKDIV8 programmed. The default clock source setting
is therefore the Internal RC Oscillator running at 8 MHz with longest start-up time and an initial system clock
prescaling of 8, resulting in 1.0 MHz system clock.

Shouldn't it be:

#define F_CPU 1000000 //8.0Mhz/8 = 1.0Mhz