当前位置:网站首页>第七篇,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);
}
边栏推荐
- Mujoco produces analog video
- Advanced learning of MySQL -- basics -- basic operation of transactions
- Cross-entrpy Method
- New feature of Oracle 19C: automatic DML redirection of ADG, enhanced read-write separation -- ADG_ REDIRECT_ DML
- Zynq transplant ucosiii
- C Primer Plus Chapter 14 (structure and other data forms)
- Js+svg love diffusion animation JS special effects
- uniapp实现从本地上传头像并显示,同时将头像转化为base64格式存储在mysql数据库中
- Linear algebra of deep learning
- Leecode brush question record sword finger offer 56 - ii Number of occurrences of numbers in the array II
猜你喜欢
Are you ready to automate continuous deployment in ci/cd?
Set (generic & list & Set & custom sort)
On February 19, 2021ccf award ceremony will be held, "why in Hengdian?"
Everyone is always talking about EQ, so what is EQ?
Slam d'attention: un slam visuel monoculaire appris de l'attention humaine
stm32F407-------SPI通信
基于GO语言实现的X.509证书
New feature of Oracle 19C: automatic DML redirection of ADG, enhanced read-write separation -- ADG_ REDIRECT_ DML
Stm32f407 ------- DAC digital to analog conversion
深度学习之线性代数
随机推荐
Slam d'attention: un slam visuel monoculaire appris de l'attention humaine
基于GO语言实现的X.509证书
[force buckle]41 Missing first positive number
[software reverse automation] complete collection of reverse tools
【JokerのZYNQ7020】AXI_ EMC。
【vulnhub】presidential1
Data sharing of the 835 postgraduate entrance examination of software engineering in Hainan University in 23
Leecode brushes questions and records interview questions 01.02 Determine whether it is character rearrangement for each other
Attention SLAM:一種從人類注意中學習的視覺單目SLAM
ZYNQ移植uCOSIII
Win10 startup error, press F9 to enter how to repair?
[yolov5 6.0 | 6.1 deploy tensorrt to torch serve] environment construction | model transformation | engine model deployment (detailed packet file writing method)
Telerik UI 2022 R2 SP1 Retail-Not Crack
37 page overall planning and construction plan for digital Village revitalization of smart agriculture
JS+SVG爱心扩散动画js特效
接口(接口相关含义,区别抽象类,接口回调)
How to get started and improve test development?
【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)
Advanced learning of MySQL -- Fundamentals -- four characteristics of transactions
Advanced learning of MySQL -- Fundamentals -- concurrency of transactions