当前位置:网站首页>STM32 - serial port learning notes (one byte, 16 bit data, string, array)
STM32 - serial port learning notes (one byte, 16 bit data, string, array)
2022-07-26 03:00:00 【The poorest are beggars and immortals will always come out】
One 、 Serial port related configuration
void uart_init(u32 bound){
//GPIO Port settings
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); // Can make USART1,GPIOA The clock
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9 USART1 Of RXD、TXD Pin You also need to check the data manual
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // Multiplexing push pull output These two modes need to check the data manual 8.1.11
GPIO_Init(GPIOA, &GPIO_InitStructure);// initialization GPIOA.9
//USART1_RX GPIOA.10 initialization
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;// Floating input
GPIO_Init(GPIOA, &GPIO_InitStructure);// initialization GPIOA.10
//Usart1 NVIC To configure
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;// preemption 3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; // Sub priority 3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ Channel enable
NVIC_Init(&NVIC_InitStructure); // Initialize... According to the specified parameters VIC register
//USART Initialize settings
USART_InitStructure.USART_BaudRate = bound;// Serial port baud rate
USART_InitStructure.USART_WordLength = USART_WordLength_8b;// The word is 8 Bit data format
USART_InitStructure.USART_StopBits = USART_StopBits_1;// A stop bit
USART_InitStructure.USART_Parity = USART_Parity_No;// No parity bit
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;// No hardware data flow control
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // Transceiver mode
USART_Init(USART1, &USART_InitStructure); // Initialize serial port 1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);// Open serial port to accept interrupt
USART_Cmd(USART1, ENABLE); // Enable serial port 1
}Two 、 Serial port sending data function encapsulation
1、 Send a byte
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
{
/* Send a byte of data to USART */
USART_SendData(pUSARTx,ch);
/* Wait for the send data register to be empty */
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}2、 Send a 16 Bit data
void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
{
uint8_t temp_h, temp_l;
/* Take out the top eight */
temp_h = (ch&0XFF00)>>8;
/* Take out the lower eight */
temp_l = ch&0XFF;
/* Send the upper eight digits */
USART_SendData(pUSARTx,temp_h);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
/* Send the lower eight bits */
USART_SendData(pUSARTx,temp_l);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}3、 send out 8 Bit array
Example :
#define DEBUG_USART5 UART5
uint8_t card_num_main[16];
Usart_SendArray( DEBUG_USART5,card_num_main, 16);
void Usart_SendArray( USART_TypeDef * pUSARTx, uint8_t *array, uint16_t num)
{
uint8_t i;
for(i=0; i<num; i++)
{
/* Send a byte of data to USART */
Usart_SendByte(pUSARTx,array[i]);
}
/* Wait for the send to complete */
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET);
}4、 String sending function
void HMISends(char *buf1) // String sending function
{
uint8_t i=0;
while(1)
{
if(buf1[i]!='\0')
{
USART_SendData(DEBUG_USART2,buf1[i]); // Send a byte
while(USART_GetFlagStatus(DEBUG_USART2,USART_FLAG_TXE)==RESET){};// Wait for the end of sending
i++;
}
else
return ;
}
}5、 Convert text to digital send
Serial port interrupt reception :
void USART1_IRQHandler(void) // A serial port 1 Interrupt service routine
{
u8 Res,temp;
#if SYSTEM_SUPPORT_OS // If SYSTEM_SUPPORT_OS It's true , Support is needed OS.
OSIntEnter();
#endif
Res =USART_ReceiveData(USART1); // Read received data // A byte
temp=Res-'0';
USART_RX_BUF[USART_RX_STA&0X3FFF]=temp ;
USART_RX_STA++;
if(temp==1)
LED0=0;
if(temp==2)
LED0=1;
#if SYSTEM_SUPPORT_OS // If SYSTEM_SUPPORT_OS It's true , Support is needed OS.
OSIntExit();
#endif
}
#endif
send out :
while(1)
{
len=USART_RX_STA&0x3fff;// The length of data received this time
if(len>0)
{
for(t=0;t<len;t++)
{
USART_SendData(USART1, USART_RX_BUF[t]);// Serial port 1 send data
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);// Wait for the end of sending
}
USART_RX_STA=0;
}
}6、 The serial port receives single byte data , And send the
u8 temp;
void USART1_IRQHandler(void) // A serial port 1 Interrupt service routine
{
u8 Res;
#if SYSTEM_SUPPORT_OS // If SYSTEM_SUPPORT_OS It's true , Support is needed OS.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Receive interrupt ( The data received must be 0x0d 0x0a ending )
{
Res =USART_ReceiveData(USART1); // Read received data // A byte
temp = Res;
USART_SendData(USART1, temp);// Serial port 1 send data
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);// Wait for the end of sending
}
#if SYSTEM_SUPPORT_OS // If SYSTEM_SUPPORT_OS It's true , Support is needed OS.
OSIntExit();
#endif
}
#endif
extern u8 temp;
while(1)
{
if(temp == 0x01)
{
temp=0;
LED0=1;
}
else if(temp == 0x02)
{
temp=0;
LED0=0;
}
}边栏推荐
- 软件测试岗:阿里三面,幸好做足了准备,已拿offer
- Zhimeng prompts you how to solve the problem of setting the field as linkage type
- 简单使用 MySQL 索引
- How to design automated test cases?
- AMD64(x86_64)架构abi文档:
- Article setting top
- [translation] safety. Value of sboms
- Study notes of pytorch deep learning practice: convolutional neural network (Advanced)
- Keil's operation before programming with C language
- Case: using kept+haproxy to build a Web Cluster
猜你喜欢
![[SQL] CASE表达式](/img/05/1bbb0b5099443f7ce5f5511703477e.png)
[SQL] CASE表达式

How to design test cases according to the requirements of login testing?

【方向盘】工具提效:Sublime Text 4的常用快捷键合集

Nahamcon CTF 2022 babyrev reverse analysis

Binary search 33. search rotation sort array

Annotation development

Image recognition (VI) | activation function

Win11麦克风权限的开启方法

MySQL教程:MySQL数据库学习宝典(从入门到精通)

Stack Title: the longest absolute path of a file
随机推荐
Longest Substring Without Repeating Characters
如何加速矩阵乘法
Teach you to rely on management hand in hand
Eslint common error reporting set
MySQL教程:MySQL数据库学习宝典(从入门到精通)
Machine learning foundation plan 0-2: what is machine learning? What does it have to do with AI?
How to design automated test cases?
[translation] cloud like internal load balancer for kubernetes?
(PC+WAP)织梦模板蔬菜水果类网站
【方向盘】使用IDEA的60+个快捷键分享给你,权为了提效(重构篇)
ShardingSphere数据分片
Win11大小写提示图标怎么关闭?Win11大小写提示图标的关闭方法
Shardingsphere data slicing
Study notes of pytorch deep learning practice: convolutional neural network (Advanced)
图像识别(六)| 激活函数
Application of shift distance and hypothesis
Turn on the LED
[C Advanced] deeply explore the storage of data (in-depth analysis + interpretation of typical examples)
信息系统项目管理师必背核心考点(五十)合同内容约定不明确规定
Autojs cloud control source code + display