Lab Exercise 3: The printf() Function

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


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 their 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.

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 Lab03.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. This will stop execution so that we may examine the variables and their values.

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.


Lab3 Results in the UART 1 Output Window

The UART1 Output window shows the results of the program. The code uses the printf() function to print out a variety of data types and data formats. We’ll break down the steps next.

Below is a table of the most commonly used control characters for the printf() function. Anywhere one of these characters is encountered after a ‘%’ within a string, it will be replaced by the formatted value of its respective argument from the list following the string. The arguments may be literals or variables.

Conversion characters used by printf function

Below is an explanation of each printf() statement from the Lab3.c source file.

a. printf("25 as decimal (d): %d\n", 25);
This statement prints out the value 25 as a decimal integer (%d). The output correctly displays “25”.

b. printf("'a' as character (c): %c\n", 'a');
This statement prints out the letter ‘a’ as a character (%c). The output correctly displays “a”.

c. printf("'a' as decimal (d): %d\n", 'a');
This statement prints out the letter ‘a’ as a decimal integer (%d). Instead of displaying “a”, the output displays the ASCII value of the character ‘a’, which is 97. This isn't necessarily incorrect as it will depend on what your code is trying to accomplish with the character or variable of type char.

d. printf("2.55 as float (f): %f\n", 2.55);
This statement prints out the value 2.55 as a floating point number (%f). The output correctly displays “2.550000”. It is possible to specify the number of digits that should be displayed, but this was left as the default of 6 digits after the decimal point. (Default may vary among compilers.)

e. printf("2.55 as decimal (d): %d\n", 2.55);
This statement prints out the value 2.55 as a decimal number (%d). The output incorrectly displays “16419”. Because floating point values such as 2.55 are stored using the IEEE-754 format, they need to be properly decoded to retrieve the correct value. In this case, we are displaying a portion of the raw IEEE-754 encoded value.

f. printf("6.02e23 as exponent (e): %e\n", 6.02e23);
This statement prints out the value 6.02 x 1023 as a signed real decimal value with an exponent (%e). The output correctly displays “6.020000e+23”.

g. printf("6.02e23 as decimal (d): %d\n", 6.02e23);
This statement prints out the value 6.02 x 1023 as a decimal integer (%d). The output incorrectly displays “26366” which is part of the raw IEEE-754 encoded floating point number.

h. printf("'Microchip' as string (s): %s\n", "Microchip");
This statement prints out the text “Microchip” as a string (%s). The output correctly displays “Microchip”.

i. printf("'Microchip' as decimal (d): %d\n", "Microchip");
This statement prints out the text “Microchip” as a decimal integer (%d). The output incorrectly displays the value -24132.

End Debug Session

Clear the UART 1 window by putting 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 Finish Debugger Session button button.

Then close the project by right-clicking on Lab03 from the Projects window and then selecting Close.