当前位置:网站首页>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 .
边栏推荐
- 一文理解OpenStack网络
- How to deal with the problem that the Flink CDC reads MySQL in full and always reports this error
- NFT pledge liquidity mining system development technology
- 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
- Development of NFT dual currency pledge liquidity mining system
- NFT双币质押流动性挖矿系统开发
- 小滴课堂海量数据处理商用短链平台大课
- 对国产数据库厂商提几个关于SQL引擎的小需求
- NFT质押流动性挖矿系统开发技术
- 网络安全审查办公室对知网启动网络安全审查
猜你喜欢

Module V

Apifox与其他接口开发工具的博弈

What other data besides SHP data

模块五

ArrayList源码解析

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

Volcano becomes spark default batch scheduler

Mqtt protocol usage of LabVIEW

Experience of MDM master data project implementation for manufacturing projects

Introduction and download of nine npp\gpp datasets
随机推荐
How to use R package ggtreeextra to draw evolution tree
TKDE2022:基于知识增强采样的对话推荐系统
Set up your own website (8)
ls 常用参数
A detailed explanation of the implementation principle of go Distributed Link Tracking
实时渲染:实时、离线、云渲染、混合渲染的区别
Apifox与其他接口开发工具的博弈
[leetcode] rotation series (array, matrix, linked list, function, string)
Real time rendering: the difference between real-time, offline, cloud rendering and hybrid rendering
Buddha bless you that there will never be a bug
Volcano成Spark默認batch調度器
目前是不是只cdc 监控mysql 可以拿到新增列的数据 sqlserver不行是吧
请问一下2.2.0版本支持动态新增mysql同步表吗
数字孪生行业案例:智慧港口数字化
Fabric 账本数据块结构解析(一):如何解析账本中的智能合约交易数据
Huawei machine learning service speech recognition function enables applications to paint "sound" and color
php OSS文件读取和写入文件,workerman生成临时文件并输出浏览器下载
华为机器学习服务语音识别功能,让应用绘“声”绘色
佛祖保佑 永无BUG
R语言corrplot相关热图美化实例分析