Lab Exercise 3: The printf() Function

 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 Main_Open_Project.png 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 OpenProjectButton.png.

2

Debug Project

Click on the Debug Project Main_Debug_Project.png button. This will build and send the program to the simulator.
Click on the Continue Debug_Continue.png button. This begins the simulation.Wait for the UART 1 Output window to finish printing.
Click on the Halt Debug_Pause.png 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 Debug_Finish_Debugger_Session.png 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.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.