Monday, October 3, 2011

Chapter 6: Making your own USB Programmer

Fig: USB In System Programmer for AVR
Programming the microcontroller with parallel or serial port programmers seems to be a tedious task. Especially for those using laptops, there is no way out. These ports are extinct now a days. So, USB remains as the only option. I am going to demonstrate the process of building, configuring and installing a USB programmer. It is easy to build and the required components are widely available.

The Circuit
The original creation was by Thomas Fischl. To know more, you can visit his site.
I have done a few modifications of the original to make it simpler to implement.
Take a look at the following circuit,

Fig: USBASP Circuit


The important aspects of the circuit are,
  • There are two variants of ATmega8, one is ATmega8L and the other one is ATmega8-16PU. You must use ATmega8-16PU. Only this one supports 12MHz Crystals.
  • The ATmega8 used here is the Master (For more details, see Chapter 4). It does the following tasks,
    • Communicate with the PC via the Universal Serial Bus (USB)
    • Perform operations on any target (Slave) microcontroller using SPI (Serial Parallel Interface) as instructed from the PC. Operations mean to Read, Write or Erase Program memory (Flash) or Data Memory (EEPROM) etc.
  • The master device and everything in this circuit are powered from USB.
  • This ATmega8 must be programmed first. We need to write the USBASP firmware in its flash. For this task, you will need another programmer! It may be your old Parallel/Serial programmer or another USBASP programmer. If you don’t have one, do it from a friend. This is needed to be done only once.
  • The Red LED will be ON when it is connected to the PC, the green led flashes during programming activities.
  • X1 is a crystal oscillator. You must connect a 12 MHz crystal with two 22p capacitors as shown in the figure. You must connect it even when you program the ATmega8 in a separate board.
  • J3 is the SIL-6 connector; it is the SPI interface for programming. The pins are described below,
    • Pin 1: GND
    • Pin 2: Unconnected 
    • Pin 3: SCK
    • Pin 4: MISO
    • Pin 5: MOSI
    • Pin 6: RESET
  • J1 is the USB connector; I have used Type B connector in the PCB.
  • JP3 is a jumper named Slow SCK. USBASP has two programming modes, Fast and Slow. If this jumper is connected (Short Circuited), the programmer works in the slow mode. If it is left open, it works in the fast mode.
  • When a microcontroller runs at a very low clock frequency such as 1 MHz or so, it will not be able to communicate if the programmer is on the fast mode. In that case, select the slow mode by shorting the jumper. However, the programming speed can be adjusted in AVRDUDE too. Normally we will keep this jumper short circuited to avoid any trouble.

Building and Configuring USBASP
That was enough discussion of the circuit. Use that section as your reference if you face any trouble. This section will tell you how do it step by step. So put your seat belts on!
  • Download the PCB layout. I have given the top silk and bottom copper layer in PDF format. You can directly print them. Or you can make your own PCB layout from the circuit diagram.
    • Bottom Copper Layer: It contains the tracks that connect different parts of the circuit. Click here to download.
    • Top Silk Layer: It is the top of the PCB describing the part names, numbers and orientations. Click here to download.
  • Print out these two. The bottom copper is already mirrored so you don’t need to mirror it anymore. You should provide these two to the PCB makers according to their requirements. In Dhaka, you should print it on a tracing paper with dark black color and good quality. Lasers or good Inkjet printers will do. Remember, there should be “No Scaling”. Never use fit to paper size or anything else. Select the Page Scaling to “None” in Adobe Reader or Scaling Type “None “in Foxit Reader. Print these on two separate A4 tracing papers.
  • Provide the both printouts to the PCB developers and get your PCB.
Fig: PCB Top Side
Fig: PCB Bottom Side (Copper Layer)
  • Collect all the parts listed in the Part list.
  • Solder them on your PCB. Do not to solder the ATmega8 directly on the board, use bases instead.
  • Now program the ATmega8. You need a working programmer to do so. Whatever programmer you use is up to you; just do the following two things,
    • First, connect a 12MHz Crystal Oscillator with the two 22pF capacitors with ATmega8 on a separate breadboard or anywhere you program it.
    • Now, set the fuse bits for High Frequency (CKSEL) and high start up time (SUT). This is a crucial step. Wrong fuse settings will make the device not detectable by the programmer or even lock it! I give you the fuse setting in three different ways,
Fuse bits
SUT1
SUT0
CKSEL3
CKSEL2
CKSEL1
CKSEL0
Pony Prog
Unchecked
Checked
Unchecked
Unchecked
Unchecked
Unchecked
     Value (AVRDUDE)
1
0
1
1
1
1
o   In AVRs, to program a fuse bit you have to write a “0” and to un-program it you have to write a “1” to that corresponding bit. If you write using Pony Prog you have to check the bit you want to program (0) and uncheck the bit you want to un-program (1). If you write using AVRDUDE, you can directly program the fuse byte using the following commands,
  •      -U lfuse:w:0xef:m
  •      -U hfuse:w:0xc9:m
o   Make sure that you have the 12MHz crystal connected, without it the AVR will not work as the fuses are configured to use the external crystal.
o   Now program the microcontroller using the firmware hex file. Click here to download it.
  • After successful programming, install the microcontroller on its base of the PCB. Your new USB programmer is now ready.
Fig: The completed programmer with the ISP Cable Connected.



The Modifications from the original circuit  
  • There was an option to supply 5V power from the USB to the target microcontroller using the six wire ISP cable. I have utilized 5 lines in the ISP cable and left one unconnected. I did not connect VCC here. The USBASP lacks short circuit protection so I do not want to risk my PC. You need to use a separate power supply for the target.
  • There was a self programming option. It was for programming the master itself using the cable the master use to program a slave.  We need to program the master only once. So, I disabled it for simplicity. If you want to update or reprogram the firmware, just take out the ATmega8 from the board and program it in a separate place. This job always requires another programmer whether self programming is enabled or not.



Parts List

Item No.
Item
Amount
Comment
1
ATmega8 - 16PU
1
Don’t use 8 PU variants
2
28 pin base
1
You can also combine two 14 pin bases
3
Crystal Oscillator- 12MHz
1

4
Capacitor- around 22pF
2

5
Capacitor- 100nF
1

6
Capacitor- 4.7uF
1

7
Zener Diode- 3V6
2

8
Resistor- 68Ω
2

9
Resistor- 2.2kΩ
1

10
Resistor- 10kΩ
1

11
Resistor- 1kΩ
2
For the LEDs
12
LED- Green
1

13
LED - Red
1

14
6 pin Single in Line (SIL) Socket / Base
1
ISP Header
15
6 wire cable with SIL connectors on both sides
1
ISP Cable
16
Jumper/Dip Switch/Switch
1
To control the Slow SCK jumper. You can do this in any way you like.
17
USB Type B Connector
1

18
USB Cable
1
One end Type B and the other end Type A. This type of cable is used for printers.



Installation 
You should double check the soldering and the connections before connecting the programmer to the PC. Any mistake might damage the microcontroller or even your PC. Look for any short circuits in the VCC and GND lines very carefully. If found correct it immediately.

You will need libusb. Download it from here. Extract The zip file.

Connect the usbasp to your PC using the USB cable. The Found New Hardware message should appear on the system tray.
The Found New Hardware wizard will start automatically. Click  Cancel here and close the window. For now, you will see this message.
Don’t worry. Just go to the “libusb-win32-bin-x.x.x.x” folder you just extracted. Here ‘x’ will be replaced by the version numbers.
Go to the “libusb-win32-bin-x.x.x.x\bin” directory.
Run the INF Wizard. (Dbl Click and open inf-wizard.exe). The following window should appear.
Click Next. In the Device Selection window, you will see a list of detected devices. Select usbasp from the list. 
 Click Next. The Device Configuration Window should look like this.
You do not need to do anything here. Just click Next again.

A new dialogue box will appear which will ask you where you want to save the created driver files.
Just create a new folder and save the driver in it. This will save your newly created driver files for the usbasp programmer in that folder. Keep the folder as a backup. You can install your usbasp using the driver you have just created.
For now we will install the driver directly from the INF wizard in the next step.

After saving the driver files, you will see the following window,
We have just created the driver files for our device but we have not installed it yet. Just click Install Now.. button to install the driver directly.

After completion, you will see this message.

To check whether it is installed correctly or not, go to the device manager (by right clicking My Computer->Properties->Hardware Tab->Device Manager. You will see the device in libusb-win32 devices category.

Now you can start using it.

Note: You have saved the .inf driver file in a folder. Next time if you need to install it you won’t need to run the inf wizard. You can install your programmer just like you install other devices. In the found new hardware wizard, you have to locate and point out the .inf file. The computer will install it itself.


In the next chapter, I shall demonstrate how to program with AVRDUDE and AVR8 Burn-O-Mat using our newly created usbasp programmer.

I express my gratitude to Golam Kibria for his contribution and help regarding the PCB.

21 comments:

  1. odvut shundor.....you r not a big boss, you r the big boss....

    ReplyDelete
  2. vaia eta tow banailam but target board kivabe connect korbo? bolben ki// ami connect korar por msg ase// "could not power on target IC"
    how to solve this?

    ReplyDelete
  3. To Anonymous,
    Tumi ki usbasp properly install korso? Then, ei blog er Chapter 7 e deya ase AVRDUDE e kivabe sobkisu test korte hoi.

    How to connect to the target:
    1. Target AVR microcontroller er sathe usbasp er je ISP header (J3) er 5 ta line properly connect korte hobe. RESET, MOSI, MISO, SCK ebong GND must be connected to the respective pins.

    2.Target Microcontroller e ekta 5V regulated supply diye power up korte hobe. Remember, target er GND ebong usbasp er GND must be same.

    Connection sob thik thakle, usbasp ebong avrdude properly installed thakle ebong microcontroller er power supply thikmoto deya hole problem hobar kotha na.

    Finally, thank you for asking and let me know if it works!

    ReplyDelete
  4. sune chi na ki usb asp atmega 16 er sathe compatible noi ,ate ki tumi test kare cho,dada,zodi karo ni akk bar kore dekhibe aeita requeat rohilo,

    ReplyDelete
  5. arr akti kathaamar kache atmeg8L ache 12mhz crystal diyea habe ki karon aeikhane sudhu 2 te mcu pawa jai tat is atmega8L ar akta holo atmega16A

    ar aer chara kicchu pawa jai na , plz ]]


    aeita amay janabe ,,

    amar akta o usb programmer nai ar amma ke serial port program karwar janno relativer er bari jeta hai bar bar,

    plz help karbe,

    ReplyDelete
  6. To Ashish,


    Currently I don't have any ATmega16. But I believe that it will work with usbasp. AVRDUDE supports this model and it has the similar ISP interface like the other AVRs. So it should work. If I get an Atmega16 someday and find out that it does not work, I will let you know.


    Secondly, ATmega8L does not operate over 8 MHz, so it will not work. Atmega8-16PU model is compatible for this frequency as it can operate up to 16MHz clock.

    I think you are from Dhaka. If so, keep looking in Patuatuli and Stadium Market(Including the the market around the swimming pool). I buy most of them from Patuatuly. Both the models of ATmega8 are normally available. It may be hard to find. ATmega88 can also be used but its not available here.

    The firmware was written for Atmega8 and Atmega88 only. If you want to use it with other micro controllers, you will need to modify the source code yourself which may be a tedious job for a beginner. If you can do it, you can use it with any model that operates at 12 MHz or more. The firmware and its original codes are available at.
    http://www.fischl.de/usbasp/

    Finally, thanks for asking.

    ReplyDelete
  7. tai bhuji tale to amar wast gelo mcu ta ar aikhane kichu pawa o jai na,tar mane ami ki ar kori ,arduino bootloader install kara jabe ki ,in atmega8L ,ACTUALI AMI PROTHOM BAR KAJ KARCHI tai asubide hatche ,and plz say that fuse settings bhul hole ki kono prob hai ,karon kal ratri bela i was messing with the fuse an sudednly my avr stoppped responding ,ami serial programmar baniyea use karchilam ,ar normaly fuse settings ki rakhte hai ,

    and pllzzz im on fb tomar jadi id hai amay request pathiyea diyo dada , amar id holo


    ashloverscn@gmail.com

    link

    http://facebook.com/ashloverscn

    ReplyDelete
  8. jadi arduino banano jai with atmega 8L or atmega 16 a pu tale o habe amar kaj ta ,

    ar parle ama ke bootloader ponyprog diyea ki kare burn karte hai bhujiyea diyo ami pony prog te hex file open kare flash memory te burn kari ar external crystal occilator 8 to 16 mhz settings diyea wri8 kari tao dekhi hai na ,tuimi akbar dekhbe i hope so arduino baniyea cho but ami boot loader ta externaly burn karte chai with pony prog using dasa or si prog programmer

    tumi to bese bhalo video upload o karo amr janno akta vudeo toutorial baniyea diyo parle that how to burn arduino boot loader in atmega 8 with fuse settings and use with rs232 module ,

    ReplyDelete
  9. To Ashish,

    I have never worked with arduino or bootloaders. Currently I can't help you as I am passing some busy weeks of my semester. If I get free time later, I will surely try to help you out.

    There are some crucial fuses that enables or disables ISP programming. So you should always be careful while dealing with fuses. Also, wrong oscillator settings might make the uC unusable. Sometimes, the AVRs get locked for unknown reasons while editing fuses with serial or parallel programmers. I faced it. But then I reset the fuses with a Universal Programmer and the AVR was OK again.

    You can always contact me in this comments section. You can also send me emails. My email address is given in my blogger profile.


    Good Luck..

    ReplyDelete
  10. and aei rakham hai ki je when im coding i have to mention my cpu clock speed nai to program mcu te run karbe na,

    ReplyDelete
  11. cuz i found that i have to compile my program emulating my mcu type in avr studio,

    if ig o mad now seriously i will start editing the bootlaoders of arduino for my mcu ,

    ReplyDelete
  12. i need to have a boot loader which will work with internal 8 mhz occilator ,
    i dont get any mcu above 8 mhz support i told it to u previously ,nale ami bose thaktan na amr programmer ta to toyri hae jeo ,ki karbo bolo to ,dhatttttttttttttttttttttttttt, :(

    ReplyDelete
  13. MY DEAR FIRENDS AND TANVIR DADA I HAVE SUCCESFULLLY SEEN USB ASP WORKING WITH ATMEGA8L PU

    DONT NOT WORRY ABOUT THE CLOCK SPEED IT WILL WORK FINE,IT DIDNT SHOW TO WORK BESAUSE U DIDNT CONFIGURE THE EXTERNAL CRYSTAL FUSE FOR THE MCU ,U NEED TO SET THE EXTERNAL CRYSTAL FUSE THEN ONLY IT WILL WORK THE REASON IS ATMEL COMPANY MAKE DEFAULT FUSE SETTINGS AS INTERNAL 8MHZ OCCILATOR FOR THE MCU,AND ALSO LET ME TEL U I HAVE SEEN AT MAVY PLACES PEOPLE TELLING THAT ATMEGA8L IS A LOW VOLTAGE MCU AND IT ONLY CLOCKS AT 8MHZ AND BLAH BLAH BLAH, BUT ITS NOT SO THE REASON UR CLOCKING AT 8MHZ IS THE DEFAULT FUSE SETTINGS WHICH IS MAKEING UR ATMEGA TO CLOCK THROUGH INTERNAL OCCILATOR THE REASON UR IC NOT DETECTING AS USB ASP IN DEVIDE MANNAGER AS A NEW HARDWARE ,SO DO NOT WORRY MAKE UR PROGRAMMER WITH ATMEGA8L ALSO ,JUST BE CARE FULL NOT TO CHOOSE A WRONG CLOCK SOURCE OR UR AVR WOULD GO DEADF TO ISP (AS U MAY DISABLE THE RESET PIN) OR UR AVR WOULD STOP WORKING THROUGH CRYSTAL OR INTERNAL OCCILATOR (AS U MAY CHOOSE A WRONG CLOCK SOURCE FOR EXAMPLE EXTERNAL CLOCK OCCILATOR WHICH WILL REPLACE SETTINGS FOR A EXTERNAL CRYSTAL AND UR AVR WILL START LOOKING FOR A CLOCK SOURCE ON XTAL 1 PIN ,SO NOW KNOWINGLY U CAN GUESS WHAT IS THE PROB UR AVR IS LOOKING FOR A CLOCK SOURCE BUT WHAT U HAVE IS A CRYSTAL ATTACHED SO NOTHING WILL WORK,NOW COMING TO THE MAIN TOPIC THAT IS WHAT IS THEN DIFFERENCE BETWEEN ATMEGA8 AND ATMEGA8L

    SO REMEMBER "ATMEGA8L" ONLY MEANS TO SAY U CAN ALSO WORK AT LOW VOLTAGE BELOW 1.5V IF UR WORKING AT 8MHZ SPEED ,I HEARD PEOPLE ALSO SAYING THAT DATA SHEET IS TELLING THAT ATMEGA8L CAN ONLY WORK AT 8MHZ,BUT ITS NOTHING LIKE THAT IT IS THE ABOVE REASONS WHY UR CHIP IS WORKING AT 8MHZ AND DATA SHEET ONLY MENT TO SAY IS THAT "U CAN ALSO WORK WITH ATMEGA8L BELOW 1.5V IF UR RUNNING UR PROGRAM AT 8MHZ",

    NOTE:ALL ATMEGA SERIES OF AVR CHIP CAN CLOCK UP TO 16MHZ DO NOT WORRY AND TRY (BUT REMEMBER NOT TO FORGET TO CHECH THE CORRECT FUSE BUT AT THE SAME TIME HANDLE FUSE WITH CATE CUT IT MAY DESTROY UR AVR CHIP PERMANNENTLY )

    ReplyDelete
  14. AND IF U HAVE MESSED THE WRONG CLOCK U MAY TRY TO USE A OLD WORKING AVR AS A CLOCK SOURCE ,ITS NOTHING HI FI U JUST NEED TO WRITE A PIN TOGGLE PROGRAM WITHOUT A DELAY (JUST SWITCHING THE PIN LOW AND HIGH AND BURN IT TO THE WORKING MICRO CONTROLLER,NOW CONNECTING THE PIN OF THE WORKING MCU WHICH U ARE TOGGELING WITH THE PROGRAM TO THE DEAD ATMEGA XTAL1 PIN (ALSO AT THE SAME TIME DO NOT CONNECT ANY THING TO XTAL PIN 2),NOW CONNECT GND AND VCC OF DEAD MCU WITH GND AND VCC OF WORKING MCU RESPECTIVELY NOW PROGRAM THE DEAD MCU ,IT SHOULD WORK IF UR CLOCK SOURCE IS MISS SOURCED WITH A EXTERNAL OCCILATOR ,AND IF NOT U R NEEDING A HI VOLTAGE PROGRAMMER U CNA MAKE IT IF UR USING ARDUINO ,

    ITS SIMPLE AS A PIECE OF A CAKE GO TO THE LINK

    http://mightyohm.com/blog/2008/09/arduino-based-avr-high-voltage-programmer/

    AND REMEMBER THE WORDS ATMEGA8L CAN ALSO BE USE IN USBASP PROGRAMMER ,

    ReplyDelete
  15. @ Ashish

    Thanks, that's a lot of information. Your ideas will be helpful for us.

    One thing regarding clock ratings,
    Actually the things you find in the datasheet are the rated values (Values that will be optimum). Operating over the maximum rated frequency is called overclocking and normally it works! However, the thing I would like you to know is that, it's risky. The AVR might fail unexpectedly anytime although It will do your job for now anyway.

    You should not overclock when reliability is a concern. Otherwise its completely fine. AVRs can be overclocked to even 20MHz.

    ReplyDelete
  16. YAAP !!! YAAP !!!
    JOIN ME AND VISIT MY USBASP PAGE ON FACEBOOK
    I HAVE UPLOADED PICTURES AND VIDEOS

    ReplyDelete
  17. https://www.facebook.com/ashloverscn

    THIS IS MY FACEBOOK LINK

    ReplyDelete
  18. the atmega8-16pu 22nd pin GND is left unconnected.. why.. can i connect to this ground..?/

    ReplyDelete

  19. ********************************************************
    ----BUY ELECTRONIC COMPONENTS AND GADGETS ONLINE AT-----

    ............ www.kimaginations.com .................

    ===============LOWEST PRICE IN INDIA ===================

    (Get Exciting Offers and Discounts)

    (ROBOTICS,EMBEDDED,VLSI KITS,MATERIALS AND PROJECTS)

    USE COUPON CODE : KI100DISC

    *GET Rs. 100 /- OFF ON PURCHASE


    ********************************************************
    *T&C Apply

    ReplyDelete