void Usart_Configuration(void) //配置Usart1 Tx->PA2 Rx->PA3 { GPIO_InitTypeDef GPIO_InitStructure; //GPIO库函数结构体 USART_InitTypeDef USART_InitStructure;//USART库函数结构体 USART_ClockInitTypeDef USART_ClockInitStructure; //使能串口1,GPIOA,AFIO总线 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO|RCC_APB2Periph_USART1,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); /* Configure USART1 Tx (PA9) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//PA9时钟速度50MHz GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用输出 GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1 Rx (PA10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入 GPIO_Init(GPIOA, &GPIO_InitStructure); 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; //发送、接收使能 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;//最后一位数据的时钟脉冲不从SCLK输出 //使能串口1、2接收中断 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_ClockInit(USART1, &USART_ClockInitStructure); USART_ClockInit(USART2, &USART_ClockInitStructure); USART_Init(USART1, &USART_InitStructure); //初始化结构体 USART_Init(USART2, &USART_InitStructure); //初始化结构体 // USART_ClearFlag(USART1,USART_FLAG_TC);//清除发送完成标志,不然有时可能会有第一个字符发送不出去的情况 // USART_ClearFlag(USART2,USART_FLAG_TC);//清除发送完成标志,不然有时可能会有第一个字符发送不出去的情况 USART_GetFlagStatus(USART1, USART_FLAG_TC);//清除发送完成标志,不然有时可能会有第一个字符发送不出去的情况 USART_GetFlagStatus(USART2, USART_FLAG_TC);//清除发送完成标志,不然有时可能会有第一个字符发送不出去的情况 InitQueue(&USART1Buffer);//USART2缓冲区初始化 USART_Cmd(USART1, ENABLE); //使能串口1 USART_Cmd(USART2, ENABLE); //使能串口2 }
/*USART3*/ void USART3_Configuration(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // B10 --T3X GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; // B11 --R3X GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &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_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(USART3, &USART_ClockInitStructure); USART_Init(USART3, &USART_InitStructure); USART_Cmd(USART3, ENABLE); USART_ITConfig(USART3,USART_IT_RXNE,ENABLE); //usart3 interrupt enable }
/*! \fn void USART2_Configuration(void) * \brief void USART2_Configuration(void) * prepared for incommer device commands. */ void USART2_Configuration(void) { RCC_APB1PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB1Periph_USART2, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // A2 --T2X GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // A3 --R2X 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; //stm32 偶校验时必须要9位 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_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(USART2, &USART_ClockInitStructure); USART_Init(USART2, &USART_InitStructure); USART_Cmd(USART2, ENABLE); USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); }
void USART_Configuration(USART_TypeDef* USART,u32 bps,u16 databit,u16 paritybit) { USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; USART_DeInit(USART); if(USART==USART1)RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); if(USART==USART2)RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); if(USART==USART3)RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); USART_InitStructure.USART_BaudRate = bps; USART_InitStructure.USART_WordLength = databit; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = paritybit ; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; USART_ClockInitStructure.USART_CPOL = USART_CPOL_High; USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; USART_Init(USART, &USART_InitStructure); USART_ClockInit(USART, &USART_ClockInitStructure); if(USART==USART1){ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); NVIC_Configuration(USART1_IRQChannel,0); } USART_Cmd(USART, ENABLE); }
void BoardConsoleInit() { RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); GPIO_InitTypeDef gpiodef; gpiodef.GPIO_Pin = GPIO_Pin_9; gpiodef.GPIO_Mode = GPIO_Mode_AF_PP; gpiodef.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &gpiodef); gpiodef.GPIO_Pin = GPIO_Pin_10; gpiodef.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &gpiodef); USART_InitTypeDef usartdef; usartdef.USART_BaudRate = 115200; usartdef.USART_HardwareFlowControl = USART_HardwareFlowControl_None; usartdef.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; usartdef.USART_WordLength = USART_WordLength_8b; usartdef.USART_StopBits = USART_StopBits_1; usartdef.USART_Parity = USART_Parity_No; USART_Init(USART1, &usartdef); USART_ClockInitTypeDef clockdef; USART_ClockStructInit(&clockdef); USART_ClockInit(USART1, &clockdef); USART_Cmd(USART1, ENABLE); }
/* ******************************************************************************** ** 函数名称 : USART1_Configuration(void) ** 函数功能 : 串口1初始化 ** 输 入 : 无 ** 输 出 : 无 ** 返 回 : 无 ******************************************************************************** */ void USART1_Configuration(void) { USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 |RCC_APB2Periph_USART1, ENABLE ); 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; // 最后一位数据的时钟脉冲不从SCLK输出 /* Configure the USART1 synchronous paramters */ USART_ClockInit(USART1, &USART_ClockInitStructure); // 时钟参数初始化设置 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; // 发送使能+接收使能 /* Configure USART1 basic and asynchronous paramters */ USART_Init(USART1, &USART_InitStructure); /* Enable USART1 */ USART_ClearFlag(USART1, USART_IT_RXNE); //清中断,以免一启用中断后立即产生中断 USART_ITConfig(USART1,USART_IT_RXNE, ENABLE); //使能USART1中断源 USART_Cmd(USART1, ENABLE); //USART1总开关:开启 }
void Usart4Init(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; //Set USART2 Tx (PA.02) as AF push-pull GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); //Set USART2 Rx (PA.03) as input floating GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOC, &GPIO_InitStructure); USART_ClockStructInit(&USART_ClockInitStructure); USART_ClockInit(UART4, &USART_ClockInitStructure); 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_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //Write USART2 parameters USART_Init(UART4, &USART_InitStructure); //Enable UART4 Receive interrupt USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); //Enable USART2 USART_Cmd(UART4, ENABLE); }
void USART3_Config(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); USART_ClockInitTypeDef USART_ClockInitStructure; GPIO_InitTypeDef UARTTX; UARTTX.GPIO_Pin = GPIO_Pin_10; UARTTX.GPIO_Mode = GPIO_Mode_AF_PP; UARTTX.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitTypeDef UARTRX; UARTRX.GPIO_Pin = GPIO_Pin_11; UARTRX.GPIO_Mode = GPIO_Mode_IPU; UARTRX.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &UARTTX); GPIO_Init(GPIOB, &UARTRX); USART_ClockStructInit(&USART_ClockInitStructure); USART_ClockInit(USART3, &USART_ClockInitStructure); USART_InitTypeDef UART3; UART3.USART_BaudRate = 9600; UART3.USART_WordLength = USART_WordLength_8b; UART3.USART_StopBits = USART_StopBits_1; UART3.USART_Parity = USART_Parity_No; UART3.USART_HardwareFlowControl = USART_HardwareFlowControl_None; UART3.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART3, &UART3); USART_Cmd(USART3, ENABLE); USART3_NVIC_Configuration(); }
void USARTxinteInit(USART_TypeDef* USARTx,uint32_t baudRate) { USART_InitTypeDef USART_InitStruct; USART_ClockInitTypeDef USART_ClockInitStruct ; USART_InitStruct.USART_BaudRate=baudRate; USART_InitStruct.USART_WordLength=USART_WordLength_8b; USART_InitStruct.USART_StopBits=USART_StopBits_1; USART_InitStruct.USART_Parity=USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode =USART_Mode_Rx|USART_Mode_Tx; USART_Init(USARTx,&USART_InitStruct) ; USART_ClockInitStruct.USART_Clock=USART_Clock_Disable; USART_ClockInitStruct.USART_CPOL= USART_CPOL_Low; USART_ClockInitStruct.USART_CPHA=USART_CPHA_2Edge; USART_ClockInitStruct.USART_LastBit=USART_LastBit_Disable; USART_ClockInit(USARTx,&USART_ClockInitStruct); USART_Cmd(USARTx,ENABLE); USART_ITConfig(USARTx,USART_IT_RXNE,ENABLE); }
void USART_Configuration(USART_TypeDef *USARTx, uint32_t baud, uint16_t stopbit, uint16_t parity) { USART_InitTypeDef USART_InitStructure; 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; /* Configure the USARTx synchronous paramters */ USART_ClockInit(USARTx, &USART_ClockInitStructure); USART_InitStructure.USART_BaudRate = baud; //115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = stopbit;//USART_StopBits_1; USART_InitStructure.USART_Parity = parity;//USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* Configure USARTx basic and asynchronous paramters */ USART_Init(USARTx, &USART_InitStructure); /* Enable USARTx Receive and Transmit interrupts */ USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE); USART_ITConfig(USARTx, USART_IT_TC, DISABLE); USART_ITConfig(USARTx, USART_IT_TXE, ENABLE); /* Enable USARTx */ USART_Cmd(USARTx, ENABLE); }
/** * @brief USART GPIO 配置,中断配置,工作模式配置。115200 8-N-1 * @param 无 * @retval 无 */ void USART_Config(unsigned int baudRate){ /* * 1. 使能 USART1 的时钟。 * 2. 配置 USART1 的 I/O。 * 3. 配置 USART1 的工作模式,具体为波特率为115200、8个数据位、1个停止位、无硬件流控制(115200 8-N-1)。 */ USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStruct; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure;// Configure the NVIC Preemption Priority Bits // Configure USART1 clock,在使用复用功能的时候,要开启相应的功能时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); // UASRT GPIO configure // Configure Tx(PA9) 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 Rx(PA10) as as input floating GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); // Configure USART1 Mode USART_InitStructure.USART_BaudRate = baudRate; // 波特率配置 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; // 硬件流控制失能 // 配置双线全双工通信,需要把 Rx 和 Tx 模式都开启 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 配置 USART1 时钟 USART_ClockInitStruct.USART_Clock = USART_Clock_Disable; // 时钟低电平活动 USART_ClockInitStruct.USART_CPOL = USART_CPOL_Low; // SLCK 引脚上时钟输出的极性->低电平 USART_ClockInitStruct.USART_CPHA = USART_CPHA_2Edge; // 时钟第二个边沿进行数据捕获 USART_ClockInitStruct.USART_LastBit = USART_LastBit_Disable; // 最后一位数据的时钟脉冲不从 SCLK 输出 // 向寄存器中写入配置参数 USART_Init(USART1, &USART_InitStructure); USART_ClockInit(USART1, &USART_ClockInitStruct); //使能 USART3 接收中断 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // Enable the USARTy Interrupt NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); // 在 STM32 中配置好串口之后,发送数据,第一个数据是发不出去的,这时由于Cortex-M3本身的问题 USART_ClearFlag(USART1, USART_FLAG_TC); // 在使用外设时,不仅要使能其时钟,还要使能外设才可以正常使用 USART_Cmd(USART1, ENABLE); }
void USART2_Configuration(void) { USART_InitTypeDef USART_InitStructure; //串口参数初始化结构体 USART_ClockInitTypeDef USART_ClockInitStructure; //串口时钟参数初始化结构体 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE ); //时钟使能 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; // 最后一位数据的时钟脉冲不从SCLK输出 USART_ClockInit(USART2, &USART_ClockInitStructure); // 时钟参数初始化设置 USART_InitStructure.USART_BaudRate = 9600; // 波特率为:9600 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; // 发送使能+接收使能 USART_Init(USART2, &USART_InitStructure); /* Enable USART1 */ USART_ClearFlag(USART2, USART_IT_RXNE); //清中断,以免一启用中断后立即产生中断 USART_ITConfig(USART2,USART_IT_RXNE, ENABLE); //使能USART2中断源 USART_Cmd(USART2, ENABLE); //USART2总开关:开启 }
void setup_usart(void) { // enable clocks for usart2 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // Setup Alternate Functions to get usart2 out on PA2/PA3... GPIO_InitTypeDef usart_af; usart_af.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; usart_af.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOA, &usart_af); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); USART_ClockInitTypeDef usart_clocks; USART_ClockStructInit(&usart_clocks); usart_clocks.USART_Clock = USART_Clock_Enable; USART_ClockInit(USART2, &usart_clocks); USART_InitTypeDef usart_init; usart_init.USART_BaudRate = 57600; usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None; usart_init.USART_StopBits = USART_StopBits_1; usart_init.USART_Parity = USART_Parity_No; usart_init.USART_WordLength = USART_WordLength_8b; usart_init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &usart_init); USART_Cmd(USART2, ENABLE); }
/*********************************************************** * 函数说明:串口参数配置函数 * * 输入: 无 * * 输出: 无 * * 调用函数:INSERT_UART_ISR_CODE * ***********************************************************/ static void USART2_Configuration(void) { USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; /* 串口参数配置 */ 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_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(USART2, &USART_ClockInitStructure); USART_Init(USART2, &USART_InitStructure); /* 串口2使能 */ USART_Cmd(USART2, ENABLE); #if 1 USART_ITConfig(USART2, USART_IT_RXNE,ENABLE); USART_ITConfig(USART2, USART_IT_TXE,ENABLE); #endif }
void init_SCI(USART_TypeDef* USARTx, uint32_t USART_BaudRate, uint16_t USART_WordLength, uint16_t USART_StopBits, uint16_t USART_Parity, uint16_t USART_Mode, uint16_t USART_HardwareFlowControl) { static uint16_t usartDeInitFlag=0; USART_InitTypeDef USART_InitStructure; //setup UART_InitStructure USART_ClockInitTypeDef USART_ClockInitStructure; //setup UART_ClockInitStructure USART_ClockStructInit(&USART_ClockInitStructure); if (usartDeInitFlag==0) { USART_DeInit(USARTx); //Deinit USARTx usartDeInitFlag=1; } USART_InitStructure.USART_BaudRate = USART_BaudRate; USART_InitStructure.USART_WordLength = USART_WordLength; USART_InitStructure.USART_StopBits = USART_StopBits; USART_InitStructure.USART_Parity = USART_Parity; USART_InitStructure.USART_Mode = USART_Mode; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl; USART_Init(USARTx, &USART_InitStructure); USART_ClockInit(USARTx, &USART_ClockInitStructure); USART_Cmd(USARTx, ENABLE); }
void BSP_Ser_Init (CPU_INT32U baud_rate) { GPIO_InitTypeDef gpio_init; USART_InitTypeDef usart_init; USART_ClockInitTypeDef usart_clk_init; /* ------------------ INIT OS OBJECTS ----------------- */ BSP_OS_SemCreate(&BSP_SerTxWait, BSP_UART2_TX_BUFFER_SIZE, "Serial Tx Wait"); BSP_OS_SemCreate(&BSP_SerRxWait, 0, "Serial Rx Wait"); BSP_OS_SemCreate(&BSP_SerLock, 1, "Serial Lock"); /* ----------------- INIT USART STRUCT ---------------- */ usart_init.USART_BaudRate = baud_rate; usart_init.USART_WordLength = USART_WordLength_8b; usart_init.USART_StopBits = USART_StopBits_1; usart_init.USART_Parity = USART_Parity_No ; usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None; usart_init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; usart_clk_init.USART_Clock = USART_Clock_Disable; usart_clk_init.USART_CPOL = USART_CPOL_Low; usart_clk_init.USART_CPHA = USART_CPHA_2Edge; usart_clk_init.USART_LastBit = USART_LastBit_Disable; BSP_PeriphEn(BSP_PERIPH_ID_USART2); BSP_PeriphEn(BSP_PERIPH_ID_IOPD); BSP_PeriphEn(BSP_PERIPH_ID_AFIO); GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); /* ----------------- SETUP USART2 GPIO ---------------- */ /* Configure GPIOD.5 as push-pull. */ gpio_init.GPIO_Pin = GPIO_Pin_5; gpio_init.GPIO_Speed = GPIO_Speed_50MHz; gpio_init.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &gpio_init); /* Configure GPIOD.6 as input floating. */ gpio_init.GPIO_Pin = GPIO_Pin_6; gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &gpio_init); /* ------------- CLEAN UP RX & TX BUFFERS ------------- */ Mem_Clr((void*)&BSP_SerialRxBuffer[0], BSP_UART2_RX_BUFFER_SIZE); Mem_Clr((void*)&BSP_SerialTxBuffer[0], BSP_UART2_TX_BUFFER_SIZE); BSP_TxBuffer_Head_ndx = 0; BSP_TxBuffer_Tail_ndx = 0; BSP_RxBuffer_Head_ndx = 0; BSP_RxBuffer_Tail_ndx = 0; /* ------------------ SETUP USART2 -------------------- */ USART_Init(USART2, &usart_init); USART_ClockInit(USART2, &usart_clk_init); USART_Cmd(USART2, ENABLE); BSP_IntVectSet(BSP_INT_ID_USART2, BSP_Ser_ISR_Handler); BSP_IntEn(BSP_INT_ID_USART2); BSP_SerialInitilizated = DEF_TRUE; }
/* * Private functions */ void initPort4(Parity iParity, StopBits iStopBit, DataBits iDataLength, LinkMode iLinkMode, BaudRate iBaudRate, uint8_t iPreempPriority, uint8_t iSubPriority, InterruptSetting iInterruptSetting) { USART_InitTypeDef usartInitStruct; USART_ClockInitTypeDef usartClockInitStruct; GPIO_InitTypeDef gpioInitStruct; //enable USART clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //init the gpio init struct with common values gpioInitStruct.GPIO_Pin = 0; gpioInitStruct.GPIO_Mode = GPIO_Mode_AF; gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz; gpioInitStruct.GPIO_OType = GPIO_OType_PP; gpioInitStruct.GPIO_PuPd = GPIO_PuPd_UP; //set up the link mode setLinkModePort4(gpioInitStruct, usartInitStruct, iLinkMode); //set up the parity setParityPort4(usartInitStruct, iParity); //set up thestop bit config setStopBitPort4(usartInitStruct, iStopBit); //set up the word length setWordlengthPort4(usartInitStruct, iDataLength); //set the baud rate usartInitStruct.USART_BaudRate = iBaudRate; //set up the usart clock usartClockInitStruct.USART_Clock = USART_Clock_Disable; usartClockInitStruct.USART_CPOL = USART_CPOL_Low; usartClockInitStruct.USART_CPHA = USART_CPHA_2Edge; usartClockInitStruct.USART_LastBit = USART_LastBit_Disable; //init the gpio and the usart GPIO_Init(UART4_GPIO_PORT, &gpioInitStruct); USART_Init(UART4, &usartInitStruct); USART_ClockInit(UART4, &usartClockInitStruct); //set the interrupt if enabled if (iInterruptSetting == SERIAL_INT_ENABLE) { setNvicPort4(iPreempPriority, iSubPriority); USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); if ((iParity == SERIAL_ODD_PARITY) || (iParity == SERIAL_EVEN_PARITY)) { USART_ITConfig(UART4, USART_IT_PE, ENABLE); } USART_Cmd(UART4, ENABLE); } }
void UsartInit(USART_TypeDef *USART) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; if (USART == USART1) { //Enable bus clocks RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //Set USART1 Tx (PA.09) as AF 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); //Set 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); } else if (USART == USART2) { //Enable bus clocks RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //Set USART2 Tx (PA.02) as AF push-pull GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); //Set USART1 Rx (PA.3) as input floating GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); } else { //Enable bus clocks RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //Set USART1 Tx (PB.10) as AF push-pull GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); //Set USART1 Rx (PB.11) as input floating GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); } //Set USART paramenhs USART_ClockStructInit(&USART_ClockInitStructure); 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_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_ClockInit(USART1, &USART_ClockInitStructure); //Write USART1 parameters USART_Init(USART1, &USART_InitStructure); //Enable USART1 USART_Cmd(USART1, ENABLE); }
void usart_setup(const usart_dev *dev, uint32_t baudRate, uint16_t wordLength, uint16_t stopBits, uint16_t parity, uint16_t mode, uint16_t hardwareFlowControl) { /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(dev->USARTx)); assert_param(IS_USART_BAUDRATE(baud)); assert_param(IS_USART_STOPBITS(stopbits)); assert_param(IS_USART_PARITY(parity)); assert_param(IS_USART_WORD_LENGTH(wordLength)); assert_param(IS_USART_MODE(mode)); assert_param(IS_USART_HARDWARE_FLOW_CONTROL(hardwareFlowControl)); memset(dev->state, 0, sizeof(*dev->state)); dev->state->txbusy = 0; dev->state->callback = 0; /* Disable USARTx */ usart_disable(dev); rb_init(dev->txrb, USART_TX_BUF_SIZE, dev->state->tx_buf); rb_init(dev->rxrb, USART_RX_BUF_SIZE, dev->state->rx_buf); /* Enables the USART's 8x oversampling mode. */ USART_OverSampling8Cmd(dev->USARTx, ENABLE); USART_ClockInitTypeDef USART_InitClock; USART_ClockStructInit(&USART_InitClock); USART_ClockInit(dev->USARTx, &USART_InitClock); USART_InitTypeDef USART_config; USART_StructInit(&USART_config); USART_config.USART_BaudRate = baudRate; USART_config.USART_WordLength = wordLength; USART_config.USART_StopBits = stopBits; USART_config.USART_Parity = parity; USART_config.USART_Mode = mode; USART_config.USART_HardwareFlowControl = hardwareFlowControl; USART_Init(dev->USARTx, &USART_config); dev->USARTx->CR1 &= ~(USART_MASK_IDLEIE | USART_MASK_RXNEIE | USART_MASK_TCEIE | USART_MASK_TXEIE | USART_MASK_PEIE); dev->USARTx->CR2 &= ~(USART_MASK2_LBDIE); dev->USARTx->CR3 &= ~(USART_MASK3_CTSIE | USART_MASK3_EIE); if(mode & USART_Mode_Rx) { /* Enable Rx request */ USART_ClearFlag(dev->USARTx, USART_FLAG_RXNE); dev->USARTx->CR1 |= USART_MASK_RXNEIE; } if(mode & USART_Mode_Tx) { USART_ClearFlag(dev->USARTx, USART_FLAG_TC); } enable_nvic_irq(dev->irq, UART_INT_PRIORITY); }
////////////////////////////// // INITIALISATION FUNCTIONS // ////////////////////////////// void UART2_init(uint32_t uart_baudrate) { /**************************************************************** * UART2_init * * * * Description: Initialises UART2 with input "uart_baudrate" * * as baudrate. * * * * Variables: uint32_t uart_baudrate * * Return: void * ****************************************************************/ /* Structures */ GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* Enable GPIO clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* Enable UART clock */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); /* Connect PXx to USARTx_Tx*/ GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); /* Connect PXx to USARTx_Rx*/ /* Configure USART Tx as alternate function */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART Rx as alternate function */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOA, &GPIO_InitStructure); /* UART control settings */ USART_InitStructure.USART_BaudRate = uart_baudrate; /* Function input variable */ USART_InitStructure.USART_WordLength = USART_WordLength_8b; 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; /* UART clock settings */ USART_ClockStructure.USART_CPHA = DISABLE; USART_ClockStructure.USART_CPOL = DISABLE; USART_ClockStructure.USART_Clock = ENABLE; USART_ClockStructure.USART_LastBit = DISABLE; USART_ClockInit(USART2, &USART_ClockStructure); /* USART clock configuration */ USART_Init(USART2, &USART_InitStructure); /* USART configuration */ USART_Cmd(USART2, ENABLE); /* Enable USART */ }
void usart1_init() { RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); GPIO_Init(GPIOA, (GPIO_InitTypeDef*) &usart_tx); GPIO_Init(GPIOA, (GPIO_InitTypeDef*) &usart_rx); USART_ClockStructInit(&USART_ClockInitStructure); USART_ClockInit(USART1, &USART_ClockInitStructure); USART_Init(USART1, &usart1); USART_Cmd(USART1, ENABLE); }
void RCC_Config(void){ USART_ClockInitTypeDef USART_ClockInitStructure; SystemInit(); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; USART_ClockInit(USART1,&USART_ClockInitStructure); }
/******************************************************************************* * * uart_init - uart初始化 * * * INPUTS: * void. * * RETURNS: * void. * *******************************************************************************/ void uart_init(void) { GPIO_InitTypeDef GPIO_InitStructure; #ifdef BOARD_VERSON_0_1 // UART IO口线设置 /* Configure USART Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART Rx as input floating */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_Init(GPIOA, &GPIO_InitStructure); #endif #ifdef BOARD_VERSON_0_2 // UART IO口线设置 /* Configure USART0 Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART0 Rx as input floating */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOA, &GPIO_InitStructure); #endif USART_InitTypeDef USART_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_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(UART_BASE, &USART_ClockInitStructure); USART_Init(UART_BASE, &USART_InitStructure); UART_BASE->CR1 |= USART_CR1_UE; }
int main(void) { int i = 10; GPIO_InitTypeDef gpio_init; USART_InitTypeDef usart_init; USART_ClockInitTypeDef usart_clk_init; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // PA9 = Tx, PA10 = Rx gpio_init.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; gpio_init.GPIO_Mode = GPIO_Mode_AF; gpio_init.GPIO_Speed = GPIO_Speed_40MHz; gpio_init.GPIO_OType = GPIO_OType_PP; gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &gpio_init); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); USART_ClockStructInit(&usart_clk_init); USART_ClockInit(USART1, &usart_clk_init); usart_init.USART_BaudRate = 9600; usart_init.USART_WordLength = USART_WordLength_8b; usart_init.USART_StopBits = USART_StopBits_1; usart_init.USART_Parity = USART_Parity_No ; usart_init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(USART1, &usart_init); USART_Cmd(USART1,ENABLE); while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {} while (1) { if ( usart_available() ) // data available { usart_print( "Data Available: " ); uint8_t ch = usart_read(); usart_write(ch); usart_print( "\r\n" ); } } return 0; }
void USART_Config(void) { USART_ClockInitTypeDef USART_ClockInitStructure; USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE ); 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; // ���һλ���ݵ�ʱ�����岻��SCLK��� /* Configure the USART1 synchronous paramters */ USART_ClockInit(USART1, &USART_ClockInitStructure); // ʱ�Ӳ�����ʼ������ /* Configure USART1 Tx (PA.09) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // ѡ�йܽ�9 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ����������� GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // ����������50MHz GPIO_Init(GPIOA, &GPIO_InitStructure); // ѡ��A�˿� /* Configure USART1 Rx (PA.10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //ѡ�йܽ�10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//GPIO_Mode_IPU; //�������� GPIO_Init(GPIOA, &GPIO_InitStructure); //ѡ��A��� USART_DeInit(USART1); USART_InitStructure.USART_BaudRate = 9600; // ������Ϊ��115200 USART_InitStructure.USART_WordLength = USART_WordLength_9b; // 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 basic and asynchronous paramters */ USART_Init(USART1, &USART_InitStructure); /* Enable USART1 */ USART_ClearFlag(USART1, USART_IT_RXNE); //���жϣ�����һ�����жϺ����������ж� USART_ITConfig(USART1,USART_IT_RXNE, DISABLE); //ʹ��USART1�ж�Դ USART_Cmd(USART1, ENABLE); }
int main(void) { RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000); GPIO_InitTypeDef gpio_init; USART_InitTypeDef usart_init; USART_ClockInitTypeDef usart_clk_init; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // PA2 = Tx, PA3 = Rx gpio_init.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; gpio_init.GPIO_Mode = GPIO_Mode_AF_PP; gpio_init.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOA, &gpio_init); gpio_init.GPIO_Pin = GPIO_Pin_5; gpio_init.GPIO_Mode = GPIO_Mode_Out_PP; gpio_init.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOA, &gpio_init); GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); USART_ClockStructInit(&usart_clk_init); USART_ClockInit(USART2, &usart_clk_init); usart_init.USART_BaudRate = 9600; usart_init.USART_WordLength = USART_WordLength_8b; usart_init.USART_StopBits = USART_StopBits_1; usart_init.USART_Parity = USART_Parity_No ; usart_init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(USART2, &usart_init); USART_Cmd(USART2,ENABLE); while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) {} while (1) { usart_print( "UART Example" ); // toggle led GPIOA->ODR ^= GPIO_Pin_5; Delay(500); } }
void usart2_Configuration(u32 baudrate) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitstructure; /* Enable GPIOA clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* Enable USART clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//usart1 is on APB2 at 84Mhz max, usart2,3,6 are on APB1 at 42Mhz max /* Connect PXx to USARTx_Tx*/ GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); /* Connect PXx to USARTx_Rx*/ GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); /* Configure USART as alternate function */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // Tx Rx GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_OverSampling8Cmd(USART2, ENABLE); 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 Clock Initialization */ USART_ClockInitstructure.USART_Clock = USART_Clock_Disable ; USART_ClockInitstructure.USART_CPOL = USART_CPOL_High ; USART_ClockInitstructure.USART_LastBit = USART_LastBit_Disable; USART_ClockInitstructure.USART_CPHA = USART_CPHA_1Edge; /* USART configuration */ USART_Init(USART2, &USART_InitStructure); USART_ClockInit(USART2, &USART_ClockInitstructure); /* Enable USART */ USART_Cmd(USART2, ENABLE); }
void ANO_UART3::Init(u32 br_num) { USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStruct; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //开启USART3时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //Tx GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB , &GPIO_InitStructure); //Rx GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB , &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = br_num; //波特率可以通过地面站配置 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_Tx | USART_Mode_Rx; //发送、接收使能 //配置USART3时钟 USART_ClockInitStruct.USART_Clock = USART_Clock_Disable; //时钟低电平活动 USART_ClockInitStruct.USART_CPOL = USART_CPOL_Low; //SLCK引脚上时钟输出的极性->低电平 USART_ClockInitStruct.USART_CPHA = USART_CPHA_2Edge; //时钟第二个边沿进行数据捕获 USART_ClockInitStruct.USART_LastBit = USART_LastBit_Disable; //最后一位数据的时钟脉冲不从SCLK输出 USART_Init(USART3, &USART_InitStructure); USART_ClockInit(USART3, &USART_ClockInitStruct); //使能USART3接收中断 USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //使能USART3 USART_Cmd(USART3, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
// ============================================================================ void vUSART2_Init( void ) { USART_ClockInitTypeDef USART_ClockInitStruct; USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; // Enable needed clocks for uart. RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2, ENABLE ); RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE ); // Make sure you use 'GPIO_PinSource2' and NOT 'GPIO_Pin_2'. Using the // latter will not work! GPIO_PinAFConfig( GPIOA, GPIO_PinSource2, GPIO_AF_USART2 ); GPIO_PinAFConfig( GPIOA, GPIO_PinSource3, GPIO_AF_USART2 ); // Setup Tx / Rx pins. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // Tx Pin GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_Init( GPIOA, &GPIO_InitStructure ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // Rx Pin GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init( GPIOA, &GPIO_InitStructure ); // Make sure syncro clock is turned off. USART_ClockStructInit( &USART_ClockInitStruct ); USART_ClockInit( USART2, &USART_ClockInitStruct ); // Setup transmit complete irq. USART_ITConfig( USART2, USART_IT_TC, ENABLE ); // Use defaults (except baud rate). USART_StructInit( &USART_InitStructure ); USART_InitStructure.USART_BaudRate = 115200; USART_Init( USART2, &USART_InitStructure ); USART_Cmd( USART2, ENABLE ); // Enable USART2 interrupt. /* NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); */ }
////////////////////////////////////////////////////硬件平台初始化 void UART1_HardwareInit(u32 baudRate) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; USART_ClockInitTypeDef USART_ClockInitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_APB2PeriphClockCmd(UART1_RCC_APB2Periph, ENABLE); //使能GPIO GPIO_InitStructure.GPIO_Pin = UART1_Tx_Pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(UART1_Port, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = UART1_Rx_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(UART1_Port, &GPIO_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(USART1, &USART_InitStructure); USART_ClockStructInit(&USART_ClockInitStruct); //之前没有填入缺省值,是不行的 USART_ClockInit(USART1, &USART_ClockInitStruct); // Configure one bit for preemption priority NVIC_PriorityGroupConfig(Devices_PriorityGroup); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = UART1_PreemptionPriority; NVIC_InitStructure.NVIC_IRQChannelSubPriority = UART1_SubPriority; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能中断 USART_Cmd(USART1, ENABLE); //装载发送中断 UART1_SetTxISRFunc(UART1_TxDequeue); //存入发送队列 //装载接收中断 UART1_SetRxISRFunc(UART1_RxData); //存入接收队列 }