STM32 USB学习笔记2

本文介绍了如何使用STM32Cube库和STM32_USB_Device_Library在STM32F103C8T6上实现USB转串口功能。通过CDC_Standalone例程,实现了USB虚拟串口(COM6),并与实际串口(COM5)进行双向通信。硬件上,USB设备使用1.5K上拉电阻,不需要额外IO控制。代码分析包括物理硬件处理和USB设备连接状态初始化的调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主机环境:Windows 7 SP1

开发环境:MDK5.14

目标板:STM32F103C8T6

开发库:STM32F1Cube库和STM32_USB_Device_Library


STM32Cube库中提供了一些有关USB的例程,在其工程目录下的Applications目录中,这里打开STM3210E_EVAL目录,可以看到如下例程:


这里选取一个简单的例子CDC_Standalone,为一个USB通信例程,具体实现是一个USB转串口的功能,相当于USB串口线。把示例里面的inc和src目录下的文件拷贝到新建工程中,这里是把跟usb通信相关的文件放到了vcp目录下。文件目录结构如下:


其中BSP目录很简单,由于所购买的单板只用到了USB,UART模块以及一个LED灯,原理图如下:


其中PA15引脚连接了一个LED灯,如下:


因此在stm32f103_demo文件中只添加了led的操作,源文件如下:

<pre name="code" class="cpp">/**
  ******************************************************************************
  * @file    stm32f103_demo.c
  * @author  MCD Application Team
  * @version V6.0.0
  * @date    13-October-2015
  * @brief   This file provides a set of firmware functions to manage Leds, 
  *          for STM32F103_DEMO
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBU
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值