PIC18F4520: How to create a header file?

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

PIC18F4520: How to create a header file?

Postby tst » Wed Jan 05, 2011 8:47 pm

Hi,

There are too many function in my main program. So I would like to simplify it by using header file. Does anyone know how to create a header file?
Thanks..
tst
Apprentice
 
Posts: 33
Joined: Sat Sep 18, 2010 9:41 pm

Re: PIC18F4520: How to create a header file?

Postby temporary40 » Thu Jan 06, 2011 11:04 pm

To create header, you will need to create a H file and another corresponding C file.

1) From your main C file (where you put the main() function), include the H file.
For example
____________________________________________
|#include <function.h>
|int main()
|{
| //your main program here...
|}
|___________________________________________

2) Then in the function.h, you will need to have your function prototype. Remember to add the preprocessor (#ifndef, #define, #endif) so your compiler will compile it once only.
For example...
____________________________________________
|#ifndef FUNCTION
|#define FUNCTION
|int myfunc();
|int myfunc2();
|int myfunc3();
|
|#endif
|___________________________________________

3) Then, in the corresponding c file (function.c), include the the function.h and write the subroutine function (function definition).
____________________________________________
|int myfunc()
|{
| //code here
|}
|int myfunc2()
|{
| //code here
|}
|int myfunc3()
|{
| //code here
|}
|___________________________________________

4) You are allowed to create global variables in the function.c file
____________________________________________
|int value, value1 = 100, value2 = 200;
|___________________________________________

5) But if you want the variables to be accessed in other file (for example the main function), you need to 'extern' the variables in the function.h file
____________________________________________
|extern int value, value1, value2
|___________________________________________

This is basically sums it up...
temporary40
Novice
 
Posts: 23
Joined: Thu Apr 01, 2010 9:30 pm

Re: PIC18F4520: How to create a header file?

Postby tst » Fri Jan 07, 2011 10:03 pm

Thanks. I try 1st.
tst
Apprentice
 
Posts: 33
Joined: Sat Sep 18, 2010 9:41 pm

Re: PIC18F4520: How to create a header file?

Postby tst » Sat Jan 08, 2011 12:38 pm

hi, there are some error after create the header file. I had follow your instruction.

Error [1020] unexpected input following '#endif'
tst
Apprentice
 
Posts: 33
Joined: Sat Sep 18, 2010 9:41 pm

Re: PIC18F4520: How to create a header file?

Postby temporary40 » Sun Jan 09, 2011 1:26 am

Are you sure your spelling is correct?

In front of #endif preprocessor need to have #if or #ifndef
temporary40
Novice
 
Posts: 23
Joined: Thu Apr 01, 2010 9:30 pm


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 6 guests

cron