©2019 Robotics for COMPLETE Beginners

17

Header Files

 

In addition to knowing the basics of programming your chip in the C language you should understand the importance and how to use header files

 

Don't worry, this will be very easy!

A header file is simply a file used for reference that you include at the top of your C program like you just learned about.

These header files contain functions or references to the names of those functions which are fully fleshed out in a separate C file or may contain pin assignments and their nicknames, etc.

The header file name looks like this:

        superLaser.h

It is saved in a folder with the name of your program.

There is often a C file under the same name as the header file in that same file, it looks like this:

        superLaser.C

Again, the C file has the meat of the functions.

The h file has the names of those files.

You then include only the header file at the top of your program like this:

         #include "superLaser.h"

Notice the quotations instead of the carats < >.

If you don't understand this chain read it one more time.

Why do we do this?

To make your program much easier to write, read and understand and avoid errors which come along with complex program writing.

Libraries are very similar and may contain something like a list of written out frequencies associated with musical notes: ex. do rae mi fa sol... or A, A#, B, C, C#, etc.

Basically, all these files do is allow you to write them once and include them very quickly and easily in any program you like.

See the following examples.

Here you can see the author info and public license info at the top which is always neat.

Then you can see they actually include another header file <inttypes.h>

After that they define the names they made up for for different ports and pins such as LDP, LCP, etc.

Right next to those names they made up there is a space and the actual PORT names and pin numbers.

Take a minute to read the kind notes that the author left for us which start with "//".

Keep in mind the term 'MCU' does not stand for Marvel Cinematic Universe but Microcontroller.

Ok.  So these are the lighter terms outlined. Let's take a look at the C file:

Here you can see the function of sending characters to an LCD screen.

The function type is "void" since it doesn't return any values, it merely performs its job.

The author chose to name the function "LCDsendChar" and the parameters are an 8 bit, aka 1 byte character which is entered in C language as "uint8_t" and "ch".

The function opens with a bracket "{" and uses the labels outlined in the .h file.

The |= and the &= are the OR and AND operators which help to flip bits on and off which can be seen in the "(1 << xxxx)" included on each line.

These are just turning ON and OFF certain registers and pins etc. Don't overcomplicate it.

What these stand for are outlined in the DATASHEET which you will learn how to use later on.

Trust me...you don't want that in your life right now...

No need to be an expert yet. Just be familiar with header files and libraries.

To simplify, they are prewritten functions and labels which you can copy from someone else or make your own.

They are like reference books or recipes for a feast which is your program.

Always, always, always relate these complicated concepts to simple everyday life concepts.

It will help you learn much much faster and save you frustration.

Header files with the .h extension and the .c extension should be in the same general path or folder that your program is being saved to.

Let's say you make a folder named "Projects" then inside you make a folder named "C Programs".

Inside your "C Programs" folder you would have folders for each project with their own unique name like "LED_Blink" which contains the .c file etc. for that specific project.

The header and the c file for the header file (if it requires one) need to be included in the "Projects" or "C Programs" folder.

The compiler will be able to find you header files in this path this way.  If your C program cannot find the header file the program you use to compile like Notepad will throw an error at the bottom saying so.

All you have to do is find the header file and its C file and move it where it should be so it is finadable.

Header files will simplify your life and you will start to understand them clearly once you get rolling with projects soon.

Remember, most of this will be learned through doing.

There is only so much you can learn through reading.

Pop Quiz

1. What would you expect to find within a file with the ".h" extension?

2. What would you expect to find within a ".c" extension?

3. Do the .c and .h parts of the header file need to have the same name?

4.  Why are header files so useful?

5.  How do you include them in a program?

Get my favorite books on programming AVR chips I use.

Equip yourself with the most dependable, no hassle, quality usb programmer I use daily.

Make sure you have a basic kit. Arduino is fine, it contains the removable AVR 328p chip we will use.

 

I started with something very similar.

Summary

Header files will simplify your programming experience and allow you to amass a library of "at the ready" functions you may use in other programs.

One of the greatest things you can do after working so hard to set up a set of working functions or mini programs is to archive it and have it available for later so that you don't need to reinvent to wheel each time.

Remember, header files and C files that go along with them must be in the same folder path in order to work properly.

They must be in the same "library" as your other programs (books).

You may not need to implement them right now but header files will come in handy in the near future and now you understand why we include them at the top of programs!