Modular Programming.

Once a beginner embedded developer has had enough of blinking LEDs, he will certainly decide to write something more serious, and like any beginner he will have only one wish: "to get it all working as fast as possible!!!". In this attempt to prove himself, he will write everything in one file without thinking about the program's structure, but after a while, when part of the plan has been implemented, it will become clear that the bigger the program gets, the harder it is to find anything in it. This will lead him to the idea that a program should have a structure, and he will go searching the internet to see how other embedded developers solve this problem. After looking at programs written by other people, he concludes that a program is split into files that are complete logical units, that some functions and variables are moved into a header, and much more. All of the above is my own experience, but all beginners go down the same path, so here I'll describe what I ran into myself.

Suppose we have a program that displays the temperature on an LCD display, and both the LCD display and the temperature sensor (ds18b20) need initialization as well as functions to work with them. So it makes sense to create two separate files, lcd.c and ds18b20.c, which will contain the functions needed to work with them. Such files are called modules; I'd like to note that each module is an independent, logically complete unit that can be compiled separately from the rest of the program. When compiling a module, the compiler turns it into an object file.

The next question that arises: since a module is an independent structure, a "thing in itself", so to speak, it has no connection with the outside world, and that doesn't suit us. To connect modules with the outside world, header files are used, which have the .h extension. You can name a header anything you like, but it's more convenient for its name to match the module name, lcd.h and ds18b20.h. I should also say that it's convenient to move all included files (#include) into the header and include only the header at the beginning of the module.
Before there was a header, the beginning of lcd.c looked like this

#define F_CPU 8000000UL
#include <util/delay.h>
#include <avr/io.h>

and after creating the header it looked like this
#include <lcd.h>


But then another question immediately arises: what should go into the header?
The header should contain the prototypes of functions that may be needed in other modules of the program. A function prototype only declares a function and contains no function body, but by looking at it you can find out the function's name, the number and types of its arguments and its return type.
The following prototypes will be declared in lcd.h:

void lcd_init(void);
void lcd_write_symbol(char symbol);
void lcd_write_string(char *str);
void lcd_clear(void);

The following prototypes will be declared in ds18b20.h:

void ds18b20_init(void);
uint8_t ds18b20_get_temperature(void);



As for macros, you can move into the header the macros responsible for conditional compilation

#define MAKE_CALIBRATION   //uncomment for calibration

And somewhere in the code there is a construct that is executed if the previous line is uncommented
#ifdef  MAKE_CALIBRATION 
		touch_x -= 300;
		touch_x = 240 - touch_x/((Xmax-Xmin)/240);
		touch_y -= 350;
		touch_y = 320 - touch_y/((Ymax-Ymin)/320);  
#endif

You can also move into the header the macros that select the pins the peripherals will be connected to

#define D0 PORTA //data is transmitted over a 16-bit bus,
#define D7 PORTD //so we use two ports for it



But at the same time, you shouldn't put into the header anything that won't be needed in other modules:
  • macros like
    #define (LCD_PIN & 0X80) check_busy_flag

  • variables with the static keyword that will only be used inside the module
  • variables with the extern qualifier
  • prototypes of functions needed for some intermediate actions, for example a function that converts a number to BCD format


Now a couple of words about including headers. When programming AVR microcontrollers, almost every module includes the header for working with the I/O ports.
#include avr/io.h

That is, it is included in lcd.h and in ds18b20.h; now if we include these two headers in the main program file, avr/io.h will be included twice, although once would have been enough. To avoid this situation and prevent the header from being included twice, #include guards are used, which look like this.
#ifndef _ENCODER_H_
#define _ENCODER_H_

// write it like a regular header

#endif

This construct lets the preprocessor determine that this header has already been included in the program and not include it again. You can read more about this here.
You can also limit the number of inclusions of a file to one with the construct

#pragma once
// write it like a regular header

You can read about the advantages of using #pragma once instead of #include guards here.
By the way, you can include not only headers but also .c files if needed
#include “font.c”

In this case, a font file is included for displaying letters on a TFT display.
That's all; I think this is the minimum every beginner microcontroller programmer needs to know.