当前位置:网站首页>第七篇,STM32串口通信编程
第七篇,STM32串口通信编程
2022-07-06 17:07:00 【车水码浓】
1.通信的基本概念
(1)串行通信和并行通信

(2)单工,半双工和全双工
(3)通信速率

单位时间内传输的比特数表示传输速度,叫做波特率(bps)
(4)通信协议(串口)
通信协议就是通信双方约定好的数据格式

2.串口的硬件连接

UART ----------------- 通用异步收发器
USART --------------- 通用同步/异步收发器
3.stm32的串口开发
(1)原理图

USB调试口连接到了CPU的PA9 PA10,它们具有串口复用的功能
(2)CPU芯片手册


(3)串口接口的编程实现
在工程中添加串口库函数源码

1)开启GPIOA和USART1的时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
2)将PA9 PA10配置为复用功能,并且映射到串口1
GPIO_Init(...); GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
3)初始化串口
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct); 参数: USARTx - 哪个串口
USART_InitStruct - 串口初始化结构
typedef struct
{
uint32_t USART_BaudRate; /*!< 波特率 */
uint16_t USART_WordLength; /*!< 数据位长度 @ref USART_Word_Length */
uint16_t USART_StopBits; /*!< 停止位长度 @ref USART_Stop_Bits */
uint16_t USART_Parity; /*!< 校验方式 @ref USART_Parity */
uint16_t USART_Mode; /*!< 发送/接收模式 @ref USART_Mode */
uint16_t USART_HardwareFlowControl; /*!< 硬件流控制 @ref USART_Hardware_Flow_Control */ } USART_InitTypeDef;
4)使能串口
USART_Cmd(USART1,ENABLE);
5)数据的发送和接收
发送:
//轮询
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
//传入往哪个串口发送什么数据
//每次发送前查询上一个数据是否发送完成
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
//传入哪个串口的什么标志,返回SET表示有该标志,返回RESET表示没有该标志
//USART_FLAG_TC ----- 发送完成标志
———————————————————————————————————————————
练习:
实现串口1字符串的发送
———————————————————————————————————————————
//功能函数代码
#include <stm32f4xx.h>
#include <usart.h>
#include <stdio.h>
#include <string.h>
#include <includes.h>
//stm32开发板上运行程序,如果主机运行了调试器,程序就会使用主机的输入输出设备
//这是方式叫半主机模式,printf如果要通过串口打印,必须关闭半主机模式
#pragma import(__use_no_semihosting)
struct __FILE{
int handle;
};
FILE __stdout;
//定义_sys_exit函数避免使用半主机模式
void _sys_exit(int x)
{
x = x;
}
//重定义fputc
int fputc(int ch,FILE *F)
{
//等待上一个数据发送完成
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
//发送
USART_SendData(USART1,ch);
return ch;
}
void usart1_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
//1.开启GPIOA和USART1时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
//2.配置PA9 PA10为串口功能
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//复用模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;//高速
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;//无上下拉
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
//3.初始化串口 8N1
USART_InitStruct.USART_BaudRate = 115200;//波特率
USART_InitStruct.USART_WordLength = USART_WordLength_8b;//8位数据位
USART_InitStruct.USART_StopBits = USART_StopBits_1;//1位停止位
USART_InitStruct.USART_Parity = USART_Parity_No;//无校验
USART_InitStruct.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;//发送接收模式
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件流控制
USART_Init(USART1,&USART_InitStruct);
//4.开启串口接收中断(清除中断标志)
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//5.初始化NVIC
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;//串口1中断通道
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x2;//抢占优先级
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x2;//响应优先级
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//使能
NVIC_Init(&NVIC_InitStruct);
//.使能串口
USART_Cmd(USART1,ENABLE);
}
//发送一个字符(轮询)
void uart1_putc(char ch)
{
//等待上一个数据发送完成
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
USART_SendData(USART1,ch);
}边栏推荐
- Leecode brushes questions and records interview questions 01.02 Determine whether it is character rearrangement for each other
- fastDFS数据迁移操作记录
- [yolov5 6.0 | 6.1 deploy tensorrt to torch serve] environment construction | model transformation | engine model deployment (detailed packet file writing method)
- Value Function Approximation
- Five different code similarity detection and the development trend of code similarity detection
- @TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
- Alexnet experiment encounters: loss Nan, train ACC 0.100, test ACC 0.100
- stm32F407-------SPI通信
- 什么是时间
- Advanced learning of MySQL -- Fundamentals -- four characteristics of transactions
猜你喜欢
![[user defined type] structure, union, enumeration](/img/a5/d6bcfb128ff6c64f9d18ac4c209210.jpg)
[user defined type] structure, union, enumeration

【批處理DOS-CMD命令-匯總和小結】-字符串搜索、查找、篩選命令(find、findstr),Find和findstr的區別和辨析

The programmer resigned and was sentenced to 10 months for deleting the code. Jingdong came home and said that it took 30000 to restore the database. Netizen: This is really a revenge

Lombok 同时使⽤ @Data 和 @Builder 的坑,你中招没?

Linear algebra of deep learning

ZYNQ移植uCOSIII

学习光线跟踪一样的自3D表征Ego3RT

Jenkins' user credentials plug-in installation

Telerik UI 2022 R2 SP1 Retail-Not Crack

集合(泛型 & List & Set & 自定义排序)
随机推荐
学习光线跟踪一样的自3D表征Ego3RT
Data processing of deep learning
深度学习之环境配置 jupyter notebook
基于GO语言实现的X.509证书
什么是时间
build. How to configure the dependent version number in the gradle file
Article management system based on SSM framework
fastDFS数据迁移操作记录
VTK volume rendering program design of 3D scanned volume data
Leecode brush questions record sword finger offer 43 The number of occurrences of 1 in integers 1 to n
Equals() and hashcode()
Dr selection of OSPF configuration for Huawei devices
The programmer resigned and was sentenced to 10 months for deleting the code. Jingdong came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
建立自己的网站(17)
Chapter 5 DML data operation
Advanced learning of MySQL -- basics -- multi table query -- self join
48 page digital government smart government all in one solution
Data analysis course notes (V) common statistical methods, data and spelling, index and composite index
Advanced learning of MySQL -- Fundamentals -- concurrency of transactions
uniapp实现从本地上传头像并显示,同时将头像转化为base64格式存储在mysql数据库中