#include <stdio.h>

Remember to include a terminating "\n" in your printf – stdout doesn't flush until it encounters one by default!!!!


	printf("Hello world!\n");

	printf("%i bytes read : %s\n", rx_length, rx_buffer);

	printf("data_h: %02x\n", sensor_data_h);

printf format codes

d or i – int, decimal (base ten) number

x or X – int, hexadecimal number.  Also "%02x" to specify length of digits

ld – long, decimal number ('l' can also be applied to any of the above to change the type from 'int' to 'long')

u – unsigned, decimal number

lu – unsigned long, decimal number

c – char, single character

s – char pointer, string

f – float, number with six digits of precision
g – float, number with up to six digits of precision
e – float, number with up to six digits of precision, scientific notation
lf – double, number with six digits of precision
lg – double, number with up to six digits of precision
le – double, number with up to six digits of precision, scientific notation

Display Buffer


//************************************
//************************************
//********** DISPLAY BUFFER **********
//************************************
//************************************ 
static void display_buffer (uint8_t* buffer, int len)
{
	int count;
	for (count = 0; count < len; count++)
	{
		printf("%02x ", buffer[i]);
		if (( count + 1 ) % 16 == 0)
		{
			printf("\n");
		}
	}
	printf("\n");
}

printf in a multi thread environment

Watch out for clashes causing bugs!  This can happen with irq's using printf, different threads using it, etc.

Using a semaphore to proect

This is one option to stop clahses, e.g.


#include "semphr.h"

//Create semphore
SemaphoreHandle_t print_semaphore = NULL;

	print_semaphore = xSemaphoreCreateMutex();
    
    
//Use sempahore
	xSemaphoreTake(print_semaphore, portMAX_DELAY);
	printf("Something");
	printf("Something");
	xSemaphoreGive(print_semaphore);

 

UK Electronic Designer
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.