Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to make a proper Header file in C that contains only my functions????

Status
Not open for further replies.

xpress_embedo

Advanced Member level 4
Joined
Jul 5, 2011
Messages
1,154
Helped
161
Reputation
396
Reaction score
189
Trophy points
1,353
Location
India
Activity points
10,591
I had write many functions for my micro-controller programs..
And now i want to place all the functions in a file that will be called by a header file...

For Example
My Function's are:-

Lcd_Init();
Lcd_Clear();
Lcd_Data(unsigned char x);
Lcd_String(unsigned char string[]);

These are my functions i want these to be called from the header file lcd.h

And i have approximately done all these things but my problem is that....
When i call my function Lcd_Init()

The Code size for the whole code is equal to, if have included all the function..


I haven't used any macros.... in the header file...
Can anyone tell me how to do this...
Without increasing the Code Size..



Regards

Arun Sharma
 

arunsharma0731 said:
Can anyone tell me how to do this...
Place the function's prototype (needs a ';' at the end of the line) inside lcd.h and the body inside lcd.c. File lcd.c must include lcd.h. All files that desire to call Lcd_Init(), must include lcd.h.


arunsharma0731 said:
When i call my function Lcd_Init()
The Code size for the whole code is equal to, if have included all the function..
Are you saying that each time you call Lcd_Init(), function's code size is added to the whole project? What is this size? What exactly did you tested to come to such a conclusion?
 

Using a header doen't mean that the functions in the library will be selectivly included, if you use one function of the library then all of them will be included.

An alternative (I don't know how convenient it will be) is to use a modified lib for your project that includes only the functions you need or you can use defines to include specific parts

Code:
// header file


#ifdef _INCLUDE_F1
	void function1(void);
#endif

#ifdef _INCLUDE_F2
	void function2(void);
#endif

#ifdef _INCLUDE_F3
	void function3(void);
#endif


//and in the c file 

#ifdef _INCLUDE_F1
	void function1(void) {
		
	};
#endif

#ifdef _INCLUDE_F2
	void function2(void){
	
	};
#endif

#ifdef _INCLUDE_F3
	void function3(void){
	
	};
#endif


to include any of the functions in your main code you have to define (any of then or all)
#define _INCLUDE_F1
#define _INCLUDE_F2
#define _INCLUDE_F3

and then include the header

Alex

p.s. I hope I did understand the question correctly
 
Suppose My Code is Calling the Following Functions
Lcd_Iniit();
Lcd_Clear();
Lcd_Data(x)
Lcd_String("Arun Sharma");

Then The Code is Suppose 2K

and Obviously it must be less than 2K When i am including only one Functions...
i.e
Lcd_Init(); //Only this functions is included....

Then my Code size must be less than 2K

But in my case it is equal to 2k


lcd.h
contains following declaration
void Lcd_Init();
void Lcd_Clear();
void Lcd_Data(unsigned char x);
void Lcd_String(unsigned char string[]);

my file lcd.c contains
#include"lcd.h>
/*
Then all the Definitions of the Functions are included here
*/


Hope u understand what i mean to say...


In some tutorials on Header file i had seen
#ifndef __LCD_H
#define __LCD_H
//some thing here
#end if

What are these macros, i don;t understand there purpose...


Can you explain me all these things???


Regards
Arun Sharma
 

It is also a good practice to use the following scheme to make sure that the lib is only included once even if it is included in many modules


Code C - [expand]
1
2
3
4
5
6
7
8
// USE THIS IN THE HEADER
 
#ifndef _SOME_UNIQUE_LIB_IDENTIFIER_
#define _SOME_UNIQUE_LIB_IDENTIFIER_
 
//write header content here
 
#endif



The first time that the header is included defines the _SOME_UNIQUE_LIB_IDENTIFIER_ and this prevents the header from being included again since #ifndef _SOME_UNIQUE_LIB_IDENTIFIER_ will be false next time.

Alex
 
arunsharma0731 said:
Then The Code is Suppose 2K

and Obviously it must be less than 2K When i am including only one Functions...
i.e
Lcd_Init(); //Only this functions is included....

Then my Code size must be less than 2K

But in my case it is equal to 2k
Modern compilers should normally optimize this even for no optimization level. Even in this way, you could comment out all not used functions inside lcd.h file. If this does not work, comment out their bodies as well, inside lcd.c. That should work for sure.
 

Thanks Alex....
I am getting what are you saying..........
Everything is clear except your last post......
Even in this way, you could comment out all not used functions inside lcd.h file. If this does not work, comment out their bodies as well, inside lcd.c. That should work for sure.


I don;t understand this line....



Anyways Thanks Alex


Regards

Arun Sharma
 

Code:
#ifndef _SOME_UNIQUE_LIB_IDENTIFIER_   // this means if _SOME_UNIQUE_LIB_IDENTIFIER_ is not defined, the following code will only be executed if this condition is true.
    
    #define _SOME_UNIQUE_LIB_IDENTIFIER_  // then define it 
 
   //write header content here
 
#endif  // end condition

The following part is from View topic - [TUT] Modularizing C Code: Managing large projects :: AVR Freaks

Final step - header file protection

The last thing to do before our conversion is complete, is to protect our header files from multiple inclusion. Take the following example. Say that Main and ADC both refer to each other:

Main.h
Code:
#include "ADC.h"


ADC.h
Code:
#include "Main.h"


What happens when this is compiled? The preprocessor will look at Main.h, then include ADC.h. However, ADC.h includes Main.h, which again includes ADC.h, etc...

To guard against this problem, we can use preprocessor defines. The following code snippet is the basic protection setup:

#ifndef MAIN_H
#define MAIN_H

// Header file contents

#endif


This construct, when applied to each of your header files, will protect against multiple inclusions. As each C file is compiled, the associated header file is included, as well as any other referenced header files (via includes in the C file's header file). As each header is included, a check is performed to see if the header's unique token is already defined, and if so the inclusion halts to prevent recursion. If the token is not already defined, the preprocessor defines it and looks at the remainder of the header file's contents. By giving each header file a different token (typically the header's filename in ALL CAPS, and the period replaced by an underscore), this system will prevent any preprocessor troubles.

Alex
 

arunsharma0731 said:
I don;t understand this line....

Comment out the unused functions like that:

Code:
void Lcd_Init (void);
[B]//[/B]void Lcd_Clear (void);
[B]//[/B]void Lcd_Data (unsigned char x);
[B]//[/B]void Lcd_String (unsigned char string[]);

Only LCD_Init() is active now. If code does not get smaller by this you should comment out those functions in lcd.c.

Code:
void Lcd_Init (void)
{
...............
}
[B]/*[/B]
void Lcd_Clear (void);
{
...............
}
void Lcd_Data (unsigned char x);
{
...............
}
void Lcd_String (unsigned char string[]);
{
...............
}
[B]*/[/B]


Hope that helped.
Alexandros
 

Helllo!!! Alexandros
I again stuck with this problem

Here is my header file lcd.h

Code:
//This is LCD Header File

#define _XTAL_FREQ 20000000
#define LCD_DATA PORTB
#define RS PORTCbits.RC0
#define RW PORTCbits.RC1
#define EN PORTCbits.RC2

#ifndef __LCD_H
#define __LCD_H

	void lcd_data(unsigned char value);
	void lcd_data(unsigned char value);

#ifdef _LCD_INIT
	void lcd_init(void);
#endif

#ifdef _LCD_CLEAR
	void lcd_clear(void);
#endif

#ifdef _LCD_DATA_STRING
	void lcd_data_string(unsigned char msg[]);
#endif

#ifdef _LCD_FIRST_ROW
	void lcd_first_row(void);
#endif

#ifdef _LCD_SECOND_ROW
	void lcd_second_row(void);
#endif

#endif

And this one is my lcd.c file

Code:
/*All LCD Functions Definitions*/
#include<htc.h>
#include<string.h>
#include"lcd.h"


void lcd_cmd(unsigned char value)
{
	LCD_DATA = value;
	RS = 0;
	RW = 0;
	EN = 1;
	__delay_ms(1);
	EN = 0;	
}

void lcd_data(unsigned char value)
{
	LCD_DATA = value;
	RS = 1;
	RW = 0;
	EN = 1;
	__delay_ms(1);
	EN = 0;
}

#ifdef _LCD_INIT
	void lcd_init(unsigned char value)
	{
		lcd_cmd(0x38);
		lcd_cmd(0x0E);
		lcd_cmd(0x01);
		lcd_cmd(0x06);
		lcd_cmd(0x80);			
	}
#endif

#ifdef _LCD_CLEAR
	void lcd_clear(void)
	{
		lcd_cmd(0x01);		//Clears The LCD
	}
#endif


#ifdef _LCD_DATA_STRING
	void lcd_data_string(unsigned char msg[])
	{
		unsigned int j,len;	
		len = strlen(msg);
		for(j=0;j<len;j++)
		{	
			lcd_data(msg[j]);
		}
	}
#endif

#ifdef _LCD_FIRST_ROW
	void lcd_first_row(void)
	{
		lcd_cmd(0x80);
	}
#endif

#ifdef _LCD_SECOND_ROW
	void lcd_second_row(void)
	{
		lcd_cmd(0xC0);
	}
#endif

and here is my Program
main_prog.c which uses these header files..

Code:
#include<htc.h>
#include"lcd.h"

#define _LCD_INIT
#define _LCD_CLEAR
#define _LCD_DATA_STRING
#define _LCD_FIRST_ROW
#define _LCD_SECOND_ROW

void main()
{
	lcd_init();
	__delay_ms(100);
	lcd_clear();
	while(1);
//	lcd_data_string("Arun Sharma");
}

Can you tell me where is the Problem, because my program doesn't gets compiled

Here is the snap of the error
Capture.png
 

You should use the #define and then #include"lcd.h"

Alex
 

Another possible option would be to simply included the #defines relevant to the LCD routines:

Code:
#define _LCD_INIT
#define _LCD_CLEAR
#define _LCD_DATA_STRING
#define _LCD_FIRST_ROW
#define _LCD_SECOND_ROW

at the top of your LCD routine header file, lcd.h, like so:

Code:
//This is LCD Header File

#define _XTAL_FREQ 20000000

[COLOR="#FF0000"]
#define _LCD_INIT
#define _LCD_CLEAR
#define _LCD_DATA_STRING
#define _LCD_FIRST_ROW
#define _LCD_SECOND_ROW
[/COLOR]

#define LCD_DATA PORTB
#define RS PORTCbits.RC0
#define RW PORTCbits.RC1
#define EN PORTCbits.RC2

#ifndef __LCD_H
#define __LCD_H

	void lcd_data(unsigned char value);
	void lcd_data(unsigned char value);

#ifdef _LCD_INIT
	void lcd_init(void);
#endif

#ifdef _LCD_CLEAR
	void lcd_clear(void);
#endif

#ifdef _LCD_DATA_STRING
	void lcd_data_string(unsigned char msg[]);
#endif

#ifdef _LCD_FIRST_ROW
	void lcd_first_row(void);
#endif

#ifdef _LCD_SECOND_ROW
	void lcd_second_row(void);
#endif

#endif

This technique encapsulates any required configuration settings into a single header file and limits any changes relevant for the LCD routines to a single header file as well.

Most well written and portable library routines use such a technique. You can utilize these routines by simply #include "lcd.h" or #include <lcd.h> if you include a common search path for your library routines, header and source files, modify the appropriate #defines within this header file for a particular implementation of an LCD interface in a new program and you're all set.

I would also add verbose comments concerning the exact purpose of each of the #defines within your header file, they come in handy to refresh your memory at a glance.

BigDog
 

Its Still not working...

And giving a huge number of errors
:-(
 

So you have used in main

Code:
#define _LCD_INIT
#define _LCD_CLEAR
#define _LCD_DATA_STRING
#define _LCD_FIRST_ROW
#define _LCD_SECOND_ROW

#include"lcd.h"

and you have also added the .c library to the project?
 

Yes For Sure i have done all these
Capture.png

Have a look on the above snap
 

Can you copy/paste the error log or post a snapshot
 

This is the error log that i am getting

Code:
Clean: Deleting intermediary and output files.
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\lcd.p1".
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\main_prog.p1".
Clean Warning: File "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.cof" doesn't exist.
Clean Warning: File "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.hex" doesn't exist.
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.sym".
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.map".
Clean Warning: File "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.hxl" doesn't exist.
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\startup.lst".
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\startup.rlf".
Clean Warning: File "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\doprnt.p1" doesn't exist.
Clean Warning: File "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\doprnt.pre" doesn't exist.
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.obj".
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.lst".
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.rlf".
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.sdb".
Clean: Deleted file "C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd.mcs".
Clean: Done.
Build C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\test_program_lcd for device 16F877A
Using driver C:\Program Files\HI-TECH Software\PICC\9.81\bin\picc.exe

Executing: "C:\Program Files\HI-TECH Software\PICC\9.81\bin\picc.exe" --pass1 C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\lcd.c -q --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s" 
Executing: "C:\Program Files\HI-TECH Software\PICC\9.81\bin\picc.exe" --pass1 C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\main_prog.c -q --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s" 
Warning [361] C:\Users\SHARMA\Documents\MPLAB_Projects\LCD_Header_File_Test\main_prog.c; 12.1 function declared implicit int
Executing: "C:\Program Files\HI-TECH Software\PICC\9.81\bin\picc.exe" -otest_program_lcd.cof -mtest_program_lcd.map --summary=default --output=default lcd.p1 main_prog.p1 --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s" 
HI-TECH C Compiler for PIC10/12/16 MCUs (Lite Mode)  V9.81
Copyright (C) 2010 Microchip Technology Inc.
(1273) Omniscient Code Generation not available in Lite mode (warning)
Error   [500] ; 0. undefined symbols:
	_lcd_init(test_program_lcd.obj) _lcd_clear(test_program_lcd.obj) ___delay_ms(test_program_lcd.obj) 

********** Build failed! **********


Capture.png
 

Don't re-invent the wheel!
If you compile your LCD functions into a library, and then include your h file in your program, the linker will only link in the code that is called from the program.
I assume you are using Hi-Tech Compiler, you need the command line to make a library.
Read the manual on how to do it.
 

Means I will not be able to make my own header file without using the Command Line Option

:-(

Anyways Thanks for your information....


And yes i am using Hi-Tech C Compiler



Regards
Arun Sharma
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top