Objective
The purpose of this lab is to illustrate how variables are declared, and how the data type of a variable affects the way it is stored in memory. It also illustrates how to view the variables within MPLAB® X IDE, both in their high-level context as well as machine-level context. Upon completion of this exercise, you will understand how to view C-level variables in MPLAB X IDE and how they are stored in memory based on their data type.
Note, that while we will be using the MPLAB XC16 compiler for these exercises, and that data memory is 16-bits wide, the fundamentals remain the same when used with one of the 8-bit compilers such as MPLAB XC8. Only the size of some variables and the width of data memory in the display windows will change.
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
MPLAB® X
Integrated Development Environment |
| | | | |
MPLAB® XC16
C Compiler |
| | | | |
Exercise Files
File | Download |
Installation
Instructions |
||
---|---|---|---|---|
Windows | Linux | Mac OSX | ||
Project and Source Files
|
| | | |
Procedure
1
Open the Project
Start MPLAB X IDE, then click on the Open Project icon on the main toolbarNavigate to the folder where you saved the exercise files for this class.
Click on the Lab01.X folder.
Select Open Project .2
Debug Project
Click on the Debug Project button. This will build and send the program to the simulator to begin the simulation.Wait for the UART1 Output window to finish printing.
Click on the Halt button. This will stop execution so that we may examine the variables and their values.3
What just happened?
We took a pre-configured MPLAB X IDE project, which included a complete C program along with the configuration settings for the tools, and compiled the code contained in the project. After compiling the code, we ran it in the simulator that is built into MPLAB X IDE. The simulator is capable of reproducing almost all of the functions of a PIC® microcontroller. The code itself doesn't do very much. We simply create and initialize a set of six variables. We then print out the size of these variables to the UART 1 Output window by using the printf() standard C library function. After stopping the code, we may then observe the contents of the variables and see how they are stored in the device’s memory.
Results
4
End Debug Session
Clear the UART 1 Window - put the cursor in the UART 1 Window then enter Ctrl +L
This will clear the UART 1 Window before your next simulationEnd the Simulation Session by clicking the Finish Debugger Session button.
Then CLOSE the Project
Conclusions
You have now seen how to declare variables:
type identifier1, identifier2, ... , identifierN;
You have also seen that different data types occupy different amounts of RAM. Since memory resources are relatively scarce in an embedded system, choosing the optimal data type for your variables is very important. This doesn’t mean that you should always use the smallest type possible. Using a char in a 16-bit architecture might allow you to pack two 8-bit variables into a single RAM location, but it may also cause more code to be generated when manipulating those variables. As a general rule, the most highly optimized data type for a given architecture is the one that matches the data word width. For an 8-bit architecture, char is often the best. For a 16-bit architecture, an int is often best. Just keep in mind that not every compiler defines an int as 16-bits.
You have also seen how we can look at the contents of variables using the MPLAB X IDE and that you can look at the value of the variable as the C compiler sees it, as well as the raw value as it is stored in data memory (RAM).