标题:
STM32F103 VCP代码
[打印本页]
作者:
普通人0o
时间:
2019-6-5 23:59
标题:
STM32F103 VCP代码
STM32F103 实现虚拟串口,进行串口通信,实现了USB通信
单片机源程序如下:
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdio.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/*LED灯相关定义*/
#define RCC_GPIO_LED RCC_APB2Periph_GPIOD /*LED使用的GPIO时钟*/
#define LEDn 4 /*神舟III号LED数量*/
#define GPIO_LED GPIOD /*神舟III号LED灯使用的GPIO组*/
#define DS1_PIN GPIO_Pin_8 /*DS1使用的GPIO管脚*/
#define DS2_PIN GPIO_Pin_9 /*DS2使用的GPIO管脚*/
#define DS3_PIN GPIO_Pin_10 /*DS3使用的GPIO管脚*/
#define DS4_PIN GPIO_Pin_11 /*DS4使用的GPIO管脚*/
//按键的定义
#define RCC_GPIO_KEY1 RCC_APB2Periph_GPIOD /*按键1使用的GPIO时钟*/
#define GPIO_KEY1 GPIOD /*按键使用的GPIO组*/
#define KEY1_PIN GPIO_Pin_0 /*按键1使用的GPIO管脚*/
#define RCC_GPIO_KEY2 RCC_APB2Periph_GPIOD /*按键2使用的GPIO时钟*/
#define GPIO_KEY2 GPIOD /*按键2使用的GPIO组*/
#define KEY2_PIN GPIO_Pin_1 /*按键2使用的GPIO管脚*/
#define RCC_GPIO_KEY3 RCC_APB2Periph_GPIOD /*按键3使用的GPIO时钟*/
#define GPIO_KEY3 GPIOD /*按键3使用的GPIO组*/
#define KEY3_PIN GPIO_Pin_2 /*按键3使用的GPIO管脚*/
#define RCC_GPIO_KEY4 RCC_APB2Periph_GPIOD /*按键4使用的GPIO时钟*/
#define GPIO_KEY4 GPIOD /*按键4使用的GPIO组*/
#define KEY4_PIN GPIO_Pin_3 /*按键4使用的GPIO管脚*/
//串口1 TX PA9 RX-PA10
#define RCC_GPIO_UART1 RCC_APB2Periph_GPIOA /*按键4使用的GPIO时钟*/
#define GPIO_UART1 GPIOA /*按键4使用的GPIO组*/
#define TX1_PIN GPIO_Pin_9 /*按键4使用的GPIO管脚*/
#define RX1_PIN GPIO_Pin_10 /*按键4使用的GPIO管脚*/
//串口2 TX -PA2 RX-PA3
#define RCC_GPIO_USART2 RCC_APB2Periph_GPIOA /*USART2的GPIO时钟*/
#define GPIO_USART2 GPIOA /*USART2使用的GPIO组*/
#define TX3_PIN GPIO_Pin_2 /*USART2使用的GPIO管脚*/
#define RX3_PIN GPIO_Pin_3 /*USART2使用的GPIO管脚*/
/* Private macro -------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
//宏定义 为适合不同的编译系统
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/* Private variables ---------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
ErrorStatus HSEStartUpStatus;
u8 count=0;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void Delay(vu32 nCount);
void Turn_On_LED(u8 LED_NUM);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
u8 i=1;
/* 配置神舟III号LED灯使用的GPIO管脚模式*/
// RCC_DeInit();
// RCC_HSEConfig(RCC_HSE_ON);
// RCC_HSICmd(ENABLE);
// while(RCC_WaitForHSEStartUp()!=SUCCESS);
// RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_16 );
// RCC_PLLCmd(ENABLE);
//
// RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK );
//LED设置
RCC_APB2PeriphClockCmd(RCC_GPIO_LED, ENABLE); /*使能LED灯使用的GPIO时钟*/
GPIO_InitStructure.GPIO_Pin = DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_LED, &GPIO_InitStructure); /*神州III号使用的LED灯相关的GPIO口初始化*/
GPIO_ResetBits(GPIO_LED,DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN);/*关闭所有的LED指示灯*/
//按键1设置
RCC_APB2PeriphClockCmd(RCC_GPIO_KEY1, ENABLE); /*使能LED灯使用的GPIO时钟*/
GPIO_InitStructure.GPIO_Pin = KEY1_PIN;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU ;
GPIO_Init(GPIO_KEY1, &GPIO_InitStructure); /*神州III号使用的按键1相关的GPIO口初始化*/
//按键2设置
RCC_APB2PeriphClockCmd(RCC_GPIO_KEY2, ENABLE); /*使能按键2使用的GPIO时钟*/
GPIO_InitStructure.GPIO_Pin = KEY2_PIN;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU ;
GPIO_Init(GPIO_KEY2, &GPIO_InitStructure); /*神州III号使用的按键1相关的GPIO口初始化*/
//按键3设置
RCC_APB2PeriphClockCmd(RCC_GPIO_KEY3, ENABLE); /*使能按键3使用的GPIO时钟*/
GPIO_InitStructure.GPIO_Pin = KEY3_PIN;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU ;
GPIO_Init(GPIO_KEY3, &GPIO_InitStructure); /*神州III号使用的按键1相关的GPIO口初始化*/
//按键4设置
RCC_APB2PeriphClockCmd(RCC_GPIO_KEY4, ENABLE); /*使能按键4使用的GPIO时钟*/
GPIO_InitStructure.GPIO_Pin = KEY4_PIN;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU ;
GPIO_Init(GPIO_KEY4, &GPIO_InitStructure); /*神州III号使用的按键1相关的GPIO口初始化*/
//********************************************************************
//串口1端口
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd( RCC_GPIO_UART1|RCC_APB2Periph_AFIO, ENABLE);
/* Enable UART clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = TX1_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_UART1, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = RX1_PIN;
GPIO_Init(GPIO_UART1, &GPIO_InitStructure);
/*串口1参数配置*/
USART_InitStructure.USART_BaudRate = 115200; /*设置波特率为115200*/
USART_InitStructure.USART_WordLength = USART_WordLength_8b; /*设置数据位为8位*/
USART_InitStructure.USART_StopBits = USART_StopBits_1; /*设置停止位为1位*/
USART_InitStructure.USART_Parity = USART_Parity_No; /*无奇偶校验*/
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /*没有硬件流控*/
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /*发送与接收*/
/*完成串口COM1的时钟配置、GPIO配置,根据上述参数初始化并使能*/
/* USART configuration */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART1, ENABLE);
//********************************************************************
//串口2端口
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd( RCC_GPIO_USART2|RCC_APB2Periph_AFIO, ENABLE);
/* Enable UART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = TX3_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_USART2, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = RX3_PIN;
GPIO_Init(GPIO_USART2, &GPIO_InitStructure);
/*串口2参数配置*/
USART_InitStructure.USART_BaudRate = 115200; /*设置波特率为115200*/
USART_InitStructure.USART_WordLength = USART_WordLength_8b; /*设置数据位为8位*/
USART_InitStructure.USART_StopBits = USART_StopBits_1; /*设置停止位为1位*/
USART_InitStructure.USART_Parity = USART_Parity_No; /*无奇偶校验*/
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /*没有硬件流控*/
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /*发送与接收*/
/*完成串口COM1的时钟配置、GPIO配置,根据上述参数初始化并使能*/
/* USART configuration */
USART_Init(USART2, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART2, ENABLE);
while(1)
{
printf("\nSTM32F103实验中。。。。。。\n");
printf("数字是%d",i);
i++;
Delay(0x1ffFFFF);
}
}
/*点亮对应灯*/
void Turn_On_LED(u8 LED_NUM)
{
switch(LED_NUM)
{
case 0:
GPIO_ResetBits(GPIO_LED,DS1_PIN); /*点亮DS1灯*/
break;
case 1:
GPIO_ResetBits(GPIO_LED,DS2_PIN); /*点亮DS2灯*/
break;
case 2:
GPIO_ResetBits(GPIO_LED,DS3_PIN); /*点亮DS3灯*/
break;
case 3:
GPIO_ResetBits(GPIO_LED,DS4_PIN); /*点亮DS4灯*/
break;
default:
GPIO_ResetBits(GPIO_LED,DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN); /*点亮所有的灯*/
break;
}
}
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART2, (uint8_t) ch); /*发送一个字符函数*/
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)/*等待发送完成*/
{
}
return ch;
}
/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nCount: specifies the delay time length.
* Output : None
* Return : None
*******************************************************************************/
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
复制代码
所有资料51hei提供下载:
STM32F103 实现虚拟串口,进行串口通信,实现了USB通信.7z
(540.68 KB, 下载次数: 22)
2019-6-7 02:15 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
JACKRENYONG
时间:
2022-7-22 11:52
编译都没通过,还封装了一个静态LIB.
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1