Program Design
There are international standards for specifying engineering designs, which should be applied in commercial work. The design standards for different types of products will vary; for instance, a military application will typically be designed to a higher standard of reliability and more rigorously tested and documented than a commercial one. Our designs here are somewhat artificial in that they are intended to illustrate features of the PIC microcontroller, rather than meet a genuine user requirement. Nevertheless, we can follow the design process through the main steps.
Application Specification
The first step in the design process is to specify the functions and performance required by the application. In the real world, this needs to be done in some detail so that the overall design, development and production costings and timescales can be predicted as far as possible, as well
as establishing the market or customer requirements. For our purposes, the minimal specification given in Chapter 3 will suffice:
The circuit should output a binary count to eight LEDs, under the control of two push-button inputs. One input will start the output sequence when pressed. The sequence will stop when the button is released, retaining the current value on the display. The other input will clear the output (all LEDs off), allowing the count to resume from zero.
The next step is to design the hardware in which the application program will run, unless it already exists. A block diagram, which shows the user interface requirements, is a good starting point. The interfacing of the microcontroller is often implemented using certain standard devices, such as push buttons, keypad, LED indicators, LCD, relays and so on. The circuit design techniques required will not be covered in any detail here; the most common interfacing techniques and devices are described in Interfacing PIC Microcontrollers: Embedded Design by Interactive Simulation by the author (Newnes 2006). The microcontroller must be selected by specifying the requirements such as:
• Number and type of inputs and outputs (based on chip pin-out)
• Program memory size (maximum number of instructions)
• Data memory size (number of spare file registers)
• Program execution speed (clock speed)
• Availability of special interfaces (e.g. analogue inputs, serial ports).
The hardware configuration for the BINx applications has already been described in Chapter 3 (Figure 3.3). We have established that the instruction set and programming features of the microcontroller selected are suitable. If further features were required, the existing hardware design could be modified. If the microcontroller selected was then found to be lacking in some respect, for example, not enough input/output (I/O) pins, another microcontroller, or other types of hardware such as a conventional microprocessor system, may be considered. However, it is easier to stay within one family of processors, since it is normally based on a shared architecture and instruction set, from which the most suitable can be selected. The PIC range is the most extensive available for small and mid-range applications.
Microcontrollers are normally used in so-called real-time applications, typically a control system where inputs are measured and outputs modified as rapidly as possible, such as a motor vehicle engine controller. Complex data processing may be needed, but data storage is minimal. For simpler applications, assembly language provides maximum speed and minimum memory requirements, but for more complex software, a higher level language may be needed. This will provide a greater range of more user-friendly statements and constructs, such as mathematical functions and display drivers. The more powerful PIC microcontrollers are therefore generally programmed in the language C, the next level up from assembler. The basic syntax is more like English than assembler, and is therefore easier to learn and does not depend on an intimate knowledge of the MCU architecture. The downside is that each C statement
translates into several assembler instructions, so the program is longer and therefore slower to execute. It also needs more program memory. Programming PIC microcontrollers in C is introduced in Programming 8-bit PIC Microcontrollers in C with Interactive Hardware Simulation by the author (Newnes 2008).
Program Algorithm
A flowchart can be useful for illustrating the overall algorithm (process) graphically, particularly when learning programming. A flowchart for BIN3 is shown in Figure 4.2.
The program title is placed in the start symbol at the top of the flowchart, and the processes required are defined as a sequence of blocks. Each flowchart box will contain a description of the action at each stage, using different shaped boxes for processes (rectangle), input and output (sloping) and decisions (pointed). The decision box has two outputs, to represent a conditional branch in the program. This decision box should contain a question with the answer yes or no, and the active selection labeled YES or NO as appropriate; only output one needs to be labeled. The jump destinations can also be labeled, and these same labels can be used in the program as address labels.
Programming packages exist that allow a flowchart to be converted directly into code, which are most useful in education and training environments. Software design techniques, including flowcharts, will be discussed in more detail later.