找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1812|回复: 1
收起左侧

STM32F103 VCP代码

[复制链接]
ID:521626 发表于 2019-6-5 23:59 | 显示全部楼层 |阅读模式
STM32F103 实现虚拟串口,进行串口通信,实现了USB通信

单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f10x.h"
  3. #include <stdio.h>
  4. /* Private typedef -----------------------------------------------------------*/

  5. /* Private define ------------------------------------------------------------*/

  6. /*LED灯相关定义*/
  7. #define RCC_GPIO_LED                    RCC_APB2Periph_GPIOD    /*LED使用的GPIO时钟*/
  8. #define LEDn                            4                       /*神舟III号LED数量*/
  9. #define GPIO_LED                        GPIOD                  /*神舟III号LED灯使用的GPIO组*/

  10. #define DS1_PIN                         GPIO_Pin_8              /*DS1使用的GPIO管脚*/
  11. #define DS2_PIN                         GPIO_Pin_9                                /*DS2使用的GPIO管脚*/
  12. #define DS3_PIN                         GPIO_Pin_10                          /*DS3使用的GPIO管脚*/
  13. #define DS4_PIN                         GPIO_Pin_11                                /*DS4使用的GPIO管脚*/

  14. //按键的定义

  15. #define RCC_GPIO_KEY1                    RCC_APB2Periph_GPIOD    /*按键1使用的GPIO时钟*/
  16. #define GPIO_KEY1                        GPIOD                  /*按键使用的GPIO组*/
  17. #define KEY1_PIN                         GPIO_Pin_0              /*按键1使用的GPIO管脚*/

  18. #define RCC_GPIO_KEY2                    RCC_APB2Periph_GPIOD    /*按键2使用的GPIO时钟*/
  19. #define GPIO_KEY2                        GPIOD                  /*按键2使用的GPIO组*/
  20. #define KEY2_PIN                         GPIO_Pin_1              /*按键2使用的GPIO管脚*/

  21. #define RCC_GPIO_KEY3                    RCC_APB2Periph_GPIOD    /*按键3使用的GPIO时钟*/
  22. #define GPIO_KEY3                        GPIOD                  /*按键3使用的GPIO组*/
  23. #define KEY3_PIN                         GPIO_Pin_2              /*按键3使用的GPIO管脚*/

  24. #define RCC_GPIO_KEY4                    RCC_APB2Periph_GPIOD    /*按键4使用的GPIO时钟*/
  25. #define GPIO_KEY4                        GPIOD                  /*按键4使用的GPIO组*/
  26. #define KEY4_PIN                         GPIO_Pin_3              /*按键4使用的GPIO管脚*/


  27. //串口1                TX PA9 RX-PA10
  28. #define RCC_GPIO_UART1                                        RCC_APB2Periph_GPIOA    /*按键4使用的GPIO时钟*/
  29. #define GPIO_UART1                       GPIOA                  /*按键4使用的GPIO组*/
  30. #define TX1_PIN                         GPIO_Pin_9              /*按键4使用的GPIO管脚*/
  31. #define RX1_PIN                         GPIO_Pin_10              /*按键4使用的GPIO管脚*/

  32. //串口2         TX -PA2  RX-PA3
  33. #define RCC_GPIO_USART2                                        RCC_APB2Periph_GPIOA    /*USART2的GPIO时钟*/
  34. #define GPIO_USART2                      GPIOA                  /*USART2使用的GPIO组*/
  35. #define TX3_PIN                         GPIO_Pin_2              /*USART2使用的GPIO管脚*/
  36. #define RX3_PIN                         GPIO_Pin_3              /*USART2使用的GPIO管脚*/
  37. /* Private macro -------------------------------------------------------------*/

  38. /* Private function prototypes -----------------------------------------------*/
  39. //宏定义 为适合不同的编译系统
  40. #ifdef __GNUC__
  41.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  42.      set to 'Yes') calls __io_putchar() */
  43.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  44. #else
  45.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  46. #endif /* __GNUC__ */

  47. /* Private variables ---------------------------------------------------------*/
  48. GPIO_InitTypeDef GPIO_InitStructure;
  49. USART_InitTypeDef USART_InitStructure;
  50. ErrorStatus HSEStartUpStatus;
  51. u8 count=0;

  52. /* Private function prototypes -----------------------------------------------*/
  53. void RCC_Configuration(void);
  54. void NVIC_Configuration(void);
  55. void Delay(vu32 nCount);
  56. void Turn_On_LED(u8 LED_NUM);

  57. /* Private functions ---------------------------------------------------------*/

  58. /*******************************************************************************
  59. * Function Name  : main
  60. * Description    : Main program.
  61. * Input          : None
  62. * Output         : None
  63. * Return         : None
  64. *******************************************************************************/
  65. int main(void)
  66. {
  67.         u8 i=1;
  68.         /* 配置神舟III号LED灯使用的GPIO管脚模式*/
  69.        
  70.         //        RCC_DeInit();
  71.         //        RCC_HSEConfig(RCC_HSE_ON);
  72.         //        RCC_HSICmd(ENABLE);
  73.         //        while(RCC_WaitForHSEStartUp()!=SUCCESS);
  74.         //        RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_16 );
  75.         //        RCC_PLLCmd(ENABLE);
  76.         //
  77.         //        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK );
  78.         //LED设置
  79.         RCC_APB2PeriphClockCmd(RCC_GPIO_LED, ENABLE); /*使能LED灯使用的GPIO时钟*/
  80.        
  81.         GPIO_InitStructure.GPIO_Pin = DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN;
  82.         GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP ;
  83.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  84.        
  85.         GPIO_Init(GPIO_LED, &GPIO_InitStructure);  /*神州III号使用的LED灯相关的GPIO口初始化*/
  86.         GPIO_ResetBits(GPIO_LED,DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN);/*关闭所有的LED指示灯*/
  87.        
  88.         //按键1设置
  89.         RCC_APB2PeriphClockCmd(RCC_GPIO_KEY1, ENABLE); /*使能LED灯使用的GPIO时钟*/
  90.        
  91.         GPIO_InitStructure.GPIO_Pin = KEY1_PIN;
  92.         GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU ;
  93.         GPIO_Init(GPIO_KEY1, &GPIO_InitStructure);  /*神州III号使用的按键1相关的GPIO口初始化*/
  94.        
  95.         //按键2设置
  96.         RCC_APB2PeriphClockCmd(RCC_GPIO_KEY2, ENABLE); /*使能按键2使用的GPIO时钟*/
  97.        
  98.         GPIO_InitStructure.GPIO_Pin = KEY2_PIN;
  99.         GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU ;
  100.         GPIO_Init(GPIO_KEY2, &GPIO_InitStructure);  /*神州III号使用的按键1相关的GPIO口初始化*/
  101.        
  102.         //按键3设置
  103.         RCC_APB2PeriphClockCmd(RCC_GPIO_KEY3, ENABLE); /*使能按键3使用的GPIO时钟*/
  104.        
  105.         GPIO_InitStructure.GPIO_Pin = KEY3_PIN;
  106.         GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU ;
  107.         GPIO_Init(GPIO_KEY3, &GPIO_InitStructure);  /*神州III号使用的按键1相关的GPIO口初始化*/
  108.        
  109.         //按键4设置
  110.         RCC_APB2PeriphClockCmd(RCC_GPIO_KEY4, ENABLE); /*使能按键4使用的GPIO时钟*/
  111.        
  112.         GPIO_InitStructure.GPIO_Pin = KEY4_PIN;
  113.         GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU ;
  114.         GPIO_Init(GPIO_KEY4, &GPIO_InitStructure);  /*神州III号使用的按键1相关的GPIO口初始化*/
  115.        
  116.         //********************************************************************
  117.         //串口1端口
  118.         /* Enable GPIO clock */
  119.         RCC_APB2PeriphClockCmd( RCC_GPIO_UART1|RCC_APB2Periph_AFIO, ENABLE);
  120.        
  121.         /* Enable UART clock */
  122.        
  123.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  124.        
  125.         /* Configure USART Tx as alternate function push-pull */
  126.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  127.         GPIO_InitStructure.GPIO_Pin = TX1_PIN;
  128.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  129.         GPIO_Init(GPIO_UART1, &GPIO_InitStructure);
  130.        
  131.         /* Configure USART Rx as input floating */
  132.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  133.         GPIO_InitStructure.GPIO_Pin = RX1_PIN;
  134.         GPIO_Init(GPIO_UART1, &GPIO_InitStructure);
  135.        
  136.         /*串口1参数配置*/
  137.         USART_InitStructure.USART_BaudRate = 115200;                 /*设置波特率为115200*/
  138.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;  /*设置数据位为8位*/
  139.         USART_InitStructure.USART_StopBits = USART_StopBits_1;       /*设置停止位为1位*/
  140.         USART_InitStructure.USART_Parity = USART_Parity_No;          /*无奇偶校验*/   
  141.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /*没有硬件流控*/
  142.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;      /*发送与接收*/
  143.         /*完成串口COM1的时钟配置、GPIO配置,根据上述参数初始化并使能*/
  144.         /* USART configuration */
  145.         USART_Init(USART1, &USART_InitStructure);
  146.                 /* Enable USART */
  147.         USART_Cmd(USART1, ENABLE);

  148.         //********************************************************************
  149.         //串口2端口
  150.         /* Enable GPIO clock */
  151.         RCC_APB2PeriphClockCmd( RCC_GPIO_USART2|RCC_APB2Periph_AFIO, ENABLE);
  152.        
  153.         /* Enable UART clock */
  154.        
  155.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  156.        
  157.         /* Configure USART Tx as alternate function push-pull */
  158.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  159.         GPIO_InitStructure.GPIO_Pin = TX3_PIN;
  160.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  161.         GPIO_Init(GPIO_USART2, &GPIO_InitStructure);
  162.        
  163.         /* Configure USART Rx as input floating */
  164.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  165.         GPIO_InitStructure.GPIO_Pin = RX3_PIN;
  166.         GPIO_Init(GPIO_USART2, &GPIO_InitStructure);


  167.         /*串口2参数配置*/
  168.         USART_InitStructure.USART_BaudRate = 115200;                 /*设置波特率为115200*/
  169.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;  /*设置数据位为8位*/
  170.         USART_InitStructure.USART_StopBits = USART_StopBits_1;       /*设置停止位为1位*/
  171.         USART_InitStructure.USART_Parity = USART_Parity_No;          /*无奇偶校验*/   
  172.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /*没有硬件流控*/
  173.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;      /*发送与接收*/
  174.         /*完成串口COM1的时钟配置、GPIO配置,根据上述参数初始化并使能*/
  175.         /* USART configuration */
  176.         USART_Init(USART2, &USART_InitStructure);
  177.                 /* Enable USART */
  178.         USART_Cmd(USART2, ENABLE);


  179.         while(1)
  180.         {
  181.                 printf("\nSTM32F103实验中。。。。。。\n");
  182.                 printf("数字是%d",i);
  183.                 i++;
  184.                 Delay(0x1ffFFFF);
  185.           }
  186. }

  187. /*点亮对应灯*/
  188. void Turn_On_LED(u8 LED_NUM)
  189. {
  190.         switch(LED_NUM)
  191.         {
  192.         case 0:
  193.           GPIO_ResetBits(GPIO_LED,DS1_PIN);  /*点亮DS1灯*/
  194.           break;
  195.         case 1:
  196.           GPIO_ResetBits(GPIO_LED,DS2_PIN);  /*点亮DS2灯*/
  197.           break;
  198.         case 2:
  199.           GPIO_ResetBits(GPIO_LED,DS3_PIN);  /*点亮DS3灯*/
  200.           break;
  201.         case 3:
  202.           GPIO_ResetBits(GPIO_LED,DS4_PIN);  /*点亮DS4灯*/
  203.           break;         
  204.         default:
  205.                   GPIO_ResetBits(GPIO_LED,DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN); /*点亮所有的灯*/
  206.           break;
  207.         }
  208. }


  209. PUTCHAR_PROTOTYPE
  210. {
  211.   /* Place your implementation of fputc here */
  212.   /* e.g. write a character to the USART */
  213.   USART_SendData(USART2, (uint8_t) ch); /*发送一个字符函数*/

  214.   /* Loop until the end of transmission */
  215.   while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)/*等待发送完成*/
  216.   {
  217.   
  218.   }
  219.   return ch;
  220. }



  221. /*******************************************************************************
  222. * Function Name  : Delay
  223. * Description    : Inserts a delay time.
  224. * Input          : nCount: specifies the delay time length.
  225. * Output         : None
  226. * Return         : None
  227. *******************************************************************************/
  228. void Delay(vu32 nCount)
  229. {
  230.   for(; nCount != 0; nCount--);
  231. }



  232. /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
STM32F103 实现虚拟串口,进行串口通信,实现了USB通信.7z (540.68 KB, 下载次数: 22)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:370824 发表于 2022-7-22 11:52 | 显示全部楼层
编译都没通过,还封装了一个静态LIB.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表