×
单片机 > 单片机程序设计 > 详情

STM32F103RC USART2串口查询方式接收数据

发布时间:2020-06-18 发布时间:
|

现有一个小需求,使用STM32F1系列单片机做串口2的收发数据的功能,通过PC上的串口调试助手给单片机发一串数据,单片机收到数据后再给PC的串口调试助手发回去。


看似简单的功能,调试了好久才调好,记录一下供大家参考。


STM32使用USART2,对应单片机的PA1控制方向,PA2发送,PA3接收。


代码如下:


z_hardware_usart2.h


#ifndef __STM32F10X_H

#define __STM32F10X_H

#include "stm32f10x.h"

#endif

 

#ifndef __Z_UTIL_TIME_H

#define __Z_UTIL_TIME_H

#include "z_util_time.h"

#endif

 

#ifndef _UART_BUF_LEN_512

#define _UART_BUF_LEN_512 512

#endif

 

#ifndef _UART_BUF_LEN_256

#define _UART_BUF_LEN_256 256

#endif

 

#ifndef _UART_COMM_CH_TIMEOUT_3MS

#define _UART_COMM_CH_TIMEOUT_3MS 3

#endif

 

#ifndef _UART_COMM_CH_TIMEOUT_2MS

#define _UART_COMM_CH_TIMEOUT_2MS 2

#endif

 

#ifndef _UART_COMM_INT_TIMEOUT_300MS

#define _UART_COMM_INT_TIMEOUT_300MS 300

#endif

 

#ifndef _UART_COMM_INT_TIMEOUT_500MS

#define _UART_COMM_INT_TIMEOUT_500MS 500

#endif

 

void init_hardware_usart2(u32 bound);

void func_usart2_send_a_byte(u8 abyte);

u8 func_usart2_recv_a_byte(u8 *recv_data);

void func_usart2_send_bytes(u8 *bytes, u8 bytes_len);

u8 func_usart2_recv_bytes(const u16 BUF_MAX_LEN, u8 *recv_datas, u16 *recv_datas_len, const u16 int_timeout_ms, const u8 ch_timeout_ms);

z_hardware_usart2.c


#ifndef __Z_HARDWARE_USART2_H

#define __Z_HARDWARE_USART2_H

#include "z_hardware_usart2.h"

#endif

 

void init_hardware_usart2(u32 bound)

{

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate = bound;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

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(USART2, &USART_InitStructure);

USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);

USART_Cmd(USART2, ENABLE);

USART_ClearFlag(USART2, USART_FLAG_TC);

GPIO_ResetBits(GPIOA, GPIO_Pin_1);

}

 

void func_usart2_send_a_byte(u8 abyte)

{

while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);

USART_SendData(USART2, abyte);

while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);

}

 

u8 func_usart2_recv_a_byte(u8 *recv_data)

{

if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET)

{

*recv_data = USART_ReceiveData(USART2);

USART_ClearFlag(USART2, USART_FLAG_RXNE);

return 0;

}

return 1;

}

 

void func_usart2_send_bytes(u8 *bytes, u8 bytes_len)

{

u8 i;

GPIO_SetBits(GPIOA, GPIO_Pin_1);

for(i = 0; i < bytes_len; i++)

{

func_usart2_send_a_byte(bytes[i]);

}

GPIO_ResetBits(GPIOA, GPIO_Pin_1);

}

 

u8 func_usart2_recv_bytes(const u16 BUF_MAX_LEN, u8 *recv_datas, u16 *recv_datas_len, const u16 int_timeout_ms, const u8 ch_timeout_ms)

{

u8 flag_recv;

u8 tmp_recv;

u16 tmp_int_timeout_cnt = 0;

u8 tmp_ch_timeout_cnt = 0;

u16 len_recv = 0;

u8 *p_recv;

p_recv = recv_datas;

GPIO_ResetBits(GPIOA, GPIO_Pin_1);

for(;func_usart2_recv_a_byte(&tmp_recv) != 0;)

{

tmp_int_timeout_cnt ++;

if(tmp_int_timeout_cnt > int_timeout_ms * 10)

{

return 1;

}

delay_us(100);

}

len_recv ++;

*p_recv = tmp_recv;

p_recv ++;

for(;;)

{

flag_recv = func_usart2_recv_a_byte(&tmp_recv);

if(flag_recv == 0)//data recv

{

tmp_ch_timeout_cnt = 0;

len_recv ++;

*p_recv = tmp_recv;

p_recv ++;

}

else //data not recv

{

tmp_ch_timeout_cnt ++;

delay_us(800);//FIXME 适用9600波特率。波特率不同需要针对性修改

}

if(tmp_ch_timeout_cnt > ch_timeout_ms)

{

*recv_datas_len = len_recv;

return 0;

}

if(len_recv > BUF_MAX_LEN - 1)

{

*recv_datas_len = BUF_MAX_LEN - 1;

return 2;

}

}

}

z_util_time.h


#ifndef __STM32F10X_H

#define __STM32F10X_H

#include "stm32f10x.h"

#endif

 

#define AHB_INPUT 72

 

void delay_us(uint32_t time_us);

void delay_ms(uint32_t time_ms);

z_util_time.c


#ifndef __Z_UTIL_TIME_H

#define __Z_UTIL_TIME_H

#include "z_util_time.h"

#endif

 

void delay_us(uint32_t time_us)

{

SysTick->LOAD = AHB_INPUT * time_us;

SysTick->VAL = 0x00;

SysTick->CTRL = 0x00000005;

for(;!(SysTick->CTRL & 0x00010000););

SysTick->CTRL = 0x00000004;

}

 

void delay_ms(uint32_t time_ms)

{

for(;time_ms-- > 0;)

{

delay_us(1000);

}

}

main.c


#include "stm32f10x.h"

 

#ifndef __Z_HARDWARE_USART2_H

#define __Z_HARDWARE_USART2_H

#include "z_hardware_usart2.h"

#endif

 

#include

 

int main()

{

u8 to_recv[_UART_BUF_LEN_256];

u16 to_recv_len;

init_hardware_usart2(9600);

while (1)

{

if(func_usart2_recv_bytes(_UART_BUF_LEN_256, to_recv, &to_recv_len, _UART_COMM_INT_TIMEOUT_500MS, _UART_COMM_CH_TIMEOUT_3MS) == 0)

{

func_usart2_send_bytes(to_recv, to_recv_len);

}

to_recv_len = 0;

memset(to_recv, 0, sizeof(to_recv));

}

}

效果如下:

经过测试发现,串口调试助手给单片机发送了1W+多次,失误率为0,准确率可以应用。

测试代码,仅供参考。


关键字:STM32F103RC  USART2串口  查询方式  接收数据 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
STM32中断向量表的位置.重定向