Article Directory
- sprintf
- Using sprintf in 51 microcontroller
- printf
- Use printf as serial port output in 51 microcontroller
sprintf
The function of srpintf() is very powerful: it is more efficient than some string operation functions; it is more flexible; it can output the desired result to the specified string or as a buffer, while printf can only be output to the command line~
Header file:
Function function: Format strings, write formatted data into strings.
Function prototype: int sprintf(char *buffer, const char *format, [argument]…)
Function return value: the length of the string pointed to by buffer;
Using sprintf in 51 microcontroller
sprintf(BUFF_HC,"Soke:%d,Someone ",(int)Value);
Note: here you must force it to int type
Reason: It may be that the storage methods of 8-bit systems and 32-bit systems are different, and the methods of them are also different.
printf
printf() is a C language standard library function that outputs formatted strings to standard output. Standard output, that is, standard output file, corresponds to the screen of the terminal. printf() declared in the header file.
Use printf as serial port output in 51 microcontroller
I checked the printf function description in the keil help file. It turned out that the printf function ultimately called the putchar function to print out the output characters.
//UART1 sends serial port data
void UART1_SendData(char dat)
{
ES=0; //Off serial port interrupt
SBUF=dat;
while(TI!=1); //Waiting for successful sending
TI=0; //Clear the send interrupt flag
ES=1; //Open serial port interrupt
}
//UART1 send string
void UART1_SendString(char *s)
{
while(*s)//Detection of end string character
{
UART1_SendData(*s++);//Send the current character
}
}
//Rewrite putchar function
char putchar(char c)
{
UART1_SendData(c);
return c;
}
Note: Be sure to close the serial port interrupt
The article is summarized and recorded by yourself. Some knowledge points have not been explained clearly. Please give more opinions and exchange more information. Everyone is welcome to leave a message.
If you have technical communication, you can add the following groups to facilitate communication