一.中断方式
1.程序设计流程
(1)处代码
if(huart->Instance == USART1&&strcmp((char *)RxBuffer,"stop")==0){
RxFlag = 1;
HAL_UART_Receive_IT(huart, (uint8_t*)&RxBuffer, LENGTH);
}
(2)处代码
else if(huart->Instance == USART1&&strcmp((char *)RxBuffer,"star")==0&&flag==1){
flag = 0;
HAL_UART_Receive_IT(huart, (uint8_t*)&ch, 1);
}
(3)处代码
if(huart->Instance == USART1 && ch == 't' && flag == 0){
RxFlag = 0;
flag = 1;
ch = '0';
HAL_UART_Receive_IT(huart, (uint8_t*)&RxBuffer, LENGTH);
}
2. 工程创建
同前面工程创建相同参考:
https://blog.csdn.net/zhuzhUzzzZAGHndu/article/details/134082075?spm=1001.2014.3001.5502
3.代码修改:
3.1 添加头文件
#include "stdio.h"
#include "string.h"
3.2 添加变量
uint8_t RxBuffer[LENGTH]; //字符缓冲区,LENGTH定义为4
uint8_t RxFlag = 0;//是否接受到stop
uint8_t ch;//额外字符't'的缓冲区
uint8_t flag = 1;//是否需要继续接收额外字符
3.3 修改while循环
while (1)
{
/* USER CODE END WHILE */
if(RxFlag == 0){
HAL_UART_Transmit(&huart1,(uint8_t*)"hello windows!\r\n",16,0xffff);
HAL_Delay(1000);
}
/* USER CODE BEGIN 3 */
}
3.4添加中断循环
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
if(huart->Instance == USART1&&strcmp((char *)RxBuffer,"stop")==0){
RxFlag = 1;
HAL_UART_Receive_IT(huart, (uint8_t*)&RxBuffer, LENGTH);
}else if(huart->Instance == USART1&&strcmp((char *)RxBuffer,"star")==0&&flag==1){
flag = 0;
HAL_UART_Receive_IT(huart, (uint8_t*)&ch, 1);
}
if(huart->Instance == USART1 && ch == 't' && flag == 0){
RxFlag = 0;
flag = 1;
ch = '0';
HAL_UART_Receive_IT(huart, (uint8_t*)&RxBuffer, LENGTH);
}
}
4.运行效果
5.波形图
二.DMA方式
1. 工程创建
RCC等其他配置同前面相同,但多了DMA分配
2. 代码实现
main.c代码:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"
#include <stdio.h>
#include <string.h>
//#include "stm32f1xx_hal.c"
extern DMA_HandleTypeDef hdma_usart1_tx;
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
uint8_t order[5]="";//下位机接收的start 和stop命令
//uint8_t *order="";//用这个会报错,因为strcmp函数
uint8_t *sendData ="feifei hello windows!\n";
uint8_t flag=1;//标志 0:停止发送 1.开始发送
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_USART1_UART_Init();
//接收命令的函数,接收后产生空闲中断进入中断服务函数HAL_UARTEx_RxEventCallback
HAL_UARTEx_ReceiveToIdle_DMA(&huart1,order,5);
while (1)
{
if (flag)
{
HAL_UART_Transmit_DMA(&huart1, sendData, strlen(sendData)); //以DMA模式给上位机发数据
}
HAL_Delay(1000);
}
}
//关于串口空闲中断的回调函数
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
if(huart==&huart1)
{
HAL_UART_Transmit_DMA(&huart1, order, strlen(order));//下位机接收命令
}
if(!strcmp(order, "start"))
{
flag = 1;
}
if(!strcmp(order, "stop!"))
{
flag = 0;
}
//继续接收命令 重新设置中断
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, order, strlen(order));
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
main.h
#define LENGTH 100
extern uint8_t RxFlag;//是否接受到stop
extern uint8_t RxBuffer[LENGTH];//接收缓冲区
extern uint8_t RxCount;//接收数,等于LENGTH-已经接受到的
//因为这些变量在另一个c文件中调用了,所以要设置成外部变量,否则会因为一些莫名其妙覆盖
stm32f1xx_it.c
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE END USART1_IRQn 0 */
/* USER CODEBEGIN USART1_IRQn 1 */
if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET){
HAL_UART_DMAStop(&huart1);
__HAL_UART_CLEAR_FEFLAG(&huart1);
if(strcmp((char *)RxBuffer, "stop")==0){
RxFlag = 1;
}else if(strcmp((char *)RxBuffer, "start")==0){
RxFlag = 0;
}
//HAL_UART_Transmit_DMA(&huart1, (uint8_t*)RxBuffer, strlen((char *)RxBuffer));
memset(RxBuffer,'\0',LENGTH);//
HAL_UART_Receive_DMA(&huart1, (uint8_t*)RxBuffer, LENGTH);
}
/* USER CODE END USART1_IRQn 1 */
}
3.结果实现
发送start开始
stop停止
4.波形图
5. 注释
5.1 串口协议定义了数据传输格式,RS-232标准规定了串口电平。这为串口通信奠定了基础。
5.2 正确设置串口波特率、数据位、停止位和校验位等参数是实现通信的关键。