Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to spread my code around multiple files with MPLAB/HI TECH C

Status
Not open for further replies.

ItsMike

New Member
Hello everyone,

I'm having hard time figuring out how to split my code into 2 files.
I currently have 2 files in the project: Test.c and Font.c, Font.c contains a lot of const variables and couple of pointer arrays.
I want to be able to read the data in Font.c from the Test.c file without re-declaring everything as extern.

How can I do that ?
 
Generally, what you would do is have a .h file that declares the stuff in the font.c file that you want to view from other files. Most .c files will have a sister .h file associated with it in general. You then include the header file in your c file.

Why are you opposed to declaring it as extern?
 
I wanted to separate the two files so that font.c would have all the data and functions to handle the font drawing, it has an array for each char in the font and a pointer array.

If i'd declare all these arrays as extern they will take a lot of space (visually).

Anyways, if i'll rename the file to .h can I just #include it ?
 
You can do that, but I consider it to be bad programming practice. Declaring everything as external will not take up a lot of visual space if you do it in a header file. Keep your font.c file the way it is. Make a font.h file and include the following lines of code in it (modified of course to match your array, variable, and function names)

Code:
// file:  font.h
extern char array_a[5];
extern char array_b[5];
extern char array_c[5];
...
extern void Print_Char( char* ch );
extern void Print_String( char* pStr );
// and so on...

All of that goes in your header file. At the top of your c file, you just add #include "font.h". Visually, it only takes up one line.
 
Last edited:
I'm missing something here :confused:
This is a summery of my code:
Code:
//file: font.h
extern const unsigned char space  					// Code for char  
extern const unsigned char exclamation_mark[8]  // Code for char !
extern const unsigned char double_quotes[14]		// Code for char "

Code:
//file: font.c
const unsigned char space= 0x09;  // Code for char  
const unsigned char exclamation_mark[] = {0x04, 0x00, 0x00, 0x00, 0x04, 0xFC, 0x0E, 0x7E, 0x04};  // Code for char !
const unsigned char double_quotes[]={0x07, 0x80, 0x01, 0xF0, 0x00, 0x38, 0x00, 0x00, 0x00, 0x80, 0x01, 0xF0, 0x00, 0x38}; // Code for char "
........

Code:
//file: test.c
#include	<htc.h>
#include <font.h>
__CONFIG(HS & WDTDIS & BORDIS & LVPEN & DUNPROT & WRTEN & DEBUGDIS & UNPROTECT);
#define _XTAL_FREQ 20000000
....

Code:
Executing: "C:\Program Files\HI-TECH Software\PICC\9.80\bin\picc.exe" --pass1 "D:\My Documents\Electronics\Projects\POV\Code\Test.c" -q --chip=16F877A -P --runtime=default,+clear,+init,-keep,+osccal,-download,-resetbits,-stackcall,+clib --opt=default,+asm,-debug,+speed,-space,9 --warn=0 -D__DEBUG=1 --double=24 --float=24 --addrqual=ignore -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s" 
Error   [141] D:\My Documents\Electronics\Projects\POV\Code\Test.c; 2.17 can't open include file "font.h": No such file or directory

font.h is at the same dir ("Code") as font.c and test.c

EDIT:
WOW, I just noticed i did an #include <font.h>, instead of #include "font.h".
No compilation errors now.

Thanks a lot.
 
Last edited:
You can probably just do;
#include "font.c"

The #include directive just tells the preprocessor to insert the contents of that file in that location BEFORE compiling.
 
Right !
That what I was trying to do at the first place but forgot the syntax with #include "font.c", and i tried it with <font.c>.
 
You're also missing the semicolons at the end of each line in the .h file. Glad to hear you have it working though, whichever route you went.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top