Continue to Site

# Header file creating in Keil

Status
Not open for further replies.

#### thannara123

Hi experts ,i want to know that how can i create Header file in keil please post step by step. thanks for all replays

#### ckshivaram

first create a file header file. these usually do not need any other header file to be added in it, but if you declare any variables that is dependent of any other , then you have to include that header file in the header file you are creating. example

i have pins.h in this header file i use reg51.h as i use variables defined by other header file...
Code:
/*  Header file declaration  */
#include<reg51.h>

/*  SFR declaration          */
sfr lcd=0x90;

/*single bit declaration */
sbit RS=0x90;
sbit EN=0x91;
sbit relay=P3^7;

i have 2nd header file lcd.h here i dont use any header file in my header file...
Code:
void lcd_cmdwrt(unsigned char cmd);
void lcd_datawrt(unsigned char *);
void dat(unsigned char);
void lcd_init();
void delay(unsigned int );

then i write my C file as main.c

Code:
/*  Header file declaration  */
#include<lcd.h>
#include<pins.h>
#include<string.h>

void main()
{
lcd_init();		   //To initalise the lcd
while(1) //infinity loop
{
lcd_cmdwrt(0x01);			//clear on lcd
delay(10);

}
}

Points: 2

### ramina

Points: 2

#### thannara123

Sir i didnt get well ,that is .
where do i make it (note pad or in keil or anything ?)
Where it put ?(keils directory )
I want to call my own functions which are written in header file (my own)?

#### ckshivaram

write it in keil by opening new file, or type it in notepad but save it as .h file and add it to keil project where all files are added.. and compile it with other files in the project window.......

store all these header files in the same folder where your .C files are stored...

ramina

### ramina

Points: 2

#### FvM

##### Super Moderator
Staff member
Generally, header files are holding the data definitions and function prototypes that shall be shared by multiple source files. As other source files they can be created in any text editor or the Keil IDE's file editor. For simple "flat" projects, they are stored in the project directory. For complex projects with subdirectories, it's your choice.

Why don't you study some example projects?

Points: 2

### thannara123

Points: 2

#### alexan_e

The header file is just a text file with a .h extension, use any text editor to create it.
You can place it to your project folder or in the include path of your compiler, if you place it in the project folder then you can include it using #include "headername.h" or if it is in the include path #include <headername.h>

Alex

Points: 2

### ramina

Points: 2

#### thannara123

Sir can you give me an another example
Having delay function
delay()

Last edited:

#### ckshivaram

delay.h

Code:
void delay(unsigned int );

delay.c
you can write it as delay.c or write the delay.c code in the main program

Code:
#include<delay.h>

void delay(unsigned int sec)
{
unsigned int i,j;

for(i=0;i<sec;i++)
for(j=0;j<1500;j++);
}

main.c

Code:
#include<delay.h>
#include<reg51.h>

main()
{
delay(100);

}

Points: 2

### ramina

Points: 2

#### alexan_e

ckshivaram I will use your code to give an example of what I do

delay.h

Code C - [expand]1
2
3
4
5
6
7
// the #ifndef prevents the file from being included more than once
#ifndef __DELAY_H__
#define __DELAY_H__

void delay(unsigned int );

#endif /* __DELAY_H__

delay.c

Code C - [expand]1
2
3
4
5
6
7
8
9
#include <delay.h>

void delay(unsigned int sec)
{
unsigned int i,j;

for(i=0;i<sec;i++)
for(j=0;j<1500;j++);
}

then in the main you have to include the delay.h in the main code and add the delay.c to the project files

Code C - [expand]1
2
3
4
5
6
7
8
#include<delay.h>
#include<reg51.h>

main()
{
delay(100);

}

Alex

thannara123

### thannara123

Points: 2

#### thannara123

thanks for all i get idea

#### rajivpatnaik

##### Newbie level 1
write it in keil by opening new file, or type it in notepad but save it as .h file and add it to keil project where all files are added.. and compile it with other files in the project window.......

store all these header files in the same folder where your .C files are stored...

The best thing is to simply open a "New" file in keil, Enter the required data(for header file) in the "New" file as advised by as ckshivram. Save as "delay.h" in the same directory. Compile. You will see the delay.h integrated in the Project directory as a file called "delay" without an extension but type as H. Avoid using another editor like notepad and add to keil.....ckshivaram ROCKS!!!!!!!