multi-file project question

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi
im working on a project that uses an lcd module. i wrote the code in a separate file from main() so that i could easily use it for other projects. the problem is that i want to be able to define what ports and pins it uses for various signals without having to change the actual code each time i change pin configs. i tried using #define in the main() file but got errors (found out later that macros only have file wide scope) after i figured that out i tried moving my macros to the lcd file. the code looks something like

#define LCD_DATA PORTB//define lcd control and data lines

the original errors went away but now i get multiple errors saying

'PORTB' undeclared (first use in function)

im guessing that the preprocessor only replaces LCD_DATA with PORTB and then doesnt take it any further. does anyone know of a right way to do what im trying to do?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nope, the preprocessor replaces all macros. Are you actually
sure you've got included everywhere?

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

twidget,

In most of my multi .c file projects where each file provides functions for a specific task (xmodem.c, dataflash.c, etc.) I have a .h file for each (xmodem.h, dataflash.h, ...) that provides the prototypes for the functions offered by that module along with enums and typedefs that they use. Such modeul specific .h files can then be included by another module if it want's to access any of those functions. But I also have a system wide .h that I include in ALL the .c files and this contains the common stuff that needs to visible to all the C files. If I had some #defines that several of the .c files might want to use they'd go in this system wide .h (I often call it sys.h or syshdr.h)

Also, if I have some global variables I tend to put the actual definitions in a global.c file and then have a global.h that declares them all as 'extern' and this can then be included in any other module's .c file that needs to access one or more of those variables.

Cliff

PS my sys.h includes (amongst other things) the "#include " so I can be sure that all the C files can see the standard SFR definitions.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ive got avr/io.h included in the main.c file. i thought you were only supposed to #include header files once. ill check if that fixes things when i get home.
if i put it in a library, wont i be stuck changing the library file each time i use a different pin configuration ( ie i use port a instead of port b)? im trying to go for flexibility and also i dont want 5 versions of the same library floating around my computer. i suppose i could standardize but when im laying out my boards i tend to swap pins around so that the board can route easier.

::edit:: that seems to have done it but the first time i built it i got a popup that said "i/o error 103" i hit build again and it didnt show up. is it a random bug or did i do something else wrong?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note, that you can include a file in a .h file, and it should be ok to include it more than once. The header files in avr-libc are written in such a way that you can include them more than once and you won't get multiple definitions or errors. So what I would suggest is in your lcd.h file is to add #include up at the top so that way your statements such as, #define LCD_DATA PORTB, will work as expected.

FYI, the word idempotent is used to describe header files that written in such a way that they can be included multiple times without errors. Here is a definition of the term:
http://www.catb.org/~esr/jargon/...