Lab Exercise 18: Enumerations

Last modified by Microchip on 2023/11/09 09:21


This demo illustrates the use of enumerations to create a list of constant labels that may be used in conjunction with variables declared with the enum’s type. The primary purpose of enumerations is to make your code more readable and easier to maintain.

Reference Materials

Exercise Files


Open the Project

Start MPLAB® X IDE, then click on the Open Project Open Main Project button icon on the main toolbar.
Navigate to the folder where you saved the exercise files for this class.
Click on the Lab18.X folder.
Select Open Project Open Project Button.

Debug Project

Click on the Debug Project Debug Main Project button button. This will build and send the program to the simulator.
Click on the Continue Debug Continue button button. This begins the simulation. Wait for the UART 1 Output window to finish printing.
Click on the Halt Debug Pause button button.


Lab18 Results in UART 1 Output Window

After running the program, the UART1 Output window should display the text: “Bandpass filter selected.

Code Analysis

Line 13
An enumeration data type called filterTypes is defined. This line essentially defines the following constants:
declared to be of type filterTypes.

Line 18
This line declares a variable called filter of type filterTypes. Some compilers will restrict this variable to holding just the values defined by the enumeration, though this isn’t always the case. At the very least, it will make your code easier to read and maintain by associating a group of constants with a particular variable.

Line 34
Here, we initialized the variable filter to BANDPASS. This effectively assigns a value of 3 to the variable, which may be used as any ordinary integer variable.

Line 36-42
These lines simply call a particular function depending on the value of the variable filter. Notice that in every place that a constant would be used, we have used one of the labels defined in the enumeration type declaration. The functions themselves are defined in the file Utilities.c, and all they do is print out which filter was selected for the SIM UART 1 window.

End Debug Session

End the Simulation Session by clicking the Finish Debugger Session Finish Debugger Session button button.
Clear out the UART 1 Output window by entering (Ctrl + L).
Close the project.


Enumerations provide a means to associate a list of constants with one or more variables. Theoretically, these constants represent the entire range of valid values for the variable. Some compilers will enforce this range (compile time only - runtime checking is up to you). However, even if the compiler doesn’t do any checking, by sticking to the list of valid constants it becomes more difficult to accidentally assign an invalid value.

Perhaps the greatest benefit of using enumerations is that they make your code more readable by replacing “magic numbers” and that it becomes much easier to maintain. If you need to add additional valid values, you need only add them to the enum list. If the values of some of the labels change, you may not have to change the rest of your code to reflect these changes (depending on how you have written your code).