Share a stm32 serial port printing function. If the printf function takes up a lot of memory and needs to be redirected, the following method can be used to simply print basic information such as debugging information, which can be directly copied and used.
/**********************************************************
Integer data to string function
char *itoa(int value, char *string, int radix)
radix=10 indicates decimal, not decimal, the conversion result is 0;
Example: d=-379;
After executing itoa(d, buf, 10);
buf="-379"
**********************************************************/
char *itoa(int value, char *string, int radix)
{
int i, d;
int flag = 0;
char *ptr = string;
/* This implementation only works for decimal numbers. */
if (radix != 10)
{
*ptr = 0;
return string;
}
if (!value)
{
*ptr++ = 0x30;
*ptr = 0;
return string;
}
/* if this is a negative value insert the minus sign. */
if (value < 0)
{
*ptr++ = '-';
/* Make the value positive. */
value *= -1;
}
for (i = 10000; i > 0; i /= 10)
{
d = value / i;
if (d || flag)
{
*ptr++ = (char)(d + 0x30);
value -= (d * i);
flag = 1;
}
}
/* Null terminate the string. */
*ptr = 0;
return string;
} /* NCL_Itoa */
/********************************************************************************
* Name: void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,...)
* Function: Format serial port output function
* Input parameter: USARTx: Specify serial port
Data: Send array
...: Undefined parameter
* Output parameter: None
* Description: Format serial port output function
"\r" Carriage return USART_OUT(USART1, "abcdefg\r")
"\n" Line feed USART_OUT(USART1, "abcdefg\r\n")
"%s" String USART_OUT(USART1, "The string is: %s","abcdefg")
"%d" Decimal USART_OUT(USART1, "a=%d",10)
* Calling method: None
************************************************************************/
void USART_OUT(USART_TypeDef* USARTx, char *Data,...)
{
const char *s;
int d;
char buf[16];
// uint8_t *Data = pData;
va_list ap;
va_start(ap, Data);
while(*Data!=0)
{
//Judge whether the end of the string has been reached
if(*Data==0x5c)
{ //'\'
switch (*++Data)
{
case 'r': //Carriage return character
USART_SendData(USARTx, 0x0d);
Data++;
break;
case 'n': //Line feed character
USART_SendData(USARTx, 0x0a);
Data++;
break;
default:
Data++;
break;
}
}
else if(*Data=='%')
{ //
switch (*++Data)
{
case 's': //String
s = va_arg(ap, const char *);
for ( ; *s; s++)
{
USART_SendData(USARTx,*s);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);
}
Data++;
break;
case 'd': //Decimal
d = va_arg(ap, int);
itoa(d, buf, 10);
for (s = buf; *s; s++)
{
USART_SendData(USARTx,*s);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);
}
Data++;
break;
default:
Data++;
break;
}
}
else USART_SendData(USARTx, *Data++);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);
}
}
Previous article:No such file or directory problem occurs when running an executable program on ARM
Next article:STM32 TFT-LCD liquid crystal learning
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Sharing of key points on the use of ADI passive components
- Four design considerations for telematics hardware in connected cars
- Wide voltage input USB-C, 5V/3A output adapter reference design
- When the machine is turned off, the LDO output is normally 3V. Why does the voltage reach 3.5V after the machine is turned on? What is the reason?
- 【Smart Sports Watch】3. Build Development Environment 2
- Ginkgo USB-SPI host mode four data transmission mode timing diagram
- History of the Development of Microwave Circuits
- 6657Statically configure serial port general interrupt in sys/bios
- Understanding Nginx Architecture in One Article
- Highly recommended! ADI's latest Chinese information is here