当前位置:网站首页>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 .
边栏推荐
- A detailed explanation of the implementation principle of go Distributed Link Tracking
- Preliminary study nuxt3
- 技术实现 | Apache Doris 冷热数据存储(一)
- 物联网?快来看 Arduino 上云啦
- Fabric ledger data block structure analysis (I): how to analyze the smart contract transaction data in the ledger
- php OSS文件讀取和寫入文件,workerman生成臨時文件並輸出瀏覽器下載
- 工作6年,月薪3W,1名PM的奋斗史
- 想问下 pgsql cdc 账号同一个 多个 task 会有影响吗,我现在3个task 只有一个 有
- Introduction to smart contract security audit delegatecall (2)
- BSS应用程序云原生部署的8大挑战
猜你喜欢

Introduction and download tutorial of administrative division vector data

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

Freeswitch uses origin to dialplan

Sr-gnn shift robot gnns: overlapping the limitations of localized graph training data

应用实践 | 海量数据,秒级分析!Flink+Doris 构建实时数仓方案

Volcano becomes spark default batch scheduler

How do programmers do we media?

Generate the last login user account report of the computer through SCCM SQL

一文理解OpenStack网络

An accident caused by a MySQL misoperation cannot be withstood by High Availability!
随机推荐
小滴课堂海量数据处理商用短链平台大课
###脚本实现raid0自动化部署
Will the CDC read out of order when I use SQL
通过SCCM SQL生成计算机上一次登录用户账户报告
Network security review office starts network security review on HowNet
mysql binlog 数据源配置文档麻烦分享一下
BSS应用程序云原生部署的8大挑战
Make track map
【计算讲谈社】第三讲:如何提出关键问题?
R language 4.1.0 software installation package and installation tutorial
Intel and Microsoft give full play to the potential energy of edge cloud collaboration to promote the large-scale deployment of AI
Introduction and download of nine npp\gpp datasets
Freeswitch使用originate转dialplan
Example analysis of corrplot related heat map beautification in R language
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
subject may not be empty [subject-empty]
Set up your own website (8)
智能合约安全审计入门篇 —— delegatecall (2)
Northwestern Polytechnic University attacked by hackers? Two factor authentication changes the situation!
Zadig + 洞态 IAST:让安全溶于持续交付