当前位置:网站首页>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 .
边栏推荐
- Introduction to alos satellite
- Volcano devient l'ordonnanceur de lots par défaut Spark
- Pingcap was selected as the "voice of customers" of Gartner cloud database in 2022, and won the highest score of "outstanding performer"
- Experience of MDM master data project implementation for manufacturing projects
- The sharp sword of API management -- eolink
- flink-sql的kafka的这个设置,group-offsets,如果指定的groupid没有提
- Unityshader world coordinates do not change with the model
- ls 常用参数
- [computer talk club] Lecture 3: how to raise key issues?
- Introduction and tutorial of SAS planet software
猜你喜欢

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

Introduction and download tutorial of administrative division vector data

优维低代码:构件渲染子构件

Drawing DEM with GEE gracefully

60 divine vs Code plug-ins!!

Northwestern Polytechnic University attacked by hackers? Two factor authentication changes the situation!

Power efficiency test

西北工业大学遭黑客攻击?双因素认证改变局面!

敏捷之道 | 敏捷开发真的过时了么?

试驾 Citus 11.0 beta(官方博客)
随机推荐
Freeswitch uses origin to dialplan
为什么生命科学企业都在陆续上云?
NFT双币质押流动性挖矿系统开发
What other data besides SHP data
试驾 Citus 11.0 beta(官方博客)
PingCAP 入选 2022 Gartner 云数据库“客户之声”,获评“卓越表现者”最高分
Power efficiency test
Nokov motion capture system makes it possible for multi field cooperative UAV to build independently
Interpreting harmonyos application and service ecology
System design idea of time traceability
php OSS文件讀取和寫入文件,workerman生成臨時文件並輸出瀏覽器下載
Application scenarios of channel of go question bank · 11
Apifox与其他接口开发工具的博弈
Volcano becomes spark default batch scheduler
Generate the last login user account report of the computer through SCCM SQL
8 challenges of BSS application cloud native deployment
Make track map
flink-sql的kafka的这个设置,group-offsets,如果指定的groupid没有提
小滴课堂海量数据处理商用短链平台大课
ls 常用参数