Calling functions from other documents

In MPLAB, how do I call functions from other files in a project?

Here is my code:


static int counter;

counter = Get_Encoder_3_Count();

And the Get_Encoder_3_Count() function is located in the file encoder.c

Here is the function code:

long Get_Encoder_3_Count(void)
{
	long count;

	INTCONbits.RBIE = 0;

	count = Encoder_3_Count;
	
	INTCONbits.RBIE = 1;

	return(count);
}

The error that I get is:

Error - could not find definition of symbol ‘Get_Encoder_3_Count’ in file ‘V:\frc-code-2007-8722\FrcCode_2007_8722\user_routines.o’.

declare the function reference as external to your source file.


extern long Get_Encoder_3_Count(void);

are you #include’ing?

I have the both the files encoder.c and encoder.h included. I added the external identifier, but I still get the same error.

Here is my new code:


static int counter;
extern long Get_Encoder_3_Count(void);

counter = Get_Encoder_3_Count();

If you’re using more than one function from a file, you could just include that file’s header file. Use quotes if it’s not a library that comes with the compiler.

#include “myfile.h”

I Have the code:

#include “encoder.h”
#include “encoder.c”

located at the top of the document

It sounds like you haven’t set up the encoder stuff correctly. Not all the encoder code is enabled by default. If that’s the case then that routine isn’t being compiled - its being excluded because ENABLE_ENCODER_3 isn’t defined.

There is an encoder_readme.txt file in the documentation directory and there is info in the encoder.h file. Is the ENABLE_ENCODER_3 line in .h un-commented?

You don’t need to include “encoder.c”, and in fact, you probably shouldn’t.

I don’t think that’s the problem, but if you include a .c file, you’re probably going to get an error about something like multiple declarations of something.

The headers tells the file that a function exists somewhere in one of the .c files. The actual code for that function doesn’t need to be in the same .c file the function is used in.

Take a few steps back. Undo everything you’ve tried to do in order to make it work. Read the documentation (I think it’s called encoder_readme.txt) that comes with the encoder software. Follow the steps (nine of them, if I recall correctly) to add the code to your project and configure it.

If it still doesn’t work, let us know.

The problem could be that you tried to return a ‘long’ (4 bytes) variable into an ‘int’ (2 bytes) holder. Let me know if that helps any.