Engineers Gallery

How to create a header (.h) file

For programming any microcontroller, the general approach is to declare and define functions in the same file in which the main logic of the program [generally in main () function] is written.
·              This approach is universally adopted because it is easy for the reader to understand the whole program as the functions used in the main logic is somewhere in the same file.
·               However this approach becomes very difficult when the program is written using many functions or it has to handle too many peripherals.
·         Assume one application in which peripherals such as 16×2 LCD, 4×4 keypad, RFID module and GSM module are connected with microcontroller.
·               As we know, all of the above devices need specific functions for communication and operation of devices. If now programmer attempts to write all the functions in the same file, then can we even think how difficult it will be for programmer to manage .?
·               Now you can realise something is required to handle large applications, isn’t it?
·               That something is “MODULAR PROGRAMMING”.
·              MODULAR PROGRAMMING is a programming approach in which the functions are written in another file (assume myfile.c) and then one header file (myfile.h) is created.
·               If programmer needs to use any of the functions, he/she has to write just one line include<myfile.h>. Now programmer can use any of the functions.
·               Now think of the above application. Instead of hundreds of lines and lots of function, the main file will contain few lines such as
#include<lcd.h>
#include<gsm.h>
#include<keypad.h>
#include<rfid.h>
in addition to the main function.
·               This approach definitely decreases the complexity of the program and at the same time, increases the reusability of the functions as once the .h file has been created, it can be used in any of the program.
Now let us see how to implement MODULAR PROGRAMMING.
1.            Create myfile.h file which contains the function declaration.
2.            Create myfile.c file which contains function definition.
3.            Include myfile.h in main program.
This procedure in illustrated in following section using KEIL µVISION 4.0 compiler.
Exit mobile version