Objective
The purpose of this lab is to illustrate the use of the printf() standard C library function. Since its original purpose was to print text to the standard output device of a computer (monitor screen or printer), this function traditionally hadn't been used in embedded systems. Nowadays, it has gained new popularity as many compilers have redirected its output to the microcontroller’s UART. When using the simulator, this function may be used to print text strings to a window within the MPLAB® X IDE. This can be a powerful debugging technique as well as a convenient method for us to display the results of the programs we will be working on in this class.
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 toolbar.Navigate to the folder where you saved the exercise files for this class.
Click on the Lab03.X folder.
Select Open Project .2
Debug Project
Click on the Debug Project button. This will build and send the program to the simulator.Click on the Continue button. This begins the simulation.Wait for the UART 1 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?
As before, we opened a preconfigured project, compiled the complete program and executed it. No code needed to be added or modified for this lab.
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 simulation.End the Simulation Session by clicking the Finish Debugger Session button.
Then CLOSE the project by right-clicking on Lab03 from the Projects window and then selecting Close.
Conclusions
When using the printf() function, it is very important to pick the correct conversion character to use in the control string. If the wrong character is used, data will still be printed out, but it may not accurately represent the true value of the data in the argument list. Depending on the application, a simple mistake with the conversion character can have serious consequences.