当前位置: 首页 > news >正文

APM/STM32F072RB基于HAL库配置USB CDC虚拟串口功能

APM/STM32F072RB基于HAL库配置USB CDC虚拟串口功能


  • 📢采用的自制开发板,开源PCB工程详情放在《极海APM32F072RB开发环境测试》
  • ✨本案例基于STM32CubeMX工具配置。
  • 📺使用STM32CubeMX工具配置工程改为APMF072RB型号过程如下:
    在这里插入图片描述

⛳注意事项

  • ⚡配置的时候将Stack_SizeHeap_Size修改大一点值,避免在使用USB CDC虚拟串口功能不够,导致初始化失败,电脑端加载不出虚拟串口。
  • 🔰在调用CDC_Transmit_FS()打印调试信息的地方包含#include "usbd_cdc_if.h"
  • 如果是使用在极海APM32072RB上,在STM32CubeMX配置完工程后,在Keil设置里,将型号修改为极海APM32072RB型号,否则烧录会报错。不支持直接选择默认的STM32F072RB型号进行烧录。
    在这里插入图片描述
  • 🍁使用USB CDC虚拟串口功能,在没有USB转串口工具的情况下,可以很方便调试。
    在这里插入图片描述

📝主程序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 "usb_device.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usbd_cdc_if.h"
/* 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 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
unsigned char buff[12] = {"hello world"};//添加的输出内容
  /* 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_USB_DEVICE_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_9);
		  HAL_Delay(1000);//加个ms单位延时
		CDC_Transmit_FS(buff,sizeof(buff));//添加的输出内容
		
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;

  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != 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 */

  • 📜串口监视器调试信息输出
    在这里插入图片描述

📚工程案例源码

  • 🌼Keil工程里已经将型号修改为APM32F072RB了。
链接:https://pan.baidu.com/s/1xJlYoSlwqCsuXfx2pRadng 
提取码:doxo

相关文章:

  • 做新闻网站服务器选购/最好看免费观看高清大全
  • 电影的网站做他妈的没完没了没了吗/sem优化师是什么意思
  • wordpress添加主题不显示图片/站长seo软件
  • 网站问卷调查系统怎么做/百度怎么推广自己的信息
  • 直播网站开发源码下载/百度网盘客服电话
  • 网站开发方式有哪四种/网页推广怎么做的
  • 传输层协议:UDP协议
  • Minecraft 1.19.2 Forge模组开发 10.3D动画盔甲
  • 第12届蓝桥杯青少组选拔赛6月c++高级
  • 小红书KOL营销策略,先搞清楚小红书kol分类有哪些
  • 结对编程踩坑指南
  • 浅说 INSERT/块参照/BlockReference
  • 基于Android的健身app
  • 美团滑块(1-18,js逆向)
  • 【蓝桥杯】历届真题 分果果(省赛)Java
  • 为何香港的IB状元特别多?
  • 贪心策略(五)主持人调度(一、二)
  • MySQL表的增删改查(初级)