Wednesday, March 23, 2011

Chapter 4: Making Your Own Programmer

We have already explored some theory. We can't wait to start building embedded projects. I discussed the embedded system development process in chapter 2. Programming a Microcontroller requires some hardware & some software. The upcoming chapters will cover the following two parts,
  • Setting up your Hardware
  • Setting up your Software
We will need a computer. We will devise an interface that will connect our Programmable Device to the PC.
Firstly, we will set up our hardware. The AVRs support In System Programming. I will give you some idea about it. This chapter will exclusively deal with In System Programming.




In System Programming
AVR Microcontrollers can be programmed in system. They support In System Programming (ISP). There is no need to remove the chip from the embedded system. It can be programmed or reprogrammed on the run.
 
Fig: In System Programming
In many cases we will program the AVR before installing it in a permanent board. We may need to reconfigure or update the program. We will just need access to
some of the pins of the AVR that will let us program it.

We need an ISP programmer to do this job. The word ‘Programmer’ means, a set of special hardware and software which in combination facilitate the programming process.

Atmel Corporation itself manufactured AVR programmers with different features. Third party programmers are also available. There are universal programmers which can program any programmable device including the AVRs, PICs etc.

In Dhaka, a few models of universal programmers are available. The prices are around 6000/7000 taka and above.

There is an alternative way. Instead of buying one, we can build one. It will not contain a lot of features like those in the market but will certainly do our job. I will describe all the procedures in a video. Before that, let’s cook some theory about ISP.



ISP Connection
Look at the following picture,
 
Fig: ISP connection diagram
In our case the PC will be the programmer and will act as Master. The target uC will act as Slave. The data exchange between the master and the slave is done serially using a very simple three wire SPI (Serial Peripheral Interface). The SPI along with VCC, GND and RESET are the six lines that we will connect.

VCC
To program the Microcontroller, we need to power it up. This can be done by feeding the Microcontroller from the Programmer through this line. This line can also be used by the programmer to draw power from the system.(If programmer lacks a power supply)


So the VCC connection can transfer power in either way as required.

However, the PC runs on its own power and our Microcontroller will have a dedicated power supply connected to it. So this line is optional. With this line connected, the MCU can be programmed without any external power. (Our PC will supply the power)

GND
The reference voltage of our PC and the target Microcontroller must be same for successful programming. We must connect the grounds of Master & Slave.

RESET
It is an active low pin. The Microcontroller enters the serial programming mode when RESET is active (Low). It must be kept active during programming. It must be toggled at the end of each erase cycle. Our programmer software will handle this task.


Serial Peripheral Interface (SPI)
The SPI is a three wire interface for serial communication between Programmer and Microcontroller. The AVRs are programmed using SPI. It consists of the following three lines,

MOSI
MOSI stands for Master Out – Slave In. In this line, the Master gives the output and the Slave receives it as input. The data is transferred from the Programmer to the AVR through MOSI.

MISO
MISO stands for Master In – Slave Out. In this line, the Slave gives the output and the Master receives it as input. The data is transferred from the AVR to the Programmer through MISO.

SCK
It stands for Serial Clock. Serial data transfer requires a Clock. The clock pulses are generated by the Master. Clock pulses are required to keep synchronization between data transmission and reception on both the sides.

At each clock pulse,
  • 1 bit of data goes from the Master to the Slave through MOSI.
  • 1 bit of data goes from the Slave to the Master through MISO.



How to connect to the PC?
We have came to know the six lines of the ISP Cable. You can identify these six pins on the AVR from its pin configuration.

What about the master? In our case, we will program the AVR directly from the PC. Our PC will be the master.

Behind the computer casing, there is an I/O panel of the motherboard where the Input/Output ports of our PC are situated.  Among them, these three types of ports can be used for programming our devices,
  • Serial Port: DE-9 (9 Pins), COM1, COM2 etc.
  • Parallel Port: DB-25 (25 Pins), LPT1
  • USB Port (Universal Serial Bus)
Note: We can not direcly program the AVR using USB. We need to build an intermediate device that will take the data from the PC via USB and Transfer it to the AVR via SPI. In this case, that intermediate device will be the Master.

Fig: The three commonly used ports for pragramming

From the very early days, Parallel and Serial ports were used for device programming. USB based programmers are becoming popular now.

If you are interested, you can visit these links to the two sample programmers in the Atmel products Page. These programmers communicate with the PC using USB. You will see that, some of the programmers support In System Debugging using JTAG interface.

Sample Model: AVRISP mkII
Sample Model: STK600

I have given these links to give you an idea. There are many other models available. I am not suggesting any model here, nor asking you to buy one.

In fact, in our country, they are not available. However, I have seen some third party Universal Programmers from Chinese vendors. They can program various devices including those from Microchip, Intel, AMD, Atmel, Philips etc. Yet they do not seem worth their price. We will use a homemade one!

I told you earlier, we will make one ourselves. We can either choose Parallel, Serial or USB for programming the AVRs. Among them the most simple and easiest way is to program via the 25 pin Parallel Port. I will describe the ISP construction which employs the Parallel Port.

Notice: A good news for those who want to make an USB Programmer. I have made my USB Programmer. If you want to make one, you can directly skip this chapter from this point and go to Chapter 6 of this blog. The theory up to this part regarding In System Programming applies for USB too. Most of the topics of Chapter 5 except the Pony Prog Part will be needed for the USB programmer. So don't skip Chapter 5.

For those who want to make a Parallel one please continue reading this chapter.

Note: The following section is for those who have been unable to locate there computers parallel port. Other readers can skip it.



Where is the Parallel Port?
The parallel port should be behind your casing where the I/O panel is situated. Most motherboards have this port, but some newer motherboards do not!

To tell you the truth, the vendors are excluding the parallel port as USB is taking over everywhere. But it may not be entirely excluded. See your motherboard manual. You might find something.
Fig: On board LPT/Parallel connection

I also faced the same problem. When I saw the motherboard diagram, I found a LPT connector on the board. Like my one, some of the motherboard manufacturers have included it on board instead of the rear panel.

Next, we will need a parallel port adapter. You will get it in the computer market. It should cost you around 200tk. Technically this is called, DB25 to IDC26 adapter.
Fig: DB25 to IDC26 adapter
 
Connect the IDC26 to the LPT/Parallel socket in the motherboard and mount the bracket on your casing. Your port should be ready.

Some vendors may exclude the parallel interface entirely. An alternate method can be a Parallel PCI adapter which fits in the PCI socket of the motherboard. I have never tried this but I believe it will work.
Fig: Parallel PCI Adapter
There are few USB to Parallel converters and Serial to Parallel converters also. I never tried them and I am not sure whether they will work with ISP or not. You have to find out yourselves.

Lastly if you can't find any way out, you can go for Serial or USB. I will try to explain these later. For now, I can give you following links which might be helpful for you,



ISP Cable Construction
 Look at the following diagram,

Fig: Our ISP cable diagram
 
The six lines of the ISP connector will go to the target AVR. We will drive the SPI lines from the PC using a serial programming software.

We need to isolate the PC and the AVR board using a buffer. So we have used a Tri State Quad Buffer, 74HC125 in the middle. This isolates the PC and the AVR; this is for safety.

You can see the 0.1uF capacitor and the 100k resistor in the diagram. These are optional. It’s good to use them if possible but they can be excluded. I did not use them.

I have followed this diagram to make my ISP cable. I have tried to demonstrate the complete process in the following two videos, Part 1 and Part 2.
I would like to mention that, I was inspired by this ISP cable construction technique from a nice tutorial by Mr. Rajib Mikail, Lecturer, Department of Electrical and Electronic Engineering, BUET.

This is the Part 1 of my video,


This is the Part 2 of my video,


To view them in better quality, watch them at YouTube. To do so, click on the YouTube logo at the lower right corner of the video window. These are available up to 720p HD quality. You can choose the video quality from the lower right corner which is by default selected to 360p.



List of Necessary Items & Tools
I have given a list of the items and the tools that will be required to make this ISP cable. Notice, in the list I have included several fields. Read the following notes carefully,
  • The price given here is the minimum price I have been able to buy when I collected the items. Prices change everyday. I have included the price to help you plan your wallet.
  • The prices vary in different markets. In some markets prices are high, in the others prices are lower. So I have given the preferable places in the "Where to buy?" column.

 Click on the following pictures and open them in the new window to have a larger view.

Necessary Items:


Necessary Tools: 



If you have completed the ISP Cable Construction, proceed on to the next chapter to test it and experience programming with it for the first time.

2 comments:

  1. ¡Gracias! ...ha sido útil tu información.
    Saludos desde España

    --maelva

    ReplyDelete