当前位置:网站首页>UART communication (STM32F103 library function)

UART communication (STM32F103 library function)

2022-06-24 19:23:00 Me-Space

One 、UART sketch

( One ) Definition

  UART Is a universal serial data bus , For asynchronous communication . The bus communicates in both directions , Can achieve full duplex transmission and reception . This article is just a simple implementation UART A serial port communication .
( Two ) Data frame format
Data link layer , Here's the picture
 Insert picture description here
   Idle level : Leisure is high
   Start bit : Pull it down 1 position
   Data bits : First low then high 5~8 position STM32:8/9 position
   Check bit : Parity check 1 position Accuracy rate : Fifty percent
   Stop bit :0.5~2 position Pull high level to end communication STM32:1/2 position
( 3、 ... and )UART Four elements
   Baud rate : Data transfer rate
   Stop bit : Character data end flag
   Data bits : Data transmitted
   Parity bit : It is used to judge the correctness of the transmitted data
( Four )UART Module workflow
 Insert picture description here
( 5、 ... and ) Hardware connection
 Insert picture description here notes :
   Connecting two devices UART level , If the level range is inconsistent, please connect after level conversion

Two 、 Program example

1、 Serial port pin initialization

void Usart1_Pin_Init(u32 brr)
{
    
    GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	// Turn on the clock 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	//PA9  Multiplexing push pull output 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	//PA10  Multiplexed input 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	USART_InitStructure.USART_BaudRate = brr;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;// Disable hardware streaming 
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;// Pattern 
	USART_InitStructure.USART_Parity = USART_Parity_No;// Disable parity 
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1,&USART_InitStructure);
	
	USART_ITConfig(USART1,USART_FLAG_RXNE | USART_IT_TXE,ENABLE);
	USART_Cmd(USART1,ENABLE);
}

2、 Change the output direction of data

int fputc(int c, FILE * stream)
{
     
	// Judge whether the last data transmission is completed 
	while(!(USART1->SR & (1<<6)));
	USART1->DR = c;
	return c;
}

3、 The main function

int main(void)
{
    
	Usart1_Pin_Init(115200);
	printf(" Successful initialization \r\n");
	
	while(1)
	{
    
		
	}
}

4、 Echo function

void Echo_Function(void)
{
    
	u8 usart_data;
	// Waiting to receive data register is not empty 
	while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) == RESET);   //EST : Not empty  RESET: It's empty 
	usart_data = USART_ReceiveData(USART1);
	/*  Wait for the send data register to be empty  */
	while(USART_GetFlagStatus(USART1,USART_IT_TXE) == RESET); // Send the received serial port string 
	USART_SendData(USART1,usart_data);
}

3、 ... and 、 experimental result

 Insert picture description here
notes :
   The above results , It is the blogger who receives characters ‘a’, Return character ’1’, Test whether the serial port is configured successfully .

Related codes , If necessary, you can download it yourself ( Just to achieve UART A single function
Network disk link :
link :https://pan.baidu.com/s/1jkPSup7QEOTNSEK5fw7JZw
Extraction code :mwnl
If you have any questions, please point them out , What modules can you contact bloggers , Bloggers will inquire and share information .

原网站

版权声明
本文为[Me-Space]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211331527337.html