Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering

How to create a header (.h) file

By Dishant Shah

·               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.
 
Screenshot of Project Menu in Keil IDE
 
Fig. 1: Screenshot of Project Menu in Keil IDE
 
Screenshot of New Project Creation in Keil
 
Fig. 2: Screenshot of New Project Creation in Keil
 
Screenshot of selecting target device in Keil IDE
 
Fig. 3: Screenshot of Selecting target Device in Keil IDE
 
Screenshot of message received on New Project Creation in Keil
 
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)
 
Screenshot of C Code in Keil IDE
 
Fig. 5: Screenshot of C Code in Keil IDE
 
Screenshot of saving 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
 
Screenshot of saving code in Keil IDE
 
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.
 
Screenshot of including sample.h file in new project
 
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 EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • PS2 Keyboard To Store Text In SD Card Using Arduino Circuit Setup On Breadboard
    How To Use PS2 Keyboard To Store Text In SD Card Using Arduino- (Part 42/49)
  • Wireless Path Tracking System Using Mouse, XBee And Arduino Circuit Setup On Breadboard
    How To Make A Wireless Path Tracking System Using Mouse, XBee And Arduino- (Part 43/49)
  • How to Make a Wireless Keyboard Using Xbee with Arduino- (Part 44/49)
  • Making Phone Call From GSM Module Using Arduino Circuit Setup On Breadboard
    How to Make Phonecall From GSM Module Using Arduino- (Part 45/49)
  • How to Make a Call using Keyboard, GSM Module and Arduino
    How To Make A Call Using Keyboard, GSM Module And Arduino- (Part 46/49)
  • Receiving SMS Using GSM Module With Arduino Prototype
    How to Receive SMS Using GSM Module with Arduino- (Part 47/49)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • Renesas delivers intelligent sensor solutions for IoT applications
  • Microchip Technology releases AVR-IoT Cellular Mini Development Board
  • Qualcomm acquires Cellwize to accelerate 5G adoption and spur infrastructure innovation
  • MediaTek’s chipset offers high-performance option for 5G smartphones
  • Nexperia’s new level translators support legacy and future mobile SIM cards

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr bluetooth dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Fixing hold violations in Innovus without inserting delay on setup timing paths
  • SDC constraints for MUXed clock input + clock divider + MUXed clock output
  • DC Motor Speed Control
  • D Flip Flop frequency divider
  • FPGA LVDS with separate clock

RSS Electro-Tech-Online.com Discussions

  • intro to PI
  • Are Cross-wind compensation and Road crown compensation functions inputs to LKA function?
  • we are facing an issue with one-wire communication by using DS2485
  • RF modules which can handle high number of bytes per second
  • Component identification.
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering