The printf() function will allow us to display any value in your code so that it is human readable. What does this mean? Let's say you would like to tell the computer what temperature your MSP430 has measured; a typical output might be "Temperature: 71°F". We know how to read the internal temperature from a previous post, but how do we get our MSP430 to output the above string over UART? We use the printf() function.

printf("Temperature: %u°F\r\n", tempValue);

I will not be explaining how printf() actually works in this post. If you would like more information on how to use this function please see the above mentioned book, or this link. Due to the fact that the MSP430 has limited resources (e.g. memory), a compact version of the standard printf() function must be used.

Since the printf() included in Code Composed Studio is very large and will not fit on many of the value line MSP430s, we must add our own printf() function to our projects. oPossum on the 43oh.com forums has shared his printf() with the community and since reinventing the wheel is rarely a good idea, we will be using oPossum's code.

"This is a tiny printf() function that can be used with the chips that come with the Launchpad. Code size is about 640 bytes with CCS."

This is how oPossum describes his function. While this function does not support all of the standard printf() features, it is more than sufficient for use on an MSP430. Using this function, we can format 7 separate data types: character, string, integer, unsigned integer, long, unsigned long, and hexadecimal (16-bit) formatting.

I want to thank oPossum for sharing his code with the community. The code is very well written and works great! Thanks again!

Code

Please scroll down for the rest of the post. One of these days I will figure out how to limit the height of my embedded code.

Customizing the Output

Depending on your project, you will want to have printf() output differently. For this example, printf() will send the formatted string out over the UART to a computer. For a different project, you might want to output the string to an LCD, or through USB. The code provided from oPossum allows you to define two functions, puts() and putc() which determines how printf() will output the formatted string. Without defining these two functions, the printf() will actually not do a thing. printf() uses puts(char *) to send out a string value, and uses putc(unsigned) to send out a character.

In the code above, my
puts(char *) and putc(unsigned) functions sends the string and character out over UART using another of my functions, sendByte(unsigned char). The code in these functions is fairly straight forward, and will not be discussed here. If you would like to another output for printf(), you would create your own puts(char *) and putc(unsigned) functions as I have done in the code above.

Testing the printf() Function

I will be using a very similar setup as oPossum for testing the printf() function. The code above will send a test sequence to the computer when the MSP430 receives the character 't' over UART. I recommend using Realterm for interfacing with the MSP430 over UART. To connect, select the correct COM port and set the baud rate to 9600. Once connected, send the character 't' by clicking on the blank window and typing 't'. You should see the following response from the MSP430.

Test sequence from the MSP430

Conclusion

Use this code in your projects! It really is a great piece of code. To use this printf() function to your own CCS projects, just add printf.c to your project and create your own puts(char *) and putc(unsigned) in your main c file as I have done in this example.

In the next post, I will expand on the previous post by creating a real-time clock using the MSP430. Stay tuned!

I'd like to know how the method you've given here is better, because in my CCS v5.2, printf() defaults to minimal for use with the MSP430 devices and the limitations appear to be the same, only it's a heck of alot easier than what you've shared here - albeit impressive.

The Wiki mentioned is not just for value line MSP430's. I think the CCS minimal printf is still too big for the value line devices, even with optimization at best - so maybe this version will work where the "minimal" won't. It's certainly worth a try.

This code was written for the MSP430G2553. With some modification, you can get this code to work (with a software UART). Also, it is important to note that not all definitions are the same between the two chips.

Thanks for the great post, although I'm a little confused how to use Realterm to communicate with the MSP430 over UART. I have my MSP430 launchpad hooked up to my Windows XP machine via the mini USB connection. According to the device manager, the Launchpad is connected on COM port 4, so I tried running the code from CCS then running Realterm to send a "t" character on COM 4, but it doesn't do anything. I tried setting a breakpoint in the USCI0RX_ISR function, but it never gets there. Does the launchpad need to be connected to my computer using an interface other than the USB connection, or do I just need to change around the UART jumper pins on the Launchpad? Thanks for any advice, your blog is extremely helpful!!

The first thing I would recommend doing is jumping the RX and TX pins on the emulator side of the LaunchPad. This will echo the characters sent in Realterm and will test if all the hardware is working. If that works, try transmitting characters from the LaunchPad to Realterm when you press a button. Once you have verified you can send characters, then attempt to receive them. Good luck!

Thanks for the response NJC, I ended up getting it working by removing the TX and RX jumper pins, although I'm a little confused about why that worked.

Right now, the TX and RX jumper pins are oriented vertically, which, according to the little diagram on the board means that it's using SW UART. I thought this is what I wanted - to use the built in UART through the USB cable. Unfortunately, this didn't work for me when using Realterm to connect to the Virtual COM port that the LaunchPad is UART is connected to. I had to change the jumper pins to the horizontal orientation, which is HW UART. This doesn't make sense, since I thought for HW UART, I'd need to use a separate UART device, such as an FTDI to USB adapter which connects to P1.1 and P1.2 of the LaunchPad. I'm guessing I've probably just got my understanding of HW and SW UART mixed up..

By the way, do you know any way to use printf to output something to the console in Code Composer Studio? Thanks!

The difference between a HW and SW UART is strictly within the MSP430. The external hardware does not care if the MSP430 has a SW or HW UART. A HW UART (such as on the MSP430G2553) is physically built into the MSP430 in the same way an ADC or Timer is. A SW UART is created when there is no HW UART (such as in the MSP430G2331) in order to "manually" allow UART communication via code.

I believe that the reason you needed to change the jumpers is because there are different hardware revisions of the LaunchPad (mine are the older versions).

As for using printf to output directly to CCS, I am not sure. I prefer Realterm and have never tried using the console in CCS. I believe it is possible though. Try asking on TIs forums. e2e.ti.com

Thanks again for your reply NJC, your answer makes sense. In regards to printing to the console, I found the following message on the 43oh.com forums which explains how to get console debug statements working with the Tiny Printf code from oPossum: http://www.43oh.com/forum/viewtopic.php?f=10&t=2953&p=21551

Haven't seen this mentioned yet... You NEED the 32kHz watch crystal soldered to your LaunchPad to get this Demo code working out of the box. Otherwise, you need to modify the UART and Timer code portions such that ACLK (auxilary clock) is set to use the DCO as opposed to the external crystal.

In case this helps somebody. With GCC 4.9.1, overriding putc caused the linker to error out with 'multiple definition' error: a conflict between the app and libc (which is strange -- the definition in the app object file *should* take precedence). A workaround was to override putchar instead of putc, and replace putc with putchar in oPossum's printf code.