本文整理汇总了C++中USART_Init函数的典型用法代码示例。如果您正苦于以下问题:C++ USART_Init函数的具体用法?C++ USART_Init怎么用?C++ USART_Init使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了USART_Init函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: USART1_Init
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);
}
示例2: console_init
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");
}
示例3: USART3_Init
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);
}
示例4: USART3_Configuration
/*
*@功能: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); /*接收中断使能*/
}
示例5: USART1_Config
/**
* @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);
}
示例6: init_usart
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;
}
示例7: USART_Config
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);
}
示例8: STM_EVAL_COMInit
/**
* @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);
}
示例9: COMInit
/**
* 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);
}
}
示例10: STM_EVAL_COMInit
/**
* @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);
}
示例11: UART_init
//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);
}
示例12: BoardInit
/**************************************************************************//**
*
* @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);
}
示例13: Init_USART
/******************************************************************************
* タイトル : 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); //割り込み設定
}
示例14: init_usart
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
}
示例15: stm32_configure
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;
}