(Based on the stm32_v5 development board)
1. Create a project and write a C program
#include"stm32f10x.h"
#include"stdarg.h"
void USART1_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Enable USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA , ENABLE);
/*Configure USART1 TX and set it to multiplexed push-pull output mode*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*Configure USART1 RX and set it to floating input mode*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*Configure USART1 mode*/
USART_InitStructure.USART_BaudRate = 57600; //Set the baud rate to 57600//
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //Configure the word length of serial port transmission to 8 bits//
USART_InitStructure.USART_StopBits = USART_StopBits_1; //Configure the stop bit to 1//
USART_InitStructure.USART_Parity = USART_Parity_No; //Do not set the parity bit //
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //Do not use hardware flow//
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Configure the serial port to two-wire full-duplex communication, turn on both RX and Tx modes //
USART_Init(USART1, &USART_InitStructure); //Write configuration parameters to registers//
USART_Cmd(USART1, ENABLE); //Enable USART1 peripheral//
}
static 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 a minus sign*/
if(value < 0)
{
*ptr++ = '-';
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 terminated string*/
*ptr = 0;
return string;
}
void USART1_printf(USART_TypeDef* USARTx, uint8_t *Data,...)
{
const char *s;
int d;
char buf[16];
va_list ap;
va_start(ap, Data);
while (*Data != 0) //Judge whether the string terminator has been reached//
{
if(*Data == 0x5c) //'\'//
{
switch( *++Data )
{
case 'r': //Carriage return character//
USART_SendData(USARTx, 0x0d);
Data ++;
break;
case 'n': //line break//
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);
drown(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 );
}
}
int main(void)
{
/*Initialize the serial port*/
USART1_Config();
USART1_printf(USART1,"\r\nHello Word\r\n");
while(1);
}
2. Connect J-Link, compile, and download the program into the stm32 development board
3. Connect the serial port and open the serial port debugging assistant for debugging
See Hello World printed, the goal is completed
Previous article:STM32 serial port register library function configuration method
Next article:STM32F103 series practical universal synchronous asynchronous receiver and transmitter (USART)
- Popular Resources
- Popular amplifiers
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
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- From cart-mounted to portable: Ultrasound smart probes could transform healthcare delivery
- [RISC-V MCU CH32V103 Review] - 9: Motor Control Board
- Raspberry Pi Windows IoT Development (Part 2) USB Camera
- EEWORLD University Hall----Live Replay: Using DLP? Micro-projection technology to design augmented reality smart glasses
- Detailed explanation of ZigBee networking principle
- A brief introduction to FPGA
- What are the difficulties and common methods of battery SOC estimation?
- VC++ In-depth Explanation (Revised Edition)
- 【NXP Rapid IoT Review】+ Summary
- Allwinner V853 NPU demo