当前位置:网站首页>【STM32】入门(五):串口TTL、RS232、RS485
【STM32】入门(五):串口TTL、RS232、RS485
2022-08-04 18:03:00 【郭老二】
1、简述
1.1 电平
TTL :全双工 ,逻辑0对应0V,逻辑1对应3.3V或者5V,一般从单片机引脚直接引出,电平3.3或5V是与IO电平兼容;
RS232:全双工,逻辑0对应+3V至+15V,逻辑1对应-15V至-3V ,TTL经过电平转换芯片后可以输出RS232。
RS485:半双工,逻辑0对应-6V至-2V,逻辑1对应+2V至+6V,这里的电平指AB两线间的电压差,即差分方式传输,因此可以长距离传输
1.2 连接方式
除了电压不同,连接方式有所区别:
TTL、RS232只能一对一连接;
RS-485在总线上是允许连接多达128个收发器。
RS232常用DB9头的定义:
1.3 传输协议
一帧数据包由起始位、有效数据、校验位以及停止位组成。
协议参数有:波特率(9600、115200等)、有效数据长度(5~8)、校验(奇、偶、0、1、无)
2、手册
2.1 功能框图

2.2 中断控制

2.3 模式配置

2.4 寄存器映射

3、代码
3.1 初始化
- 使能GPIO时钟:RCC_AHB1PeriphClockCmd
- 使能UART时钟:RCC_APB2PeriphClockCmd
- GPIO引脚复用为UART:GPIO_PinAFConfig
- GPIO配置:GPIO_InitStructure、GPIO_Init
- UART配置:USART_InitStructure、USART_Init
- 中断配置;NVIC_InitStructure、NVIC_Init
void uart1_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
#a)使能时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
#b)引脚复用映射
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
#c)GPIO配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9与GPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA9,PA10
#d)UART配置
USART_InitStructure.USART_BaudRate = bound; //波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1; //一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART1, &USART_InitStructure); //初始化串口1
USART_Cmd(USART1, ENABLE); //使能串口1
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开启相关中断
#e)中断配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //串口1中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3; //抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure);
}
3.2 发送
发送一个字符:版本1
void uart1SendChar(u8 ch)
{
while((USART1->SR&0x40)==0);
USART1->DR = (u8) ch;
}
发送一个字符:版本2
void uart1SendChar(uint8_t ch)
{
#a) 等待发送数据寄存器为空
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1,ch);
}
发送数组
void Usart1SendArray(uint8_t *array, uint16_t num)
{
uint16_t i;
#a)循环发送每个字符
for(i=0; i<num; i++)
{
uart1SendChar(array[i]);
}
#b)等待发送完成
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
USART_FLAG_TXE和USART_FLAG_TC的详细说明
USART_FLAG_TXE:发送缓冲区空标志,说明可以往数据寄存器写入数据了,但并不代码数据发送完成了。
USART_FLAG_TC:发送完成标志,这个才是代表USART在缓冲区的数据发送完成了,即从机接收到了数据。
这两个标志的区别在于,它们分别表示数据在发送过程中,在两个不同的阶段中的完成情况。
TXE 表示数据被从发送缓冲区中取走,转移到的移位寄存器中,此时发送缓冲是空的,可以向其中补充新的数据了。
TC 表示最后放入发送缓冲区的数据已经完成了从移位寄存器向发送信号线 Tx 上的转移。
所以,判定数据最终发送完成的标志是 TC,而不是 TXE.
3.3 接收
查询中断向量表,在代码文件startup_stm32f10x_hd.s中:
串口1USART1对应到中断函数为USART1_IRQHandler
对应上面的中断控制配置:NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
__Vectors DCD __initial_sp ; Top of Stack
...
DCD USART1_IRQHandler ; USART1
...
重载中断函数 USART1_IRQHandler,实现一个回显功能,即接收啥,发送啥
void USART1_IRQHandler(void)
{
uint8_t ucTemp;
if(USART_GetITStatus(USART1, USART_IT_RXNE)!=RESET)
{
ucTemp = USART_ReceiveData(USART1);
USART_SendData(USART1, ucTemp);
}
}
边栏推荐
- Short-term reliability and economic evaluation of resilient microgrids under incentive-based demand response programs (Matlab code implementation)
- 【软件工程之美 - 专栏笔记】37 | 遇到线上故障,你和高手的差距在哪里?
- 静态iP与权限更改[通俗易懂]
- 基于 eBPF 的 Kubernetes 可观测实践
- 又一款高颜值 Redis 官方可视化工具,功能真心强大!
- Go 言 Go 语,一文看懂 Go 语言文件操作
- 报道称任天堂在2023年3月前不会推出任何新硬件产品
- How to recruit programmers
- 路由懒加载
- EasyCVR本地接入国标设备映射公网后,本地设备出现无法播放与级联的解决方法
猜你喜欢

Hezhou Cat1 4G module Air724UG is configured with RNDIS network card or PPP dial-up, and the development board is connected to the Internet through the RNDIS network card (taking the RV1126/1109 devel

After EasyCVR is locally connected to the national standard device to map the public network, the local device cannot play and cascade the solution

2022 May 1 Mathematical Modeling Question C Explanation

npm配置国内镜像(淘宝镜像)

如何让 JS 代码不可断点

EasyCVR如何通过接口调用设备录像的倍速回放?

2019年海淀区青少年程序设计挑战活动小学组复赛试题详细答案

Codeforces积分系统介绍

Introduction of three temperature measurement methods for PT100 platinum thermal resistance

合宙Cat1 4G模块Air724UG配置RNDIS网卡或PPP拨号,通过RNDIS网卡使开发板上网(以RV1126/1109开发板为例)
随机推荐
Google Earth Engine APP——一键在线查看全球1984-至今年的影像同时加载一个影像分析
"No title"
After EasyCVR is locally connected to the national standard device to map the public network, the local device cannot play and cascade the solution
不论你是大众,科班和非科班,我这边整理很久,总结出的学习路线,还不快卷起来
PT100铂热电阻三种测温方法介绍
EasyCVR calls the cloud recording API and returns an error and no recording file is generated. What is the reason?
Create Sentinel high-availability cluster current limiting middleware from -99
使用bash语句,清空aaa文件夹下的所有文件
数仓建模面试
Error when using sourcemap for reporting an error: Can‘t resolve original location of error.
Codeforces积分系统介绍
JS兼容问题总结
clickhouse online and offline table
防火墙基础之防火墙做出口设备安全防护
基于大学生内卷行为的调查研究
【日记】nodejs构建API框架以及RESTful API 和 JSON-RPC的取舍
buuctf(探险1)
The Industrial Metaverse Brings Changes to Industry
Flink/Scala - Storing data with RedisSink
Matlab drawing 1