Program Downloading
After testing in the simulator for correct operation, the machine code program can be blown into the flash memory on the chip. The program is downloaded via a serial link into a specific pin, RB7 in the case of the 16F84A. There are two methods for program downloading, outlined below.
Programming Unit
The original programming method for PIC chips required the chip to programmed before fitting in the finished hardware circuit. A programming unit was plugged into the serial port of the PC (COM1 or COM2) and the chip inserted into the zero insertion force (ZIF) socket on the programmer (Figure 4.5). The chip orientation had to be carefully checked, as inversion would reverse the supply pins and burn out the chip. Antistatic precautions had to be observed, since the PIC is a complementary metal oxide semiconductor (CMOS) device, and static discharge on a pin can break down the field effect transistor (FET) gate insulation in the internal circuitry. Before downloading, the correct device must be selected in MPLAB under device specifications, and the configurations bits set as described below. These can also be set up using an assembler directive in the source code.
Oscillator (Clock): RC
The main options in the earlier chips were ‘RC’ and ‘XT’. RC must be selected for the oscillator configuration used in the BIN hardware. XT will be selected in later applications
sing an external crystal oscillator. The program will not run in the hardware if the wrong type of oscillator has been selected, so check this carefully. More recently introduced chips now have a third option, an internal oscillator that eliminates the need for external components to control the clock. This is now generally the preferred option.
Watchdog Timer: Off
The watchdog timer (WDT) is an internal timer, which automatically restarts the program if it is not cleared back to zero within 18 ms, using the instruction CLRWDT. This can be used to stop the controller hanging, owing to an undetected program bug or an input condition that has not been predicted in testing. For applications not using this feature, WDT must be switched off, or the program will reset repeatedly, preventing normal operation.
Power-up Timer: On
Mains-derived power supplies may take some time to reach the correct value (þ5 V) when first switched on. The power-up timer (PuT) is an internal timer which delays the start of program execution until the power supply is at the correct voltage and is stable. This helps to ensure that the program starts correctly every time. It is not relevant in simulation mode, but should be enabled when the program is downloaded for hardware operation.
Code Protect: Off
If the code protect (CP) bit is enabled, the program cannot be read back into MPLAB and copied or manipulated. This is normally only necessary for commercial applications to prevent software piracy, so we can switch off code protection for our simple examples.
For program downloading, PICSTART would be selected from the Programmer menu. If the programming unit had been correctly connected, a programming dialogue was displayed, with the hex code to be downloaded visible (Figure 4.6; note that this illustration dates back to version 5 of MPLAB). When the configuration bits had been checked, the Program operation could be selected to download the machine code. When complete, confirmation of successful programming should be received, and the chip manually transferred to the application circuit, again observing antistatic precautions.
In-Circuit Programming and Debugging
In-circuit programming is now usually the preferred downloading method, where the chip is left in-circuit after assembly of the printed circuit board (PCB), allowing programming and final debugging in the final hardware. When a board is produced in volume, the programming can be done as the final stage when the hardware is complete. This is also very useful at the prototyping stage, as program modifications can be more rapidly and safely implemented and tested.
To facilitate in-circuit programming and debugging (ICD), the board is designed with a six-pin connector, which connects the programming pins on the chip to a programmer/ debugger module that is in turn connected to the universal serial bus (USB) output on the host PC. This system has already been illustrated in Chapter 1 (Figure 1.11), and will explained in more detail in Chapter 7 by examining some Microchip demonstration systems.
When the hardware has been connected, the appropriate programmer/debugger is selected from the programmer menu. PICkit2/3 is the low-cost option, which nevertheless offers some very useful features. With the connection confirmed, the program can be downloaded. The same module can then be selected from the debugger menu and the program run and debugged using the same tools as used in software simulation (single step, breakpoints, etc.). However, program execution now takes place in the chip itself. The interaction with the real hardware can be monitored, and hence the hardware and software verified at the same time. Note that not all chips support in-circuit debugging, especially smaller and older chips. Neither the 16F84A nor the 16F690 supports ICD without a special header, which must be connected between the debugger and the chip. The PIC 16F887 used later on the Microchip 44-pin demo board does support ICD.
When debugging is complete, the final step is to configure the chip to run independently by selecting the Release option in the drop-down menu in the project toolbar. This allows the board to run when disconnected from the debugging module.
Thus, the software can be tested and debugged in stages: initially in MPSIM or Proteus VSM, and finally in the actual hardware. Proteus also provides fully featured PCB design as well as circuit simulation, so the finished hardware can be produced using the same package.