文章目录
一、通过CubeMX配置项目
1、设置RCC
2、设置SYS
3、设置USART
4、设置NVIC
5、创建项目
二、中断方式
1、在main函数前定义全局变量
char c;//指令 0:停止 1:开始
char message[]="hello Windows\n";//输出信息
char tips[]="CommandError\n";//提示1
char tips1[]="Start.....\n";//提示2
char tips2[]="Stop......\n";//提示3
int flag=0;//标志 0:停止发送 1.开始发送
2、在main函数中设置接收中断函数
HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
HAL_UART_Receive_IT(&huart1, (uint8_t *)&c, 1);
功能:串口中断接收,以中断方式接收指定长度数据。
大致过程是,设置数据存放位置,接收数据长度,然后使能串口接收中断。
接收到数据时,会触发串口中断。
再然后,串口中断函数处理,直到接收到指定长度数据
而后关闭中断,进入中断接收回调函数,不再触发接收中断。(只触发一次中断)
3、main函数中的while循环里面添加传输代码
if(flag==1){
//发送信息
HAL_UART_Transmit(&huart1, (uint8_t *)&message, strlen(message),0xFFFF);
//延时
HAL_Delay(1000);
}
5、main函数全部代码
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 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 "usart.h"
#include "gpio.h"
#include "string.h"
char getBuffer[100];
char value;
char str1[] = "start";
char str2[] = "stop";
char message[]="hello Windows\n";
char tips[]="CommandError\n";
char tips1[]="Start.....\n";
char tips2[]="Stop......\n";
int flag=1;
int countofGetBuffer=0;
/* 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 */
/**ep
* @brief The application entry point.
* @retval int
*/
#define COUNTOF(a) (sizeof(a)/sizeof(*(a)))
//jkl
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
HAL_UART_Receive_IT(&huart1, (uint8_t *)&value, 1);
//HAL_UART_Receive_IT(&huart1, (uint8_t *)&c, 10);
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(flag==1){
//
HAL_UART_Transmit(&huart1, (uint8_t *)&message,COUNTOF(message),0xFFFF);
//
HAL_Delay(1000);
}else if(flag==0){
HAL_Delay(1000);
}
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart1){
HAL_UART_IRQHandler(huart1);
getBuffer[countofGetBuffer++]=value;
if(strcmp(getBuffer,str1)==0){
flag = 1;
HAL_UART_Transmit(huart1, (uint8_t *)&tips1, COUNTOF(tips1),0xFFFF);
countofGetBuffer = 0;
memset(getBuffer,0,COUNTOF(getBuffer));
}else if(strcmp(getBuffer,str2)==0){
flag = 0;
HAL_UART_Transmit(huart1, (uint8_t *)&tips2, COUNTOF(tips2),0xFFFF);
countofGetBuffer = 0;
memset(getBuffer,0,COUNTOF(getBuffer));
}
HAL_UART_Receive_IT(huart1, (uint8_t *)&value,1);
}
void USART1_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart1); //??oˉêy?á?????D??±ê??£?è????D??ê1?ü£?2¢???óμ÷ó???μ÷oˉêy
HAL_UART_Receive_IT(&huart1, (uint8_t *)getBuffer,10); //ìí?óμ?ò?DD′ú??
}
/**
* @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_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
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_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != 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 */
6、编译并烧录
三、DMA方式
2STM32CubeMX 配置 DMA
rcc配置
串口配置
设置 DMA
时钟配置
输出设置
添加如下代码
/* USER CODE BEGIN Init /
uint8_t Senbuff[] = "\n\r\0 Congratulations, you did it! "; //定义数据发送数组
/ USER CODE END Init */
到main函数
while (1)
{
/* USER CODE END WHILE */
HAL_UART_Transmit_DMA(&huart1, (uint8_t )Senbuff, sizeof(Senbuff));
HAL_Delay(1000);
/ USER CODE BEGIN 3 */
}到while循环
编译为hex文件
3烧录并进行串口调试
3.1烧录
3.2串口调试
————————————————
版权声明:本文为CSDN博主「guiqulaix」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/guiqulaix/article/details/121165663
四、结果
五、输出波形
五、总结
过硬件连接、引入库文件、初始化串口、发送数据和接收数据等步骤,可以实现STM32与外部设备之间的简单串口通信。这为后续更复杂的串口通信应用奠定了基础。