Chapter 2 – Programming the PIC16F84A Microcontroller

Now that the led_demo project was successfully compiled and built, we can now download the program to microcontroller. This step is known as “programming” the microcontroller.

First, we need to communicate with the PICkit2 from within MPLAB. With PIC16F84A already selected as the target microcontroller in the Project Wizard (you may also do it in Configure > Select Device menu), connect the PICkit2 to the computer’s USB port. In MPLAB, go to the Programmer menu, click on Select Programmer, and try clicking on PICkit 2.

Unfortunately, PICkit2 is not select-able as shown in the figure below. PICkit2 does not support the PIC16F84A chip in MPLAB. We need another means of programming the PIC16F84A outside of the MPLAB application.


Figure 2.8 PICkit2 is not select-able if using the PIC16F84A
(Click to view larger image)

Now, to program the PIC16F84A using the PICkit2, we will need the PICkit2 programming software. Go to, download the PICkit2 programming application from the PICkit2 product page, then install. Run the PICkit2 software; if the programmer is not connected to the USB, you will get this screen.


Figure 2.9 PICkit2 not found.
(Click to view larger image)

Re-connect the PICkit2 to the USB. Then go to Tools menu and click Check Communication.


Figure 2.10 Check Communication
(Click to view larger image)

The PICkit2 software successfully communicates with the PICkit2 programmer; the result is shown below.


Figure 2.11 PICkit2 found and connected.
(Click to view larger image)

Next, if you still haven’t done so, connect your PIC16F84A circuit to the PICkit2 programmer. (In case you forgot how, go back and read the first part of Chapter 2 to see how it is done.) Click Check Communications again. If successful, it will indicate that both PICkit2 and the PIC16F84A were both detected.


Figure 2.12 Both PICkit2 and the target PIC16F84A are found
(Click to view larger image)

Its time to import the generated HEX file of the led_demo project. Go to File menu and click on Import Hex.


Figure 2.13 Import the Hex file
(Click to view larger image)

Navigate to the project folder, click on the file led_demo.hex, and click Open to import the file.


Figure 2.14 The Hex file is located in the project directory
(Click to view larger image)

The Hex file will be imported to the PICkit2 programming application. Notice that you will get a warning indicating that the file is missing an important information known as the configuration words. The configuration setting is critical to enable the PIC to successfully run the program.


Figure 2.15 Hex file is imported but has warning about missing configuration words
(Click to view larger image)

In the meantime, lets proceed. With PICkit2 device and the target PIC16F84A circuit already enabled, we can start programming the microcontroller. Click on Write to begin the programming process.

If the PIC16F84A is powered by the PICkit2 and not by its own +5V power supply, you will get a warning window below. This is because the PICKit2 may try to provide only +2.5V power and not a proper +5V that it obtains from the USB port.


Figure 2.16 Warning! window appears if the voltage setting is 2.5 volts
(Click to view larger image)

Click Cancel to close the Warning! window and change the voltage setting in VDD PICkit2 to 5.0.


Figure 2.17 Change the VDD voltage setting to 5.0
(Click to view larger image)

Then click on Write button once more. This time, programming is successful.


Figure 2.18 Programming successful
(Click to view larger image)

Next, we must provide power to the PIC16F84A circuit. Click on the check box besides On under the VDD PICkit 2. The PICkit2 will provide +5V power to the target circuit.


Figure 2.19 Click ‘On’ to power up the device.
(Click to view larger image)

At this time, we expect the PIC16F84A to run the newly downloaded program. And what the program does is turn on the LED connected to the RB0 pin.

But the LED is not activated, simply because the program didn’t run.

As indicated several steps back, we failed to include the configuration words on the hex file. The configuration words is needed to properly configure the hardware operation of the PIC16F84A, like which oscillator clock configuration is supposed to run, or whether to enable or disable the watch-dog timer, code protection, and the power-up timer.

To include this critical info in the Hex file, go back to MPLAB. Go to Configure menu and click Configuration bits. The Configuration window will appear. There are four entries to modify but we only need to change two of these.

To change these, first uncheck the box besides Configuration Bits set in code. Since our circuit uses a 4-Mhz crystal oscillator to run the PIC16F84A, we need to set FOSC to XT oscillator. Also, the Watchdog Timer (WDT) should be disabled. The other two remaining settings can be left as is.


Figure 2.20 The Configuration window
(Click to view larger image)

Build the led_demo project again so that the Hex file is updated. Now to export the Hex file that contains the configuration words, go to File menu and click Export. On the Export File window that appears, make sure that the check box besides Configuration bits is ticked.


Figure 2.21 Export the Hex file from MPLAB
(Click to view larger image)

Now go back to the PICkit2 software and import the Hex file once more; there will be no more warning message.

Then, click Write to re-program the PIC16F84A.  Turn on the circuit by clicking the ‘On’ check-box. This time the PIC16F84A will run the program and the LED is activated.

No comments yet.

Leave a Reply