当前位置:网站首页>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 
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 
( 5、 ... and ) Hardware connection
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

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 .
边栏推荐
- Generate the last login user account report of the computer through SCCM SQL
- flink cdc全量读mysql老是报这个错怎么处理
- 我用sql形式的会出现cdc读取乱序吗
- Power efficiency test
- Sr-gnn shift robot gnns: overlapping the limitations of localized graph training data
- Internet of things? Come and see Arduino on the cloud
- Dataworks development ODPs SQL development production environment automatic completion of ProjectName
- Volcano成Spark默认batch调度器
- Mqtt protocol usage of LabVIEW
- How to use JWT authentication in thinkphp6
猜你喜欢

Using alicloud RDS for SQL Server Performance insight to optimize database load - first understanding of performance insight

怎么使用R包ggtreeExtra绘制进化树

Interprétation de la thèse (SR - gnn) Shift Robust GNNS: Overcoming the Limits of Localized Graph Training Data

论文解读(SR-GNN)《Shift-Robust GNNs: Overcoming the Limitations of Localized Graph Training Data》

Do you have all the basic embedded knowledge points that novices often ignore?

What other data besides SHP data

Network security review office starts network security review on HowNet

Introduction to alos satellite

Intel and Microsoft give full play to the potential energy of edge cloud collaboration to promote the large-scale deployment of AI

starring开发HttpJson接入点+数据库
随机推荐
Does version 2.2.0 support dynamic addition of MySQL synchronization tables
Volcano devient l'ordonnanceur de lots par défaut Spark
ls 常用参数
一文理解OpenStack网络
Huawei machine learning service speech recognition function enables applications to paint "sound" and color
The script implements the automated deployment of raid0
我用sql形式的会出现cdc读取乱序吗
Buddha bless you that there will never be a bug
If the programmer tells the truth during the interview
Will the CDC read out of order when I use SQL
Source code analysis of ArrayList
PHP OSS file reads and writes files, and workman generates temporary files and outputs them to the browser for download
Starring V6 platform development take out point process
What type of datetime in the CDC SQL table should be replaced
The cdc+mysql connector joins the date and time field from the dimension table by +8:00. Could you tell me which one is hosted by Alibaba cloud
IBPS开源表单设计器有什么功能?
Using alicloud RDS for SQL Server Performance insight to optimize database load - first understanding of performance insight
Ls common parameters
Several ways of connecting upper computer and MES
Make track map