| 
 
中级会员  
 
	积分220金钱220 注册时间2016-6-5在线时间66 小时 | 
 
 发表于 2019-4-30 17:01:15
|
显示全部楼层 
| 下面是我配置好的串口函数,测试了六个串口也都能进的去
 
 * usart1: PA9, PA10
 * usart2: PA2, PA3
 * usart3: PB10, PB11
 * usart4: PA0, PA1
 * usart5: PB3, PB4
 * usart6: PA4, PA5
 */
 void uart_init(E_UartType eUartType, unsigned int uBaudRate)
 {
 uint8_t uIRQChannel = USART1_IRQn;
 NVIC_InitTypeDef NVIC_InitStruct;
 GPIO_InitTypeDef GPIO_InitStructure;
 USART_InitTypeDef USART_InitStructure;
 GPIO_TypeDef *ptGpio = GPIOA;
 USART_TypeDef *ptUsart = USART1;
 
 if((unsigned char)eUartType < MAX_UART_COUNT){
 memset((void*)g_ptUartBufInfos[(unsigned char)eUartType], 0, sizeof(T_UartBuf));
 }
 
 switch(eUartType){
 case E_Uart1:
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,  ENABLE);
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
 
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource9,  GPIO_AF_1);
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
 
 uIRQChannel = USART1_IRQn;
 
 ptGpio  = GPIOA;
 ptUsart = USART1;
 break;
 
 case E_Uart2:
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
 
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
 
 uIRQChannel = USART2_IRQn;
 
 ptGpio  = GPIOA;
 ptUsart = USART2;
 break;
 //#ifdef STM32F030xC
 case E_Uart3:
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
 
 GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_4);
 GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_4);
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
 
 uIRQChannel = USART3_6_IRQn;
 
 ptGpio  = GPIOB;
 ptUsart = USART3;
 break;
 
 case E_Uart4:
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,  ENABLE);
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART4, ENABLE);
 
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_4);
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_4);
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
 
 uIRQChannel = USART3_6_IRQn;
 
 ptGpio  = GPIOA;
 ptUsart = USART4;
 break;
 
 case E_Uart5:
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,  ENABLE);
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART5, ENABLE);
 
 GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_4);
 GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_4);
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;
 
 uIRQChannel = USART3_6_IRQn;
 
 ptGpio  = GPIOB;
 ptUsart = USART5;
 break;
 
 case E_Uart6:
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,  ENABLE);
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
 
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_5);
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_5);
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
 
 uIRQChannel = USART3_6_IRQn;
 
 ptGpio  = GPIOA;
 ptUsart = USART6;
 break;
 //#endif
 default:
 break;
 }
 
 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_NOPULL;
 
 GPIO_Init(ptGpio, &GPIO_InitStructure);
 
 USART_InitStructure.USART_BaudRate                          = uBaudRate;                              //波特率
 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(ptUsart, &USART_InitStructure);
 
 NVIC_InitStruct.NVIC_IRQChannel                 = uIRQChannel;
 NVIC_InitStruct.NVIC_IRQChannelPriority = 0x01;
 NVIC_InitStruct.NVIC_IRQChannelCmd                 = ENABLE;
 NVIC_Init(&NVIC_InitStruct);
 
 USART_ITConfig(ptUsart, USART_IT_RXNE, ENABLE);
 
 USART_Cmd(ptUsart, ENABLE);
 }
 
 
 void uart_gpio(void)
 {
 
 GPIO_InitTypeDef GPIO_InitStructure;
 
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,  ENABLE);
 //RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE );
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
 GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
 GPIO_InitStructure.GPIO_OType =  GPIO_OType_PP;
 //GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
 }
 
 void USART1_IRQHandler(void)  //接收中断
 {
 
 if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){
 
 
 unsigned char ucData = USART_ReceiveData(USART1);
 
 usart_put_data_to_buf(g_ptUartBufInfos[0], ucData);
 }
 }
 
 void USART2_IRQHandler(void)
 {
 if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET){
 unsigned char ucData = USART_ReceiveData(USART2);
 //printf("USART2 recv data: %c, %x\n", ucData, ucData);
 usart_put_data_to_buf(g_ptUartBufInfos[1], ucData);
 }
 }
 
 
 #ifdef STM32F030xC
 void USART3_6_IRQHandler(void)
 {
 unsigned char ucData;
 
 //uart_senddata("ok \n",4, E_Uart4);
 if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){
 ucData = USART_ReceiveData(USART3);
 //printf("USART3 recv data: %c, %x\n", ucData, ucData);
 
 usart_put_data_to_buf(g_ptUartBufInfos[2], ucData);
 }
 
 if(USART_GetITStatus(USART4, USART_IT_RXNE) != RESET){
 ucData = USART_ReceiveData(USART4);
 
 USART_SendData(USART4, ucData);
 //printf("USART4 recv data: %c, %x\n", ucData, ucData);
 
 usart_put_data_to_buf(g_ptUartBufInfos[3], ucData);
 }
 
 if(USART_GetITStatus(USART5, USART_IT_RXNE) != RESET){
 ucData = USART_ReceiveData(USART5);
 
 //printf("USART5 recv data: %c, %x\n", ucData, ucData);
 
 usart_put_data_to_buf(g_ptUartBufInfos[4], ucData);
 }
 
 if(USART_GetITStatus(USART6, USART_IT_RXNE) != RESET){
 ucData = USART_ReceiveData(USART6);
 
 //printf("USART6 recv data: %c, %x\n", ucData, ucData);
 
 usart_put_data_to_buf(g_ptUartBufInfos[5], ucData);
 }
 }
 #endif
 | 
 |