Microcontrollers have traditionally been programmed using the assembly language. This lan- guage consists of various mnemonics which describe the instructions of the target microcon- troller. An assembly language is unique to a microcontroller and cannot be used for any other type of microcontroller. Although the assembly language is very fast, it has some major disad- vantages. Perhaps the most important of these is that the assembly language can become very complex and difficult to maintain. It is usually a very time-consuming task to develop large projects using the assembly language. Program debugging and testing are also considerably more complex, requiring more effort and more time.
Microcontrollers can also be programmed using the high-level languages. For example, it is possible to use BASIC, PASCAL, FORTRAN and C compilers to program the PIC family of microcontrollers. Most of these compilers generate native machine code which can be directly loaded into the program memory of the target microcontroller.
In this chapter we shall look at the principles of programming PIC microcontrollers using the C language. C is one of the most popular programming languages used today and there are several C compilers available for the PIC microcontrollers. We shall look at how to program the PIC microcontrollers using one of the popular C compilers known as the PICC Lite C compiler, developed by the Hi-Tech Software.
PICC Lite is an efficient compiler which can be used to program the PIC16F84, PIC16F877, PIC16F627 and PIC16F629 family of microcontrollers. This compiler is distributed free of charge and is a subset of the PICC programming language, manufactured by Hi-Tech Software. PICC Lite is equipped with an integral editor with syntax highlighting, which makes program development relatively easy. A large number of library functions are provided which can easily be used by the programmer. Some of the reasons for choosing the PICC Lite compiler are:
• support for floating point arithmetic;
• availability of a large number of mathematical functions;
• direct support for LCD displays;
• ease of availability.
As a programming language, C is similar to PASCAL and FORTRAN where values are stored in variables and programs are structured by operating on variables and by defining and calling functions. Program flow is controlled using if statements, while statements and loops. In- puts can be read from switches, keyboards and sensors, and outputs can be sent to LEDs, LCDs, screens, sound devices, motors and so on. Related data can be stored in arrays or structures.
The program development cycle using C is relatively straightforward. High-level user pro- grams are normally developed on a PC using the integral editor. The code is then compiled and if there are no errors the object code is downloaded into the program memory of the target PIC microcontroller. Depending on the type of microcontroller used, either a flash memory programmer device or an EPROM programmer device is used to load the program memory of the target microcontroller. This cycle is repeated until the developed program operates as required.