当前位置:网站首页>Part 7: STM32 serial communication programming
Part 7: STM32 serial communication programming
2022-07-07 00:51:00 【Heavy vehicle water】
1. Basic concepts of communication
(1) Serial communication and parallel communication
(2) Simplex , Half duplex and full duplex
(3) Communication rate
The number of bits transmitted per unit time indicates the transmission speed , It's called baud rate (bps)
(4) Communication protocol ( A serial port )
Communication protocol is the data format agreed by both parties
2. Hardware connection of serial port
UART ----------------- Universal asynchronous transceiver
USART --------------- Universal synchronization / Asynchronous transceiver
3.stm32 Serial port development
(1) Schematic diagram
USB The debugging port is connected to CPU Of PA9 PA10, They have the function of serial port multiplexing
(2)CPU Chip manual
(3) Programming of serial interface
Add serial port library function source code in the project
1) Turn on GPIOA and USART1 The clock of
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
2) take PA9 PA10 Configured as multiplexing function , And mapped to serial port 1
GPIO_Init(...); GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
3) Initialize serial port
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct); Parameters : USARTx - Which serial port
USART_InitStruct - Serial port initialization structure
typedef struct
{
uint32_t USART_BaudRate; /*!< Baud rate */
uint16_t USART_WordLength; /*!< Data bit length @ref USART_Word_Length */
uint16_t USART_StopBits; /*!< Stop bit length @ref USART_Stop_Bits */
uint16_t USART_Parity; /*!< Verification method @ref USART_Parity */
uint16_t USART_Mode; /*!< send out / Reception mode @ref USART_Mode */
uint16_t USART_HardwareFlowControl; /*!< Hardware flow control @ref USART_Hardware_Flow_Control */ } USART_InitTypeDef;
4) Enable serial port
USART_Cmd(USART1,ENABLE);
5) Sending and receiving data
send out :
// polling
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
// What data is sent to which serial port
// Before each transmission, check whether the previous data has been sent
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
// What symbol of the serial port to be transmitted , return SET Indicates that there is this flag , return RESET Indicates that there is no such flag
//USART_FLAG_TC ----- Send completion flag
———————————————————————————————————————————
practice :
Realize serial port 1 Sending strings
———————————————————————————————————————————
// Function code
#include <stm32f4xx.h>
#include <usart.h>
#include <stdio.h>
#include <string.h>
#include <includes.h>
//stm32 Run the program on the development board , If the host runs the debugger , The program will use the input and output devices of the host
// This is called semi host mode ,printf If you want to print through serial port , Half host mode must be turned off
#pragma import(__use_no_semihosting)
struct __FILE{
int handle;
};
FILE __stdout;
// Definition _sys_exit Function avoid using semi host mode
void _sys_exit(int x)
{
x = x;
}
// redefinition fputc
int fputc(int ch,FILE *F)
{
// Wait for the last data to be sent
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
// send out
USART_SendData(USART1,ch);
return ch;
}
void usart1_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
//1. Turn on GPIOA and USART1 The clock
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
//2. To configure PA9 PA10 Serial port function
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;// Reuse mode
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;// Push pull output
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;// High speed
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;// No up and down
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. Initialize serial port 8N1
USART_InitStruct.USART_BaudRate = 115200;// Baud rate
USART_InitStruct.USART_WordLength = USART_WordLength_8b;//8 Digit bit
USART_InitStruct.USART_StopBits = USART_StopBits_1;//1 Bit stop bit
USART_InitStruct.USART_Parity = USART_Parity_No;// No verification
USART_InitStruct.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;// Send receive mode
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;// No hardware flow control
USART_Init(USART1,&USART_InitStruct);
//4. Open serial port receive interrupt ( Clear interrupt flag )
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//5. initialization NVIC
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;// A serial port 1 Interrupt channel
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x2;// preemption
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x2;// Response priority
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;// Can make
NVIC_Init(&NVIC_InitStruct);
//. Enable serial port
USART_Cmd(USART1,ENABLE);
}
// Send a character ( polling )
void uart1_putc(char ch)
{
// Wait for the last data to be sent
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
USART_SendData(USART1,ch);
}
边栏推荐
- 【批处理DOS-CMD命令-汇总和小结】-跳转、循环、条件命令(goto、errorlevel、if、for[读取、切分、提取字符串]、)cmd命令错误汇总,cmd错误
- Configuring the stub area of OSPF for Huawei devices
- 腾讯云 WebShell 体验
- Command line kills window process
- Learn self 3D representation like ray tracing ego3rt
- Zynq transplant ucosiii
- Model-Free Control
- The difference between redirectto and navigateto in uniapp
- Uniapp uploads and displays avatars locally, and converts avatars into Base64 format and stores them in MySQL database
- 【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)
猜你喜欢
Uniapp uploads and displays avatars locally, and converts avatars into Base64 format and stores them in MySQL database
ActiveReportsJS 3.1中文版|||ActiveReportsJS 3.1英文版
Five different code similarity detection and the development trend of code similarity detection
Chapter II proxy and cookies of urllib Library
【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)
建立自己的网站(17)
C9高校,博士生一作发Nature!
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
uniapp中redirectTo和navigateTo的区别
随机推荐
基於GO語言實現的X.509證書
英雄联盟|王者|穿越火线 bgm AI配乐大赛分享
Js+svg love diffusion animation JS special effects
【软件逆向-自动化】逆向工具大全
Trace tool for MySQL further implementation plan
AI超清修复出黄家驹眼里的光、LeCun大佬《深度学习》课程生还报告、绝美画作只需一行代码、AI最新论文 | ShowMeAI资讯日报 #07.06
深度学习简史(一)
Chapter II proxy and cookies of urllib Library
【批處理DOS-CMD命令-匯總和小結】-字符串搜索、查找、篩選命令(find、findstr),Find和findstr的區別和辨析
Levels - UE5中的暴雨效果
Are you ready to automate continuous deployment in ci/cd?
Jenkins' user credentials plug-in installation
以机房B级建设标准满足等保2.0三级要求 | 混合云基础设施
Explain in detail the implementation of call, apply and bind in JS (source code implementation)
Telerik UI 2022 R2 SP1 Retail-Not Crack
基于GO语言实现的X.509证书
St table
Equals() and hashcode()
Data sharing of the 835 postgraduate entrance examination of software engineering in Hainan University in 23
mongodb客户端操作(MongoRepository)