void USART1_Init(uint32_t speed) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* Connect PXx to USARTx_Tx */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_7); /* Connect PXx to USARTx_Rx */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_7); /* Configure USART Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure USART Rx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_Init(GPIOB, &GPIO_InitStructure); /* USART configuration */ USART_InitStructure.USART_BaudRate = speed; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART1, &USART_InitStructure); /* Enable USART */ USART_Cmd(USART1, ENABLE); }
void console_init() { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* USARTx configured as follow: - BaudRate = 38400 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = CFG_BAUD_RATE; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_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; /* Configure USARTx_Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_TxPin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USARTx_Rx as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_RxPin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_Init(USART1,&USART_InitStructure); USART_Cmd(USART1, ENABLE); console_ops.write=console_write; console_ops.read=console_read; console_ops.config=console_config; console_ops.open=NULL; acoral_drv_register(&console_ops,"console"); }
void USART3_Init() { GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIO clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); /* Connect PXx to USARTx_Tx*/ GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); /* Connect PXx to USARTx_Rx*/ GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); /* Configure USART Tx as alternate function */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); /* Configure USART Rx as alternate function */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_Init(GPIOD, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = 38400; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_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; USART_Init(USART3, &USART_InitStructure); USART_Cmd(USART3, ENABLE); }
/* *@功能:uart3的配置 * 波特率:9600 * 数据长度:8位 * 停止位:1位 * 无奇偶校验 * 无硬件流控制 * 全双工 *@说明:可以根据需求更改 */ void USART3_Configuration(void) { USART_InitTypeDef USART_InitStructure; /* USART3 configuration ------------------------------------------------------*/ /* USART3 configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled - USART Clock disabled - USART CPOL: Clock is active low - USART CPHA: Data is captured on the middle - USART LastBit: The clock pulse of the last data bit is not output to the SCLK pin */ USART_InitStructure.USART_BaudRate =9600;//115200;//4800;//9600; //115200; //9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_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; //下面是同步模式下才用的 //USART_InitStructure.USART_Clock = USART_Clock_Disable; //USART_InitStructure.USART_CPOL = USART_CPOL_Low; //USART_InitStructure.USART_CPHA = USART_CPHA_2Edge; //USART_InitStructure.USART_LastBit = USART_LastBit_Disable; //USART_StructInit(USART_InitStructure); USART_Init(USART3, &USART_InitStructure); /* Enable USART3 */ USART_Cmd(USART3, ENABLE); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); /*接收中断使能*/ }
/** * @brief USART1 GPIO 配置,工作模式配置。115200 8-N-1 * @param 无 * @retval 无 */ void USART1_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* config USART1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); /* USART1 GPIO config */ /* Configure USART1 Tx (PA.09) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1 Rx (PA.10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /* USART1 mode config */ USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_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; USART_Init(USART1, &USART_InitStructure); /* 使能串口1接收中断 */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); /* 配置串口中断优先级 */ NVIC_Configuration(); USART_Cmd(USART1, ENABLE); }
int init_usart(void) { // Enable clocks RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // Configure GPIO pins GPIO_InitTypeDef usart_rx, usart_tx; usart_rx.GPIO_Speed = GPIO_Speed_50MHz; usart_rx.GPIO_Mode = GPIO_Mode_AF_PP; usart_rx.GPIO_Pin = GPIO_Pin_9; usart_tx.GPIO_Speed = GPIO_Speed_50MHz; usart_tx.GPIO_Mode = GPIO_Mode_IN_FLOATING; usart_tx.GPIO_Pin = GPIO_Pin_10; // Init GPIO pins GPIO_Init(GPIOA, &usart_rx); GPIO_Init(GPIOA, &usart_tx); // Configure USART USART_InitTypeDef usart; usart.USART_BaudRate = 9600; usart.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; usart.USART_WordLength = USART_WordLength_8b; usart.USART_StopBits = USART_StopBits_1; usart.USART_Parity = USART_Parity_No; usart.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // Init USART USART_Init(USART1, &usart); // Start USART USART_Cmd(USART1, ENABLE); return 0; }
void USART_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); // sort out clocks RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* Configure USART2 Tx (PA.02) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); // Map USART2 to A.02 GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); // Initialize USART USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx; /* Configure USART */ USART_Init(USART2, &USART_InitStructure); /* Enable the USART */ USART_Cmd(USART2, ENABLE); USART_SendData(USART2,0xFF); }
/** * @brief Configures COM port. * @param COM: Specifies the COM port to be configured. * This parameter can be one of following parameters: * @arg COM1 * @arg COM2 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure that * contains the configuration information for the specified USART peripheral. * @retval None */ void STM_EVAL_COMInit(COM_TypeDef COM, USART_InitTypeDef* USART_InitStruct) { GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIO clock */ RCC_APB2PeriphClockCmd(COM_TX_PORT_CLK[COM] | COM_RX_PORT_CLK[COM] | RCC_APB2Periph_AFIO, ENABLE); /* Enable UART clock */ if (COM == COM1) { RCC_APB2PeriphClockCmd(COM_USART_CLK[COM], ENABLE); } else { /* Enable the USART2 Pins Software Remapping */ GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); RCC_APB1PeriphClockCmd(COM_USART_CLK[COM], ENABLE); } /* Configure USART Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = COM_TX_PIN[COM]; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(COM_TX_PORT[COM], &GPIO_InitStructure); /* Configure USART Rx as input floating */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = COM_RX_PIN[COM]; GPIO_Init(COM_RX_PORT[COM], &GPIO_InitStructure); /* USART configuration */ USART_Init(COM_USART[COM], USART_InitStruct); /* Enable USART */ USART_Cmd(COM_USART[COM], ENABLE); }
/** * Configures the specified COM port. * * @param COM COM_TypeDef Specifies the COM port to be configured. * @param USART_InitStruct USART_InitTypeDef* Pointer to a USART_InitTypeDef structure that * contains the configuration information for the specified USART peripheral. * @retval none */ void COMInit(COM_TypeDef COM, USART_InitTypeDef* USART_InitStruct) { if (COM == COM1 || COM == COM2) { GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIO clock */ RCC_AHB1PeriphClockCmd(COM_TX_PORT_CLK[COM] | COM_RX_PORT_CLK[COM], ENABLE); /* Enable UART clock */ RCC_APB1PeriphClockCmd(COM_USART_CLK[COM], ENABLE); /* Connect PXx to USARTx_Tx*/ GPIO_PinAFConfig(COM_TX_PORT[COM], COM_TX_PIN_SOURCE[COM], COM_TX_AF[COM]); /* Connect PXx to USARTx_Rx*/ GPIO_PinAFConfig(COM_RX_PORT[COM], COM_RX_PIN_SOURCE[COM], COM_RX_AF[COM]); /* Configure USART Tx as alternate function */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = COM_TX_PIN[COM]; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(COM_TX_PORT[COM], &GPIO_InitStructure); /* Configure USART Rx as alternate function */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = COM_RX_PIN[COM]; GPIO_Init(COM_RX_PORT[COM], &GPIO_InitStructure); /* USART configuration */ USART_Init(COM_USART[COM], USART_InitStruct); /* Enable USART */ USART_Cmd(COM_USART[COM], ENABLE); } }
/** * @brief Configures COM port. * @param COM: Specifies the COM port to be configured. * This parameter can be one of following parameters: * @arg COM1 * @arg COM2 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure that * contains the configuration information for the specified USART peripheral. * @retval None */ static void STM_EVAL_COMInit(COM_TypeDef COM, USART_InitTypeDef* USART_InitStruct) { GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIO clock */ //RCC_APB2PeriphClockCmd(COM_TX_PORT_CLK[COM] | COM_RX_PORT_CLK[COM] | RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // if (COM == COM1) // { // /* Enable the USART2 Pins Software Remapping */ // GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); // RCC_APB1PeriphClockCmd(COM_USART_CLK[COM], ENABLE); // } /* Configure USART Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Pin = COM_TX_PIN[COM]; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(COM_TX_PORT[COM], &GPIO_InitStructure); /* Configure USART Rx as input floating */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_Pin = COM_RX_PIN[COM]; GPIO_Init(COM_RX_PORT[COM], &GPIO_InitStructure); /* USART configuration */ USART_Init(COM_USART[COM], USART_InitStruct); /* Enable USARTy Receive and Transmit interrupts */ USART_ITConfig(COM_USART[COM], USART_IT_RXNE, ENABLE); //USART_ITConfig(COM_USART[COM1], USART_IT_TXE, ENABLE); /* Enable USART */ USART_Cmd(COM_USART[COM], ENABLE); }
//Function to intialise the UART to allow transmission to PC and enable interrupts to detect messages receievd from PC void UART_init(void) { USART_InitTypeDef USART_InitStructure; //Enable periph clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //Configure required pins as UART TX/RX GPIO_Config(); //NVIC_Config(); //USART configuration values USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8 bit bytes USART_InitStructure.USART_StopBits = USART_StopBits_1; //one stop bit USART_InitStructure.USART_Parity = USART_Parity_No; //no parity bit USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //No hardware flow control //USART_InitStructure.USART_Mode = USART_Mode_Tx; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Enable USART for transmit and receive USART_Init(USART1, &USART_InitStructure); //Enable interrupts for RX USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //USART_ITConfig(USART2, USART_IT_TXE, ENABLE); USART_ClearITPendingBit(USART1, USART_IT_RXNE); //Conigure Nested Vectored INterrupt Controller to accept USART inerrupts NVIC_Config(); //Clear TXE flag (which indicates when transmit buffer is empty) USART_ClearFlag(USART1, USART_FLAG_TXE); //Enable UART USART_Cmd(USART1, ENABLE); }
/**************************************************************************//** * * @brief Initialize board * *****************************************************************************/ void BoardInit(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; Pin_Init(); LED_CLK(); BSP_LedSet(0); GPIO_InitStructure.GPIO_Pin = LED_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(LED_PORT, &GPIO_InitStructure); SystemCoreClockUpdate(); TimingInit(); PC_GPIO_CLK(); GPIO_InitStructure.GPIO_Pin = PC_PIN_RX; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(PC_PIN_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = PC_PIN_TX; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(PC_PIN_PORT, &GPIO_InitStructure); PC_UART_CLK(); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_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; USART_Init(PC_UART, &USART_InitStructure); USART_Cmd(PC_UART, ENABLE); }
/****************************************************************************** * タイトル : USART設定 * 関数名 : Init_USART * 引数1 : USART_TypeDef型 *USARTx USART番号 * 引数2 : unsigned int型 baudrate ボーレート * 引数3 : GPIO_TypeDef型 *GPIOx_TX TXポート * 引数4 : uint16_t型 pin_TX TXピン * 引数5 : GPIO_TypeDef型 *GPIOx_RX RXポート * 引数6 : uint16_t型 pin_RX RXピン * 作成者 : 石井岳史 * 作成日 : 2014/11/12 ******************************************************************************/ void Init_USART(USART_TypeDef *USARTx,unsigned int baudrate, GPIO_TypeDef *GPIOx_TX ,uint16_t pin_TX, GPIO_TypeDef *GPIOx_RX, uint16_t pin_RX){ //構造体変数を宣言 USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; //モジュールストップ状態の解除 // RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_PeriphClock_USART(USARTx); //GPIOの設定 Init_port(GPIO_Mode_AF,GPIOx_TX,pin_TX,GPIO_PuPd_NOPULL,GPIO_OType_PP); Init_port(GPIO_Mode_AF,GPIOx_RX,pin_RX,GPIO_PuPd_NOPULL,GPIO_OType_PP); //PINをオルタネィテブファンクションに割り当て GPIO_PinAFConfig(GPIOx_TX, Pin_select_source(pin_TX), GPIO_af_USART_select(USARTx));//USART1 TX/PB6 GPIO_PinAFConfig(GPIOx_RX, Pin_select_source(pin_RX), GPIO_af_USART_select(USARTx));//USART1 RX/PB7 //USART1の設定 USART_InitStructure.USART_BaudRate = baudrate; //ボーレートの設定 USART_InitStructure.USART_WordLength = USART_WordLength_8b; //ビット長 USART_InitStructure.USART_StopBits = USART_StopBits_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; //送信受信の選択 USART_Init(USARTx, &USART_InitStructure); //USART1の設定 USART_Cmd(USARTx, ENABLE); //USART1周辺回路の有効化 USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE); //USART1周辺回路の割込み有効化 //割り込み設定 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //優先度のビット設定 NVIC_InitStructure.NVIC_IRQChannel = USART_irqn_select(USARTx); //有効化するIRQチャンネルの指定 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //割り込みの優先順位(グループ)の指定。0が最優先 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //割り込みの優先順位(サブ)の指定。0が最優先 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //割り込みの有効化 NVIC_Init(&NVIC_InitStructure); //割り込み設定 }
void init_usart(uint32_t baudrate) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* enable peripheral clock for USART2 */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* GPIOA clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* GPIOA Configuration: USART2 TX on PA2 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Connect USART2 pins to AF2 */ // TX = PA2 // RX = PA3 GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); USART_InitStructure.USART_BaudRate = baudrate; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure); USART_Cmd(USART2, ENABLE); // enable USART2 }
static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg) { struct stm32_uart* uart; USART_InitTypeDef USART_InitStructure; RT_ASSERT(serial != RT_NULL); RT_ASSERT(cfg != RT_NULL); uart = (struct stm32_uart *)serial->parent.user_data; if (cfg->baud_rate == BAUD_RATE_4800) USART_InitStructure.USART_BaudRate = 4800; else if (cfg->baud_rate == BAUD_RATE_9600) USART_InitStructure.USART_BaudRate = 9600; else if (cfg->baud_rate == BAUD_RATE_115200) USART_InitStructure.USART_BaudRate = 115200; if (cfg->data_bits == DATA_BITS_8) USART_InitStructure.USART_WordLength = USART_WordLength_8b; if (cfg->stop_bits == STOP_BITS_1) USART_InitStructure.USART_StopBits = USART_StopBits_1; else if (cfg->stop_bits == STOP_BITS_2) USART_InitStructure.USART_StopBits = USART_StopBits_2; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(uart->uart_device, &USART_InitStructure); /* Enable USART */ USART_Cmd(uart->uart_device, ENABLE); /* enable interrupt */ USART_ITConfig(uart->uart_device, USART_IT_RXNE, ENABLE); return RT_EOK; }
void uart_init(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* 开启GPIO_D的时钟 */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE); /* 开启串口3的时钟 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_Init(GPIOG, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; GPIO_Init(GPIOG, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOG, GPIO_PinSource9, GPIO_AF_USART6); GPIO_PinAFConfig(GPIOG, GPIO_PinSource14, GPIO_AF_USART6); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART6, &USART_InitStructure); /* 使能串口 */ USART_Cmd(USART6, ENABLE); }
static void usart_init(USART_TypeDef *usart) { if (usart == USART1) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); } else if (usart == USART2) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); } else if (usart == USART3) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); } USART_InitTypeDef init; init.USART_BaudRate = 115200; init.USART_WordLength = USART_WordLength_8b; init.USART_StopBits = USART_StopBits_1; init.USART_Parity = USART_Parity_No; init.USART_Mode = USART_Mode_Tx; init.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(usart, &init); USART_ITConfig(usart, USART_IT_RXNE, DISABLE); USART_Cmd(usart, ENABLE); }
void FDTI_Start() { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; // Start USART2 clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // GPIOA Configuration for USART2 (TX on PA2) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_Init(GPIOA, &GPIO_InitStructure); // Connect the TX pin (PA2) to USART2 alternative function GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); // Setup the properties of USART2 USART_InitStructure.USART_BaudRate = 256000; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure); // Enable USART2 USART_Cmd(USART2, ENABLE); }
/**************************************************************************** * 名 称:void Usart1_Init(void) * 功 能:串口1初始化函数 * 入口参数:无 * 出口参数:无 * 说 明: * 调用方法:无 ****************************************************************************/ void Usart1_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //LCD背光控制 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_ResetBits(GPIOD, GPIO_Pin_13); //LCD背光关闭 RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 , ENABLE); //使能串口1时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 TX GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure); //A端口 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1 RX GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //复用开漏输入 GPIO_Init(GPIOA, &GPIO_InitStructure); //A端口 USART_InitStructure.USART_BaudRate = 115200; //速率115200bps 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; //收发模式 /* Configure USART1 */ USART_Init(USART1, &USART_InitStructure); //配置串口参数函数 /* Enable the USART1 */ USART_Cmd(USART1, ENABLE); }
void USART1_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_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; USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 10; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART1->SR; }
/** * @brief Configures COM port. * @param COM: Specifies the COM port to be configured. * This parameter can be one of following parameters: * @arg COM1 * @arg COM2 * @param USART_InitStruct: pointer to a USART_InitTypeDef structure that * contains the configuration information for the specified USART peripheral. * @retval None */ void STM_EVAL_COMInit(COM_TypeDef COM, USART_InitTypeDef* USART_InitStruct) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* Enable the USARTx Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EVAL_COM1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable GPIO clock */ RCC_AHB1PeriphClockCmd(COM_TX_PORT_CLK[COM] | COM_RX_PORT_CLK[COM], ENABLE); /* Enable UART clock */ RCC_APB2PeriphClockCmd(COM_USART_CLK[COM], ENABLE); /* Connect PXx to USARTx_Tx*/ GPIO_PinAFConfig(COM_TX_PORT[COM], COM_TX_PIN_SOURCE[COM], COM_TX_AF[COM]); /* Connect PXx to USARTx_Rx*/ GPIO_PinAFConfig(COM_RX_PORT[COM], COM_RX_PIN_SOURCE[COM], COM_RX_AF[COM]); /* Configure USART Tx as alternate function */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = COM_TX_PIN[COM]; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(COM_TX_PORT[COM], &GPIO_InitStructure); /* Configure USART Rx as alternate function */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = COM_RX_PIN[COM]; GPIO_Init(COM_RX_PORT[COM], &GPIO_InitStructure); /* USART configuration */ USART_Init(COM_USART[COM], USART_InitStruct); /* Enable USART */ USART_Cmd(COM_USART[COM], ENABLE); USART_ITConfig(EVAL_COM1, USART_IT_RXNE, ENABLE); }
void USART1_Configuration(void) { USART_InitTypeDef USART_InitStructure; _GPIO_Configuration(); /* USART resources configuration (Clock, GPIO pins and USART registers) ----*/ /* USART configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 38400; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; #if defined(FLOW_CONTROL) USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS; #else USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; #endif USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* USART configuration */ USART_Init(USART1, &USART_InitStructure); /* Enable USART */ USART_Cmd(USART1, ENABLE); // USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; }
////////////////////////////////////////// //tyh:用于和 AI采样芯片通信 void COM3_Configuration(uint32_t BaudRate) { USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = BaudRate; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_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; USART_Init(USART3, &USART_InitStructure); DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE); //设置发送完成中断 //USART_ITConfig(USART3, /*USART_IT_ORE*/USART_IT_RXNE, ENABLE); //设置ORE中断 0x0360 USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE); //启用DMA传输 USART_DMACmd(USART3, USART_DMAReq_Rx , ENABLE); USART_Cmd(USART3, ENABLE); //如下语句解决第1个字节无法正确发送出去的问题 USART_GetFlagStatus(USART3, USART_FLAG_TC); // 清标志 }
//extern volatile uint8_t *twirBuffer; //extern volatile uint8_t *twiwBuffer; int main(void) { uint16_t strom; ADC_Init(); strom = ReadCurrent(1.5); LED_DDR |= LED; //Setter pinnen som LED'en er koblet til som utgang. GPS_SLEEP_DDR |= GPS_SLEEP; //Setter pinnen som GPS-signalet ON/OFF er koblet til som utgang. GPS_SLEEP_PORT &= ~GPS_SLEEP; //Sikrer at GPS'en ikke går i hibernate-state. char GPSstr[128]={0xFF}; //char datum_conf[]="$PSRF106,65,*13\r\n"; //char output_conf[]="$PSRF100,1,4800,8,1,0*0D\r\n"; //char lla_nav_init[]="$PSRF104,63.42842,10.38901,30,0,170340,1043,20,1*1C\r\n"; //char lla_nav_init[]="$PSRF104,63.42842,10.38901,30,0,170340,1043,20,1*2A\r\n"; //char nav_init[]="$PSRF101, USART_Init(MYUBRR); // Initialiserer USART som skal kommunisere med GPS'en //USART_Send_Str(lla_nav_init); //USART_Send_Str(output_conf); TW_Slave_Init(TWI_OWN_ADRESS); //Initialiserer TWI som skal kommunisere med hovedprosessoren sei(); //Enable Global Interrupt for(;;) { GPS_Get_Str(GPSstr); TW_Write(GPS_Data.FixTime,sizeof(GPS_Data)); } return 0; }
static void init_usart(serial_t *obj) { USART_TypeDef *usart = (USART_TypeDef *)(obj->uart); USART_InitTypeDef USART_InitStructure; USART_Cmd(usart, DISABLE); USART_InitStructure.USART_BaudRate = obj->baudrate; USART_InitStructure.USART_WordLength = obj->databits; USART_InitStructure.USART_StopBits = obj->stopbits; USART_InitStructure.USART_Parity = obj->parity; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; if (obj->pin_rx == NC) { USART_InitStructure.USART_Mode = USART_Mode_Tx; } else if (obj->pin_tx == NC) { USART_InitStructure.USART_Mode = USART_Mode_Rx; } else { USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; } USART_Init(usart, &USART_InitStructure); USART_Cmd(usart, ENABLE); }
/* *@功能:uart2 的配置 * 波特率:9600 * 数据长度:8位 * 停止位:1位 * 无奇偶校验 * 无硬件流控制 * 全双工 *@说明:配置成这样是RFID模块确定的 */ void USART2_Configuration(void) { USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_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; //同步通信时用的 //USART_InitStructure.USART_Clock = USART_Clock_Disable; //USART_InitStructure.USART_CPOL = USART_CPOL_Low; //USART_InitStructure.USART_CPHA = USART_CPHA_2Edge; //USART_InitStructure.USART_LastBit = USART_LastBit_Disable; //USART_StructInit(USART_InitStructure); USART_Init(USART2, &USART_InitStructure); /* Enable USART2 */ USART_Cmd(USART2, ENABLE); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); /*接收中断使能*/ }
/******************************************************************************* * Function Name : USART_Config_Default. * Description : configure the EVAL_COM1 with default values. * Input : None. * Return : None. *******************************************************************************/ void USART_Config_Default(void) { /* EVAL_COM1 default configuration */ /* EVAL_COM1 configured as follow: - BaudRate = 9600 baud - Word Length = 8 Bits - One Stop Bit - Parity Odd - Hardware flow control desabled - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_Odd; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* Configure and enable the USART */ USART_Init(EVAL_COM1, &USART_InitStructure); /* Enable the USART Receive interrupt */ USART_ITConfig(EVAL_COM1, USART_IT_RXNE, ENABLE); }
void USART_Configuration(void) { /** Set the USART_CR2 Clock enable(CLKEN) : enable the CK bit or not Clock polarity(CPOL) : polarity of the clock output on the CK pin in synchronour mode. It works in conjunction with the CPHA bit to produce the desired clock/data relationship. CPOL_Low : Stedady low value on CK pin outside transmission window. Clock phase (CPHA) : phase output on the CK pin in synchronous mode. CPHA_2Edge : the second clock transition is the first data capture edge Last bit clock pulse(LBCL) : select whether the clock pulse associated with the MSB has to be output on the CK pin in synchronous mode. The last bit is the 8th or 9th data bit depending on the M bit in USART_CR1 register. **/ USART_ClockInitTypeDef USART_ClockInitStructure; USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; USART_ClockInit(USART1,&USART_ClockInitStructure); USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 9600; //Set the USART_BRR automatically USART_InitStructure.USART_WordLength = USART_WordLength_8b; //Set the USART_CR1 M bit USART_InitStructure.USART_StopBits = USART_StopBits_1; //Set the USART_CR2 STOP[0:1] USART_InitStructure.USART_Parity = USART_Parity_No; //Set the USART_CR1 PCE, PS USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //Set the USART_CR3 RTSE, CTSE USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); }
void USART1_Configuration(void) { /* USART1 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); USART_InitTypeDef USART_InitStructure; /* USARTx configuration ------------------------------------------------------*/ /* USARTx configured as follow: * - BaudRate = 115200 baud * - Word Length = 8 Bits * - One Stop Bit * - No parity * - Hardware flow control disabled (RTS and CTS signals) * - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 921600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_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; USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); }
//Configures the serial port hardware to function with interrupts, just like other serial ports //Baud 115200, 8-N-1 //Only Tx, no RX active void debug_config() { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); /* Configure USART Tx as alternate function */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 10; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx; USART_Init(USART3, &USART_InitStructure); USART_Cmd(USART3, ENABLE); USART_ITConfig(USART3, USART_IT_TXE, ENABLE); }