Programming Microcontrollers In C:Structure of a Microcontroller-based C Program

Structure of a Microcontroller-based C Program

The structure of a C program developed for a microcontroller is basically the same as the structure of a standard C program, with a few minor changes. The structure of a typical microcontroller-based C program is shown in Fig. 2.1. It is always advisable to describe the project at the beginning of a program using comment lines. The project name, filename, date, and the target processor type should also be included in this part of the program. The register definition file should then be included for the type of target processor used. This file is supplied as part of the compiler and includes the definitions for various registers of the microcontroller. In the example in Fig. 2.1, the register definition file for the Atmel 89C2051 type microcontroller is included. The global definitions of the variables used should then be entered, one line for each definition. The functions used in the program should then be included with the appropriate comments added to the heading and alsotoeach line of the

Programming-Microcontrollers-in-C-00

functions. The main program starts with the keyword main(), followed by the opening brackets ‘{‘. The lines of the main program should also contain comments to clarify the operation of the program. The program is terminated by a closing bracket ‘}’.

An example program is shown in Fig. 2.2. This program receives an 8-bit data from port 1 of an 89C2051 type microcontroller. The state of a switch, connected to bit 0 of port 3, is then checked. If the switch is 1, the value of the data is doubled by calling function double_it. If, on the other hand, the state of the switch is 0, the data value is incremented by 2 by calling function inc_by2, and then the program stops. It is important to realize that there is no returning point in a microcontroller program. Thus, where necessary, an endless loop should be formed at the end to stop the program from going into undefined parts of its code memory.

Leave a comment

Your email address will not be published. Required fields are marked *