Question

In many languages, such as C++, having lots of different source files is normal, but it doesn't seem like this is the case very often with PIC microcontroller programs -- at least not with any of the tutorials or books I've read.

I'm wondering how I can have a source (.c) file with a bunch of routines, global variables and defines in it that can be used by my main.c file. Is this even possible?

Thanks for your advice!

Was it helpful?

Solution

This is absolutely possible with PIC development. Size is certainly a concern both from a code and data perspective but it's still just C code meaning most (see compiler documentation for exceptions) of the rules of C apply including having multiple source files that get compiled and linked into a single output (usually .hex file). For example, in a separate C file from your main.c like test.c:

int AddNumbers(int a, int b)
{
  return a + b;
}

You could then define that in a header file test.h:

int AddNumbers(int a, int b);

Include test.h at the top of your main.c file:

#include "test.h"

You should then be able to call AddNumbers(4,5) from main.c. I have not tested this code but provide it simply as an example of the process.

OTHER TIPS

Typically, most code for PIC18 is included from other files. so rather than the high level techniques of compile then link, it is more common to include (and include from includes) all of the code so that there is a single stream going to the compiler. I think you can do it under PIC18, but I never spent enough time to get it to work. Most of the libraries and such are designed as include file, rather than as separately translated units.

It's a different mindset, but there is a reason. I think this is due to the historic need to keep things as small as possible. Therfore, things are done with MUCH more macros based on the chip, and much less (linkable) library development.

PIC32 compiler is much better for its library support.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top