1 ///**************************************/// 2 //author zq&jp // 3 //net zq-jp.cnblogs.com // 4 //qq 627334696 // 5 //blog zq-jp.cnblogs.com // 6 ///**************************************/// 7 8 #ifndef __USART_4_H 9 #define __USART_4_H 10 11 #include "stm32f10x.h" 12 #include "stm32f10x_usart.h" 13 #include "misc.h" 14 #include "stdarg.h" 15 16 void USART4_GPIO_Configuration(void); 17 void USART4_NVIC_Configuration(void); 18 19 void USART4_Config(USART_TypeDef* USARTx); 20 21 22 23 #endif
配置程序
1 ///**************************************/// 2 //author zq&jp // 3 //net zq-jp.cnblogs.com // 4 //qq 627334696 // 5 //blog zq-jp.cnblogs.com // 6 ///**************************************/// 7 #include "USART_4.h" 8 9 10 /* Private variables ---------------------------------------------------------*/ 11 12 uint8_t TxBuffer4[] = "USART Interrupt Example: This is USART4 DEMO"; 13 uint8_t RxBuffer4[]; 14 __IO uint8_t TxCounter4 = 0x00; 15 __IO uint8_t RxCounter4 = 0x00; 16 17 18 static GPIO_InitTypeDef GPIO_InitStructure; 19 static USART_InitTypeDef USART_InitStructure; 20 21 22 /**************************************************************************** 23 * 名 稱:USART_Config(USART_TypeDef* USARTx) 24 * 功 能:配置串口 25 * 入口參數: 26 * 出口參數:無 27 * 說 明: 28 * 調用方法:例如: USART_Config(USART1) 29 ****************************************************************************/ 30 void USART4_Config(USART_TypeDef* USARTx){ 31 32 RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); 33 34 USART_InitStructure.USART_BaudRate = 115200; //速率115200bps 35 USART_InitStructure.USART_WordLength = USART_WordLength_8b; //數據位8位 36 USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位1位 37 USART_InitStructure.USART_Parity = USART_Parity_No; //無校驗位 38 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //無硬件流控 39 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發模式 40 41 /* Configure UART4 */ 42 USART_Init(USARTx, &USART_InitStructure); //配置串口參數函數 43 44 45 /* Enable USART1 Receive and Transmit interrupts */ 46 // USART_ClearFlag(UART4,USART_IT_RXNE); 47 // USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); //使能接收中斷 48 // USART_ITConfig(UART4, USART_IT_TXE, ENABLE); //使能發送緩沖空中斷 49 50 /* Enable the UART4 */ 51 USART_Cmd(UART4, ENABLE); 52 } 53 54 55 /**************************************************************************** 56 * 名 稱:void GPIO_Configuration(void) 57 * 功 能:通用IO口配置 58 * 入口參數:無 59 * 出口參數:無 60 * 說 明: 61 * 調用方法: 62 ****************************************************************************/ 63 void USART4_GPIO_Configuration(void) 64 { 65 66 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); 67 68 /* 默認復用功能 */ 69 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART4 TX 70 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出 71 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 72 GPIO_Init(GPIOC, &GPIO_InitStructure); //A端口 73 /* 復用功能的輸入引腳必須配置為輸入模式(浮空/上拉/下拉的一種)*/ 74 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //USART4 RX 75 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //復用浮空輸入 76 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 77 GPIO_Init(GPIOC, &GPIO_InitStructure); //A端口 78 79 // //485使能端 80 // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //USART5 RX 81 // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出 82 // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 83 // GPIO_Init(GPIOD, &GPIO_InitStructure); //D端口 84 85 } 86 87 /**************************************************************************** 88 * 名 稱:void NVIC_Configuration(void) 89 * 功 能:中斷源配置 90 * 入口參數:無 91 * 出口參數:無 92 * 說 明: 93 * 調用方法:無 94 ****************************************************************************/ 95 void USART4_NVIC_Configuration(void) 96 { 97 /* 結構聲明*/ 98 NVIC_InitTypeDef NVIC_InitStructure; 99 100 /* Configure the NVIC Preemption Priority Bits */ 101 /* Configure one bit for preemption priority */ 102 /* 優先級組 說明了搶占優先級所用的位數,和響應優先級所用的位數 在這里是0, 4 103 0組: 搶占優先級占0位, 響應優先級占4位 104 1組: 搶占優先級占1位, 響應優先級占3位 105 2組: 搶占優先級占2位, 響應優先級占2位 106 3組: 搶占優先級占3位, 響應優先級占1位 107 4組: 搶占優先級占4位, 響應優先級占0位 108 */ 109 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); 110 111 NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; //設置串口4中斷 112 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //搶占優先級 0 113 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子優先級為0 114 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能 115 NVIC_Init(&NVIC_InitStructure); 116 117 }
