Programming a Microcontroller
For the examples in this book, we will be using PIC chips that have flash ROM program memory, which can be easily erased and reprogrammed. This is very useful when learning, but also allows the firmware (microcontroller program) to be upgraded in any application, adding an app to a mobile phone, or upgrading its operating system. It is the same kind of memory used to store the image data in the SD card in the camera, and for general storage in a memory stick.
There are two ways of programming the PIC microcontroller. The preprogramming system is shown in Figure 1.10. The programming interface is the basic PICSTART Plus module, which accepts dual-in line (DIL) pin-out PIC chips up to 40 pins in a zero insertion force socket. The serial connection to the host PC COM port is made via an RS232 lead. This protocol is rather slow, and the COM port connector is not usually fitted to current PCs, so it is being replaced by USB in current programmers.
Alternatively, the PIC can be programmed in circuit, that is, after it has been fitted into the finished circuit board. This is known as in-circuit serial programming (ICSP), and the same hardware can also support in-circuit testing and debugging (ICD), as seen in Figure 1.11. The in-circuit programming module is the Microchip ICD2, which connects to the host via USB and to the target system via a six-way RJ-45 connector. In this case, the application board is the PIC Mechatronics demo board, which is used to investigate control of brushed dc motors and stepper motors.
The program is written as a text file and converted (assembled) to machine code (hex) in the host computer, using suitable development system software, usually Microchip MPLAB integrated development environment (IDE). Mistakes in the source code must be corrected before a hex file can be successfully created. The program operation can then be tested in MPLAB and downloaded to the target system.
Electronic computer-aided design (ECAD) software such as Proteus VSM also allows us to simulate the circuit on screen, in order to debug the program before downloading. The complete circuit can then be checked for correct operation, ideally by running the microcontroller in debug (fault-finding) mode, if this is supported by that particular device. All these techniques will be explained later on.
The basic technology for implementing digital systems is described in appendices at the back of this book. If you are not familiar with any of these hardware concepts, please refer to these sections as necessary. Appendix A covers information coding and assembler programs, Appendix B describes the basic electronics of digital systems, and Appendix C show how these work together to provide data input, storage, processing and storage devices.