uart_printf ()

Source: Internet
Author: User
Tags sprintf

uart_printf () for the commonly used serial printing function, which is often used to print information through the serial port when certain conditions are met. Many developers like to use it in interrupt service programs, so only a single uart_printf () Interrupt service program can be considered a universal and simple application. The following is a test result for a interrupt service program that contains only one sentence of uart_printf ().

uart_printf () function analysis

Arm and PC communication, often through the uart_printf () This function in the host computer output information. The following is a detailed analysis of this function function.

Prototype:

//-----------------------------------------------------------------

void uart_printf (char *fmt,...)//... Represents a variable parameter (multiple variable parameters compose a

List, followed by a special pointer pointing to him), with no limits on number and type,

{

Va_list ap;//Initializes a pointer to a variable argument list

Char string[256];

Va_start (AP,FMT);//The address of the first variable parameter is paid to the AP, that is, the AP points to the variable parameter column

The beginning of the table

vsprintf (STRING,FMT,AP)//convert parameter FMT to format with variable parameters pointed to by AP

The string array, which acts as

sprintf (), just different parameter types

Uart_sendstring (string); Send the formatted string from the Development Board serial port

Va_end (AP); AP value of 0, no practical use, mainly for the robustness of the program

}//-----------------------------

Va_list in this macro is defined in Stdarg.h, so a program that uses variable parameters should contain this file.

(1) Format string

printf ("%s,%-19s:%6.2", lastname,firstname,prize);

Printed Result: Bechr,teddy 2000.00

We say "Bechr,teddy 2000.00" is a formatted string, and printf's role is to translate ("%s,%-19s:6.2", lastname,firstname,prize) into a computer-aware string, and for "%s,%-19s:6.2", lastname,firstname,prize computer roots do not know, so need to printf translation.

(2) vsprintf

Function Name: vsprintf

Function: Send formatted output string to specified array

Usage: int vsprintf (char *string, char *format, va_list param);

Vsprintf is the same as the sprintf function, which is to output the formatted string to the specified array, sprintf (char *string, char *farmat [, Argument,...]) The parameters of a function start with the second argument as printf, except that the sprintf is output to the specified array, printf is output to the screen (a standard output file), and thus sprintf more char *string this parameter.

uart_printf () This function 44blib.c in the library function provided by Samsung, where the Va_start,vsprintf,va_ End is defined in the Stdarg.h, this file in the Linux kernel, there is no more analysis, first master how to use it.

In short, this function can be simply interpreted as translating your C language output into a string that can be recognized at the bottom of the hardware. Call this function can be in accordance with standard C-oriented terminal output method, output to output their own content

Add a few header files:

#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.