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.

Multiple files structure for C in Microcontroller

Status
Not open for further replies.

eepty

Full Member level 2
Joined
Oct 21, 2005
Messages
143
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
2,611
Hi all, I have the experience to program C for a microcontroller, but it is in a single file, that means one .c source file and one .h header file. However I know that we can use multiple c file and header file, and compile and build to a single .hex file, how can I do that?

I am using the MPLAB IDE to programe the PIC18F.

Thanks for your help.
 

U can include another .c file in ur main .c file. but u have to take care about the variables defined in ur .c files(have to define as extern).
Ex.
ur main file is main.c & the other file is lcd.c. then ur syntax will be
#include <main.h>
#include "lcd.h"
 

Hi friend

To use multiple files structure for c is very easy.
First of all, write multiple C files.

then, if you are use MPLAB
then go to project window and right click on source files.
select Add files, and add your all C file here.

Take care about Global variable.
If you define Global variable in one C file(like int Value = 100) and if you want to use that variable in other c file
you muse redefine that variable with extern keyword(like extern int Value).

Hope this is help you
Shyam
 

Use Project features in IDEs they will definitely help you.
Else What you can do is
1) write a.c and a.h b.c and b.h etc...
2) take care to put the # if defined __A_H_ and end the file with #endif
3) include a.h,b.h file in your main.c
4) compile all files separately
5) link all the .obj files

you can check any standard C book for how to write headerfile or google it.

Hope it helps
 

Hi friend

To use multiple files structure for c is very easy.
First of all, write multiple C files.

then, if you are use MPLAB
then go to project window and right click on source files.
select Add files, and add your all C file here.

Take care about Global variable.
If you define Global variable in one C file(like int Value = 100) and if you want to use that variable in other c file
you muse redefine that variable with extern keyword(like extern int Value).

Hope this is help you
Shyam

Does that mean that if I added the .c source file in the MPLAB project, then I do not need to include it in the main.c?
 

After many trials, I found that if I included the .h header file in main.c, I do not need to declare the variable again with or without extern, for example:

in main.c:
include "ADC.h"
void main(void){ }

in ADC.c:
int i = 0;

in ADC.h:
extern int i;

Is it correct?
 

After many trials, I found that if I included the .h header file in main.c, I do not need to declare the variable again with or without extern,

right, But how try to enclose with a some #defines to protect multiple inclusion.
 

Hi Friend

After many trials, I found that if I included the .h header file in main.c, I do not need to declare the variable again with or without extern, for example:

in main.c:
include "ADC.h"
void main(void){ }

in ADC.c:
int i = 0;

in ADC.h:
extern int i;

Is it correct?

Yes friend you are right.
By this way you can add as many C source file as you wish.

Best Luck
Shyam
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top