The simplest way to program and read the PIC16F84A MCU

by SV3ORA

Many of your projects eventually may include programming microcontrollers. Before I learn about MCUs, I was always tried to avoid using them in my projects, because I thought it to be very complex. Obviously I was wrong. Deny it or not, MCUs can offer much greater capabilities at a huge fraction of space and cost. The designer has to focus more on writing the software for the function he wants to implement, rather than building the actual circuit.

PIC16F84A is a great little MCU and huge lot of projects are freely available on the internet that use this chip. Being obsolete nowadays, you may be able to find it easily in stocks, or ruin it from older projects. For extensive information on the PIC16F84A, see my PIC dedicated page. The current page has been written for the complete beginner and focused only on the procedure of programming the PIC16F84A and not on how to learn how to write a program for it. The issues covered can be summarized below:

 

Reading and writing a HEX file into the PIC

Writing software for an MCU is a complex issue that involves lot of effort and it usually cannot be done by unskilled people. But if you just happen to already have software that has been written by other people for a project and you want to build this project, then you do not need to know anything about the software itself. You just need to know how to write this software into the MCU. This is the most common situation when building another's project.

All you need is this wonderful little program called PICprog.exe. Download it and run it. It is a standalone program and it does not setup to your computer. If you want to remove it, just delete it. Here is a screenshot of the program:

I have marked the different areas with numbers in red color, so I can describe them.

Number 1: Load the software, which is to be programmed into the chip, in the PICprog buffer. This has to be a HEX file. PICprog cannot compile ASM files into HEX ones and it cannot load BIN files. You have to have the HEX file available.

Number 3: Read an already programmed PIC16F84A into the PICprog buffer. If the PIC16F84A has been previously programmed without code protection, you will be able to extract it's program into the PICprog buffer.

Number 2: Save the program buffer into a HEX file on your computer.

Number 4: Erase the PIC16F84A.

Number 5: Program the PIC16F84A. Write the contents of the PICprog buffer into the PIC16F84A.

Number 7: Choose which type of oscillator to use. I usually use quartz crystals, so I select the XT option.

Number 8: The number of the serial port in which your programmer is connected to, usually COM1.

Number 6: A screenshot of the complete schematic diagram of the programmer you have to build in order to program the PIC16F84A.

The schematic is very easy, easier than your most quick-and-dirty projects. Four resistors, two diodes, a capacitor, a DB-9 connector and a DIP or a ZIF socket is all that is needed to make this programmer. No external power is required, everything is taken out of the serial port of your computer. Yes, this is an ultra portable programmer. And the most important thing, I have build this on a prototype board and it works perfectly ok. No need to go for a more expensive programmer if you are only interested about the PIC16F84A MCU.

Note, this programmer works ok on a desktop PC, but there may be a case when using a laptop with a USB to COM port conversion or low voltage level COM ports. So please have this in mind when using a modern laptop.

 

Extracting the source code from the HEX file

So far, I have talked about extracting or burning a HEX file into the PIC16F84A. But what about if you want to convert this HEX file into it's original source code, so you can inspect the code or make changes? Then you need to dissasemble the HEX file into an ASM file. This may be useful for example in a project that involves a display, if you want to change the display messages to your own customized.

All you need is this wonderful little program called PICDisasm.exe. Download it and run it. It is a standalone program and it does not setup to your computer. If you want to remove it, just delete it. Here is a screenshot of the program:

It has been designed for a variety of PIC microcontrollers. For the PIC16F84A, select the 16F84A option from the PIC-Type field (it is the default selection) and then load the HEX file into the program. The decompiled source code is automatically displayed in the buffer after you load the HEX file.

If you have both the original HEX file and ASM file available, it may be worth it to dissasemble this HEX file and open the produced ASM file with a text editor. Then open the original ASM file with a text editor as well and compare the two ASM files. You will notice differences on the comments and the labels of the two ASM files. This does not mean the two files are different. If you reassemble the dissasembled file into a HEX file, it will work identically with the original HEX file. The assembler just ignores the comments and the labels you have made, because these are not needed by the PIC to run the program. With a little bit of inspection you can figure out where is the part that needs to be changed. To save the buffer into an ASM file for editing, click the save button.

 

Assembling the ASM file into a HEX file

Ok, so now you have your ASM file saved on your hard disk. You may open it with a text editor and make any changes you like to it. The changes may be just labels or text messages or completely rewriting parts of the code, if you know how to program the PIC. If you want to reprogram your PIC16F84A with this altered ASM file or with your own ASM file, you have to assemble it into a HEX file. It is the opposite process from the dissasembly. To assemble the ASM file into a HEX file, that can be burned into the PIC, you can use the official MPASMWIN.exe assembler.

The original version of MPASMWIN has been designed for a variety of PIC microcontrollers but I cut down all the include files to have a single exe file, so this can only work with PIC16F84A by embedding inside your ASM programs the necessary processor parameters. This makes your programs standalone, independent of any header or include files.

To make it easier for you, I include a TEMPLATE.ASM file for the PIC16F84A MCU. Just use this template to write all your new programs. You do not need to select the processor type on the MPASMWIN, because it is already defined inside your ASM program. Just load the ASM source file into the program and click the Assemble button. A HEX file, along with some other files, will be generated in the same directory as your ASM file, if your ASM file is error free. If it is not, you can see the errors by opening the error file generated, using a text editor. You can use the produced HEX file to program your PIC16F84A as described at the beginning of this page.

 

Editing an ASM file

One of the most important things when writing a program in assembly language is to keep the code well documented. Another important thing is to be able to easily read your code later on. Syntax highlighting is important then, but windows notepad lacks this feature. The most lightweight solution is an alternative editor called Notepad2.exe.

This single executable editor offers, among other, syntax highlighting and zoom features, which can be very helpful when writing code.

Back to main site