20 May 2013

How to Do ISP Programming – Part 1: The Theory


Introduction
Many of my projects use AVR controllers from Atmel. And they need to be "programmed". This means that the software needs to get written into their internal program memory.
 
For beginners, this can be a major stumbling block in any project. And I do get lots of questions in connection with programming. So I decided to write this tutorial. But my intention is not just to give a list of buttons you have to press. Instead I want you to understand what you are doing.
 
This tutorial assumes you have a Windows PC and use an USBasp programmer. They are available from your favourite auction website (very cheap, ready made, from China). Alternatively you can support their creator Thomas Fischl and buy his kit.

Overview
This tutorial consists of four parts:
The Theory (this post)
Hardware Considerations
Software Usage
Writing Fuses / Troubleshooting
   
What Memory?...
One of the differences between a microcontroller and a microprocessor is that the controller contains its own "non-volatile" memory. This is memory that doesn't lose its contents when the power supply is switched off. AVRs actually have three types of non-volatile memory:
"Program Memory" or "Flash Memory"
This is the type of memory, that you must write to in every project. It contains the executable code, which tells the controller what to do. Depending on the model of AVR, it can be written between 1,000 and 10,000 times. With no contents in this memory, the AVR is like a computer with an empty hard drive.
"Data Memory" or "EEPROM"
This is meant for data. It can not contain executable code for the AVR. You can keep any data in this type of memory. This can be constant or variable data. It can be written 100,000 times. Not all projects need an EEPROM. Some smaller AVRs haven't even got EEPROM.
Special Bits 
There are a few extra bits in AVRs, which control the general behaviour of the chip. See the respective data sheets for details.
  • Lock bits can control read/write access to the chip. They are meant to protect memory contents from being overwritten or read. They are a kind of copy protection. Lock bits can be erased only with the chip erase command, which also erases all other memories. Advice for beginners: Keep away, can cause unnecessary trouble.
  • Fuse bits are there to set some hardware-related properties of the controller. E.g. with fuse bits you can select your system clock source. Without the correct clock source selected, your chip wouldn't even start. So these bits couldn't be set by software. BE CAREFUL! You can disable ISP programming with fuses, which means that wrong fuse settings could leave you with an inaccessible device - at least if you have no access to a high voltage programmer.
  • Calibration bytes are there to fine tune the internal clock generator. I guess that this can be useful, but if you need an accurate clock just use an external crystal.
  • Signature bytes (read-only) contain a number that is unique to the type of AVR which you have selected. In a production environment this can be used to verify that the correct type of controller is used. It is also useful to test whether the programmer is working, because you can read that known data and check, whether or not you where able to get it right.
ISP versus High Voltage Programming
For most AVR controllers, there are at least two ways to program them. One is called "High Voltage Parallel Programming". For this mode you stick the controller in a special socket of your programmer. This gets you more or less direct access to the memory, but means you need a lot of pins. The mode is activated by (amongst other things) applying 12 volts to the AVR's reset pin. Also, you have to supply the system clock through the programmer.
With small ATtiny controllers, you simply don't have enough pins for parallel programming, so instead something called "High Voltage Serial Programming" is used. Again, this mode is activated by setting the reset pin to 12 volts. But the data in this case has to be sent in a serial format.
Both "high voltage" modes have one disadvantage: They can usually not be used while the controller is in the target system.

There is a programming mode called ISP (In-System Programming). This mode needs no high voltage. So you can leave the microcontroller in the system, while you program it. It is a serial mode, so all you need are the pins MISO / MOSI / SCK for the serial interface. Plus, you need RESET to switch to this mode. The pins can be shared between normal function and the programming interface. Just make sure that the pins are not driven by external periphery during programming.

 
Of course, even if this mode can be used while the controller is in the system you can build a little programming board. And I would recommend to do this, if you start out with microcontrollers. It means that you have no kind of interference by any peripheral hardware. So this is just the basic advice of keeping it simple. Here is an example of a small ATTiny13 board (I added an LED on the unused pins for some experiments):


In part 2 we are going to look at the hardware side of things. 

5 comments:

  1. Thanks Tom!

    Can you say a bit more about the advantages of high voltage programming?

    Paul

    ReplyDelete
    Replies
    1. Sorry Paul,
      I am not at home right now. Main advantage is full access, so all fuses can be set / reset. HV programming uses its own clock, so a wrongly selected clock source is no problem.

      Delete
  2. Dear sir,
    Sir i have received almost all the items before expected......ur service is incredible . Today i tried to program my ATmega8L , by using the usbasp given by you.......I placed the chip at the required position on the green colour acceptor at the specific at required position (starting from 4th pin) , after some difficulty i installed the drivers on my pc and the usbasp has started working on my pc and my pc has recognised it , i am using AVrdude to program the chip, but my problem is--- it is giving the error that "Target doesnot answer 1 ; check ur connection" .............i have done lots of things to get over this problem but i failed......Do u have any idea about this problem.......Please let me know any thing that might help me sir......
    Regards,
    Rushikesh

    ReplyDelete
    Replies
    1. Sorry my friend, but you must be mixing things up. I am not selling anything and I have no idea where you got your programmer.

      I don't even understand what you mean by the green colour acceptor.

      Are you sure you are on the correct web site?

      Delete
  3. Tom sir i am really sorry for this post....actually i was posting it to my seller.....and by mistake i posted here........sorry again

    ReplyDelete