Advice needed: splitting a project into modules

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
nigelmercier
Posts: 316
Joined: 23 Oct 2008 09:36

Advice needed: splitting a project into modules

#1 Post by nigelmercier » 05 Jan 2010 15:54

Further to this post I'm still having problems trying to split my first project into separate .C modules.

As I understand it, using #include has the effect of loading that file into to code at that point, whereas modules are compiled separately, is that right?

Below are the definitions and prototypes as at present. I want to separate this code (plus the associated function definitions) into 4 modules. Turning the included file [1] into a .C file, and each of [2], [3] and [4]. Note that there are function calls from one section to another: for example main() makes a call to tCal() in [2], and tCal() makes a call to testBtn() in [3].

Everything I have tried causes dozens of errors, can anyone tell me what needs to go where?

Code: Select all

// mikroElektronica Libraries used: ADC, CONVERSIONS, EEPROM

#include "nokia3310.h" // I want this to be a separate .C file [1]
#include <built_in.h>

typedef unsigned short int byte;

#define ModeControl PORTB        // Step = b0-b2, control = b3, Btn = b4
#define TCal_Ena ModeControl.F3  // Go to Tank Cal if b3=0
#define Btn_Key ModeControl.F4   // Button pressed if b4=0
#define FuelADC 0                // use ADC 0 for fuel sensor

#define BufSize 128              // Size of array for EEPROM storage
#define MaxEntries (BufSize - 8) // maximum number of entries in table
#define MaxStore (BufSize - 3)   // EE address: max # data tabMax
#define StepStore (BufSize - 2)  // EE address: step switch data gaugeStep
#define ChkStore (BufSize - 1)   // EE address: checksum of table

// Global variables

byte volatile fuelLevel; // fuel level from ADC made 8 bit
byte volatile gaugeStep; // fuel step value, saved in EE StepStore
byte fuelCalTab[BufSize]; // lookup table for fuel sensor calibration
byte tabMax;              // index of last entry in table, saved in EE MaxStore

char txt4[4], txt7[7];    // temporary variables to hold conversion strings
byte i, j;                  // misc loops

// Tank Cal Function Prototypes [2]

void tCal(void); // Calibrate Tank Gauge Step value and calibration points
void tCalModeMsg(void); // CLS, LCD line 0 = "TANK CAL SET"
void tCalBootMsg(void); // LCD line 1 = "REBOOT TO EXIT"
void tCalBtnMsg(void);  // LCD line 5 = "CONTINUE BTN ?"
void tCal3Msg(void);    // All 3 messages above to LCD
void tCalStMsg(void);   // LCD line 4 = "STORING DATA", 1 S delay, clear line

// IO Function Prototypes [3]

byte getFuel(void); // Get ADC for fuel level as 0-255
byte testBtn(void); // Return 1 if button pressed, 100ms delay x 2

// EEPROM Function Prototypes [4]

void loadEEPROM(void); // EEPROM data GSE: fuelCalTab[], tabMax, gaugeStep
void writeEE(byte addr, byte val);// write EEPROM address with val, delay 20mS
byte readEE(byte addr);           // return value at EEPROM address, delay 20mS

// Tank Cal Function Definitions

// [code snipped]

/*-----------------------------------------
                Main Program
-----------------------------------------*/
void main( void )
{
// [code snipped]
}
[/url]

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#2 Post by drdoug » 05 Jan 2010 16:12

I need to do this kind of thing myself but I can not give any definite answers. Look at the 7 segment example from mE. I think you will find what you need there.

nigelmercier
Posts: 316
Joined: 23 Oct 2008 09:36

#3 Post by nigelmercier » 05 Jan 2010 20:32

drdoug wrote:I need to do this kind of thing myself but I can not give any definite answers. Look at the 7 segment example from mE. I think you will find what you need there.
As far as I can see, the 7 segment example just uses an #include.

Anyone else?

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#4 Post by drdoug » 06 Jan 2010 03:41

I think the include statement gives the compiler a place to look for the information and only those functions that are needed are compiled into the program. If a function is not needed, it will not be included. I do not know what you are referring to about modules.

As illustrated in the 7 segment project, there is a header file which contains the prototype that is found in the .c file.

I assume you have the actual functions somewhere and not just the prototypes listed.

I would start with a much smaller example to get things working first and then expand on that success.

Perhaps you are talking about creating Libraries and this thread may help http://www.mikroe.com/forum/viewtopic.p ... =libraries

zdavesf
Posts: 43
Joined: 10 May 2007 03:52

#5 Post by zdavesf » 06 Jan 2010 05:17

make sure the .c or .h file is in your project folder (same as your main_source.c file). If it is not in the same destination then go to project manager and under header files (if .h) or sources(if .c) and add the file.

do not declare your function prototypes from your header files in the main source.

can you post code of your header file?

I just went through this a couple weeks ago because my code was getting too long to read...

Dave
PICFlash2, PIC16f877a, 18F4550, PIC16F883, PIC16F72. Electronics Tech trying hard to be a programmer...

nigelmercier
Posts: 316
Joined: 23 Oct 2008 09:36

#6 Post by nigelmercier » 06 Jan 2010 09:51

zdavesf wrote:make sure the .c or .h file is in your project folder (same as your main_source.c file). If it is not in the same destination then go to project manager and under header files (if .h) or sources(if .c) and add the file.
Hi Dave,

The procedure I followed was:

[1] Create new blank file in project folder, and add to project
[2] Cut related function prototypes and functions from main file
[3] Paste these into new file
[4] Compile, and watch the errors scroll past :(

zdavesf
Posts: 43
Joined: 10 May 2007 03:52

#7 Post by zdavesf » 06 Jan 2010 10:25

can you post a screen shot of your errors?

please pardon my lack of "C" knowledge but a function prototype would be something like
void function_proto();
outside of main correct?
at least for me any function that is in the "included file" does not have any function prototype ANYWHERE

here is what i have been doing and it has been working...
i do not "ADD to project" any files i make sure the include file is in the path as my project

here is and example of a main source simplified (same name as project)
(this is not tested so it may not compile, just going off the top of my head)

Code: Select all

//blinks led on port c0 every 500ms

#include "set_bits.c"

#define LED1 PORTC.F0
char x;

void DELAY(); //function prototype???  only declared for functions in main code

void main() {

     CONFIG_BITS;  // call function from include file
     x=1:
     while(x=1) {
     
     LED1=~LED1;   //flip bit
     DELAY();     //call function in main source
    } end while loop

void DELAY() {

   delay_ms(250);
}// end delay

and then the include file would be named "set_bits.c" and look something like this.

Code: Select all


void CONFIG_BITS() {
     
     TRISC=0x00;
     LATC=0x00;

     ADCON1 |= 0x0F;     // Configure all ports with analog function as digital
     ADCON0 |= 0x00;
     ADCON2 |= 0x00;
     CMCON  |= 7;        // Disable comparators
}//end config bits

I hope that helps give you an idea of whats I am doing at least, like i said i am a beginner so i may not be doing it proper but it is working for me.
PICFlash2, PIC16f877a, 18F4550, PIC16F883, PIC16F72. Electronics Tech trying hard to be a programmer...

nigelmercier
Posts: 316
Joined: 23 Oct 2008 09:36

#8 Post by nigelmercier » 06 Jan 2010 10:38

zdavesf wrote:like i said i am a beginner so i may not be doing it proper
Thanks, but I need to make sure I am "doing it proper", hence my question.

zdavesf
Posts: 43
Joined: 10 May 2007 03:52

#9 Post by zdavesf » 06 Jan 2010 10:46

like i said i dont know if i am doing it right or not. but i've been doing it like that for a while. i only say i dont know if its proper because i have no one to tell me otherwise :) hope it helps. if you getting it working please inform us so we all can do it right

Dave
PICFlash2, PIC16f877a, 18F4550, PIC16F883, PIC16F72. Electronics Tech trying hard to be a programmer...

Post Reply

Return to “mikroC PRO for PIC General”