www.angelfire.com/dragon/letstry
cwave04 at yahoo dot com

Programming a microcontroller serially

This tutorial is about programming a microcontroller. It is a rather nerdy tutorial, and is going to bombard you with technicalities. But before going into those nitty gritty details I would like to give you an informal overview about what I am going to write about and why.

Overview

A microcontroller is a single-chip computer. Like any other computer it needs to be programmed before it can do things. The programming is done via a circuit that connects your PC with a microcontroller. Think of it as a little circuit board connected by wires with your PC (to its parallel port, say) as shown in the following picture. There is a place in the circuit where you can insert the microcontroller to be programmed.

Next you have to have a program that you want to download to the microcontroller. This may be written in C or assembly or some other language. The program needs to be compiled into the machine language of the microcontroller. There are many (free) compilers that do this for you, and produce the binary output in a simple format called the (Intel) HEX format.

Now there is another piece of software that you need. This is for communicating with the circuit board that we just connected to the parallel port of your PC. This software loads the binary output from the compiler, and somehow pokes the bytes down into the microcontroller via the circuit.

The circuit (including the connecting cable) and its software driver are together called a "programmer". (So here the term does NOT refer to the sleep-deprived, sore-fingered homo sapiens that sit glued in front of the computers!) This tutorial will try to teach you how to make a programmer. This is not a tutorial that talks about microcontroller architecture or assembly language.

What is special about this tutorial?

The internet is full of free tutorials, softwares and circuits to make programmers. You may even call this one "yet another" tutorial on this subject. But I have tried to make this tutorial a bit different from the rest. And you might like to know how before deciding to waste your time with these pages.

All the tutorials that I have seen on this subject give you ready made circuits and programs (with or without source codes) and some guidance about assembling the circuit or running the program. Few, alas, tell you how YOU can DESIGN your own programmer. Worse still, they are all silent about what to do if their programmers do not work (and believe me, it is IMMENSELY difficult to get a programmer to work in the first few attempts!) Indeed, it took me an year between seeing the first flicker of success and being confident about writing a programmer. Most of the failed attempts were because of bad working habits or misconceptions on my part, that could be eliminated only after months of experimentation and frantic emails to people around the globe. (I would particularly like to thank Steve and Wichit Sirichote for their patience and readiness to help.)

Now that I can finally make a programmer confidently, it seems that just a basic few basic guidelines can save hours of frustration for another newcomer in this field.

And that is the aim of this tutorial. Like the other tutorials on the web, this one will also give you a circuit and a ready software. But the main aim will remain to teach you how to explore and understand the details.

What you'll need

  1. This tutorials will teach you making programmers for AT89S51 or AT89S52 (same progrmmer works for both). Very similar technique also works for ATMEGA8515. Whichever microcontroller you plan to work with, you must have about 4 different pieces of it. In principle one should suffice, but every now and then you'll make a wrong connection and then start worrying whether the microcontroller is burnt. Checking with a fresh microcontroller will be helpful at such moments. By the way, AT89S52/51 is a lot cheaper than ATMEGA8515. The latter costs around Rs 90 while the former is available at Rs 35 in Kolkata (July, 2008).
  2. You'll need good soldering equipments, and a perf board or vero borad. Never dream of using a breadboard. They are extremely unreliable at high frequencies.
  3. The datasheet for the microcontroller. This is freely available on the web. Here is a local copy. Some websites have only a short version. You'll need the long version. Just make sure that the datasheet has a section on serial programming.
  4. Access to a good multimeter is a must. If you have a logic probe that will be nice too. A digital oscilloscope with one-shot feature would be heavenly. But ordinary mortals like myself usually do not have one of these costly gizmos. I could manage to make the programmer without ever using such an oscilloscope.
  5. An 8MHz crystal (anything from 3MHz to 33MHz should suffice according to the datasheet). I have worked equally successfully with both 8MHz as well as 11.0592MHz crystals. You'll also need 2 capacitors (33pF each). A few 1K resistors will be needed for experimenting. You'll need a DB-25 male connector that fits into the parallel port of your PC. And yes, you do need a PC with a parallel port!
  6. You need to have a C complier. I used Borland's free commandline C compiler on Windows, and gcc on Knoppix (a variant of Linux) and Fedora 8. A good grasp of bit level programming (bitwise and, or, shifts) will be helpful.
  7. Last but by no means the least, you'll need lots of free time and LOTS of patience. Making a programmer is not very difficult in principle but there are only too many chances of making mistakes.

Getting started

There are three ways to program a microcontroller:
  1. Parallel prgramming: this is the most basic, and every microcontroller supports it.
  2. Serial programming: this is the easiest method in my opinion, though some lowcost microcontrollers do not have this provision.
  3. Programming using a bootloader: this is available only for advanced microcontrollers. It has many advantages, but is much more complex that the other two techniques. The bootloading technique requires two steps. In the first step you need to load a small program called a bootloader into the microcontroller. Once it is there, the booloader can download the rest of the program from the PC and put it inside the microcontroller.
Guideline 1: Use serial programming for your first programmer.
There are at least three reasons for this.

Reason 1: Microcontrollers need 5V for running. Apart from this, parallel programming needs an external 12V to be applied for a very short time to one of the pins. So you need external circuitry for both 5V and 12V. But serial programming uses only 5V.

Reason 2: The 12V pulse in parallel programming must not be too long (unless you do not mind toasting the microcontroller). The data sheet specifies the exact maximum allowable pulse widths. One typical value is 500ns. To produce pulse of such narrow width you cannot use your PC directly, since standard operating systems like Windows or Linux or Mac cannot guarantee pulse widths down to such precision. So you will need another microcontroller (which you have to program somehow) to generate the pulses. But for serial programming the pulses can be as long as you wish. So you can generate them directly from the parallel port of your PC, and make them very slow at first so that you can see the pulses by connecting LEDs to the pins. That is how we shall start out.

Reason 3: Parallel programming requires many wires (more than 8), while serial programming requires just 5.

However, serial programming is not supported by all microcontrollers. All AVRs do. And so does AT89S51/52. But not AT89C51. To find out whether your microcontroller does, just look at the pin out. If there are pins called MISO and MOSI then it supports serial programming. Otherwise it does not.

In this tutorial we shall use AT89S52. Everything will carry through verbatim for AT89S51. I shall also present a programmer for ATMEGA8515 written along the same line. The same principle should work for many other microcontrollers also.

We shall provide codes for three different platforms: Windows XP, Knoppix and Fedora 8.

Next
© Arnab Chakraborty (2008)