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
|