· 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.
Step-1
STEP-1:We have to create new project in which we have to define functions required for the future use.
Follow: Create new project in KEIL, provide the folder location to save the project files.
Select respective controller and add startup file (which contains address of SFRs) to project.
Fig. 1: Screenshot of Project Menu in Keil IDE
Fig. 2: Screenshot of New Project Creation in Keil
Fig. 3: Screenshot of Selecting target Device in Keil IDE
Fig. 4: Screenshot of Message Received on New Project Creation in Keil
SelectYES.
Step-2
STEP-2: We have to create workbook where we can write function declaration.
FOLLOW: Create new file (ctrl+N).Declare all the functions here and save file with any name with .h (e.g. Sample.h)
Fig. 5: Screenshot of C Code in Keil IDE
Fig. 6: Screenshot of Saving Code in Keil IDE
Step-3
STEP-3: We have to provide function definition in new file.
FOLLOW:Create new file again and write all function body in that file.All functions are given below.Note the use of lcd.h instead of reg51.h
#include<lcd.h>
sbit RS= P1^7; //define RS pin on P1.7
sbit RW= P1^6; //define RW pin on P1.6
sbit E =P1^5; //define E pin on P1.5
// ………………………….FUNCTIONS FOR LCD……………… …..
void delay() //function of delay for small time
{
unsigned int i;
for(i=0;i<10000;i++);
}
voidcr(char C) // function to write commands for LCD
{
unsigned int i;
P0=C;
RS=0;
RW=0;
E=1;
for(i=0;i<100;i++);
E=0;
}
voiddd(char d) //function to write data on Lcd
{
unsigned int i;
P0=d;
RS=1;
RW=0;
E=1;
for(i=0;i<100;i++);
E = 0;
}
voidlcd_init() // To initialize LCD
{
cr(0x38); //2 lines and 5×7 matrix
delay();
cr(0x0e); //Display on, cursor blinking
delay();
cr(0x01); //clear LCD display
delay();
cr(0x80); //force cursor to beginning of 1st line
delay();
cr(0x0c); //display on ,cursor off
delay();
cr(0x80);
delay();
}
voiddata_string(char *a) //puts function to print a string
{
unsigned int x=0;
for(;a[x]!=0;x++)
dd(a[x]);
}
voidclrscreen()
{
cr(0x01); //clear LCD display
delay();
}
voidselectline(unsigned char a) // Select line to display data on LCD
{
if(a==1)
cr(0x80); // Select line1
else
cr(0xc0); // Select line2
delay();
}
voidshiftleft(unsigned char a) // Cursor Shifts towards left
{
unsigned char di;
for(di=0;di<a;di++)
{
cr(0x10);
delay();
}
}
voidshiftright(unsigned char a) // Cursor Shifts towards right
{
unsigned char di;
for(di=0;di<a;di++)
{
cr(0x14);
delay();
}
}
Stpe-4 & 5
STEP-4: Save the file as sample.c
Fig. 7: Screenshot of Saving Code in Keil IDE
STEP-5:Now the programmer can use sample.h and sample.c files for future use. To use these files, copy both the files in the folder where the project is saved.
Step-6
STEP-6:Include sample.h file in new project and write code in main function.
Fig. 8: Screenshot of Including Sample.h File in New Project
As you can see, we have used various functions which are defined in sample.h file but as we don’t have to define it here, the program becomes very small, effective and easy to understand.
In this way, we can create .h files and include it in the project.
Filed Under: Tutorials
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.