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.
Now, to program the PIC16F84A using the PICkit2, we will need the PICkit2 programming software. Go to Microchip.com, 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.
Re-connect the PICkit2 to the USB. Then go to Tools menu and click Check Communication.
The PICkit2 software successfully communicates with the PICkit2 programmer; the result is shown below.
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.
Its time to import the generated HEX file of the led_demo project. Go to File menu and click on Import Hex.
Navigate to the project folder, click on the file led_demo.hex, and click Open to import the file.
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.
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.
Click Cancel to close the Warning! window and change the voltage setting in VDD PICkit2 to 5.0.
Then click on Write button once more. This time, programming is successful.
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.
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.
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.
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.