3.3.4 How to Implement the Function that Handles STDIO
The function that handles STDIO is void puthc(char c)
.
Implementing this function will transmit its char argument over EUSART and will cause
the printf
to be redirected to EUSART.
void putch(char txData)
{
EUSART2_write(txData);
}
The following line will send messages over EUSART:
printf("%c%c%c%c", START_DATA_STREAM_PROTOCOL, cnt, cnt * 2, STOP_DATA_STREAM_PROTOCOL);
The Data Stream Protocol frame has start and stop tokens, as inverse/one’s complement each other. In between those, the payload may contain any number of values. The protocol must be sent in binary format.
Note: The
printf
function
uses placeholder specifiers in the format string to mark where to insert variables. Some
of the available placeholders are displayed in the table below.Placeholder | Description |
---|---|
%d | Insert a signed integer |
%f | Insert a floating point number |
%s | Insert a sequence of characters |
%c | Insert a character |
%x | Insert integer unsigned in hex
format |
In this example, the %c specifier is used, because the row binary value will be transmitted.
Note: The %x specifier inserts the hex value
as ASCII characters, thus, this is not well-suited here.
Use MPLAB X Data Visualizer as described in the appendix, How to Configure MPLAB X Data Visualizer to Decode Data Stream Protocol.