Skip to content

Modules

  • C programs are structured with the help of modules.
  • Each C module must have a header file (*.h). The header file is used to include the module.
  • The source file (.c) and contains the source code of the module's functions.
  • The following table shows the code separation between the source and the header file.
Content Source File example.c Header File example.h
Header
// Used libraries
#include <msp430.h>
#include <stdint.h>

// Own header file
#include "example.h"
// Include guard
#ifndef EXAMPLE_H_
#define EXAMPLE_H_
Global Variables
// Global variables
const uint8_t example_list[] = { 1, 2, 3 };
uint8_t example_number = 3;
// Declaration of the global variables
extern const uint8_t example_list[];
extern uint8_t example_number;
Local variables
// Local variables
static uint8_t counter;
static uint8_t *pointer;
---
Prototypes of Local Functions
// Prototypes
static void set_output(uint8_t pattern);
---
Global Functions
// Global functions
void example_init(uint8_t x) {
  P1DIR = 0xFF;
  P1OUT = 0x00;
}

uint8_t example_show() {
  set_output(*pointer);
  return 1;
}
// Prototypes of the global functions
void example_init(uint8_t x);
uint8_t example_show();
Local Functions
// Local Functions
static void set_output(uint8_t pattern) {
  P1OUT = pattern;
}
---
  ---
#endif /* EXAMPLE_H_ */
  • The so-called include guard ensures that the header file is only parsed once by the compiler. The name of the include guard refers to the module name. The format NAME_H_ is typical.
  • All includes are typically made inside the source file. Include the own header file last.
  • All global objects of the module needs to be declared inside the header file. To prevent naming collisions all global object should start with the module name plus underscore. Snake casing is typical for all C variables and functions.
  • For global variables the declaration is done inside the header file by adding the keyword extern. No values are added to the declaration.
  • All local C objects are defined by using the keyword static. Local variables appear only inside the source file and can only be accessed by this module. That is why no special naming convention is needed.
  • The prototypes of the local functions are added on top of the source code. The prototypes of the global functions are added to the header file.
  • The source code of both global and local functions are added to the source code.
  • The closing #endif of the include guard should be the last statement in the header file.