STM32F072串口3和串口4求助

2019-12-12 18:16发布

有个项目需要用到STM32F072的4个串口,串口1和2都已经OK,串口3和4怎么配置都配置不成功,发送数据都没有出来。配置串口1和2的时候,仿真读串口1和2的寄存器地址能看到设置进去的数据,但是3和4就没有,都是0。
有用过的帮忙看一下是什么情况,谢谢。
配置代码如下:
  1. void USART3_Config(void)
  2. {
  3.     USART_InitTypeDef USART_InitStructure;
  4. //    NVIC_InitTypeDef NVIC_InitStructure;
  5.     GPIO_InitTypeDef GPIO_InitStructure;

  6.     USART_DeInit(USART4);

  7.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  8.     RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  9.    
  10.     /* Connect PXx to USARTx_Tx */
  11.     GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_1);
  12.     /* Connect PXx to USARTx_Rx */
  13.     GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_1);

  14.     /* Configure USART Tx and Rx as alternate function push-pull */
  15.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  16.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
  17.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  18.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  19.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  20.     GPIO_Init(GPIOC, &GPIO_InitStructure);

  21.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  22.     GPIO_Init(GPIOC, &GPIO_InitStructure);

  23.     USART_InitStructure.USART_BaudRate = 115200;
  24.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  25.     USART_InitStructure.USART_StopBits = USART_StopBits_1;
  26.     USART_InitStructure.USART_Parity = USART_Parity_No;
  27.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  28.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  29.     USART_Init(USART3, &USART_InitStructure);

  30. //    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);  

  31.     /* NVIC configuration */
  32.     /* Enable the USARTx Interrupt */
  33. //    NVIC_InitStructure.NVIC_IRQChannel = USART3_4_IRQn;
  34. //    NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  35. //    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  36. //    NVIC_Init(&NVIC_InitStructure);

  37.     /* Enable USART */
  38.     USART_Cmd(USART3, ENABLE);
  39. }
复制代码
  1. void USART4_Config(void)
  2. {
  3.     USART_InitTypeDef USART_InitStructure;
  4. //    NVIC_InitTypeDef NVIC_InitStructure;
  5.     GPIO_InitTypeDef GPIO_InitStructure;

  6. //    USART_DeInit(USART4);

  7.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  8.     RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART4, ENABLE);
  9.    
  10.     /* Connect PXx to USARTx_Tx */
  11.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_4);
  12.     /* Connect PXx to USARTx_Rx */
  13.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_4);

  14.     /* Configure USART Tx and Rx as alternate function push-pull */
  15.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  16.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
  17.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  18.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  19.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  20.     GPIO_Init(GPIOA, &GPIO_InitStructure);

  21.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  22.     GPIO_Init(GPIOA, &GPIO_InitStructure);

  23.     USART_InitStructure.USART_BaudRate = 115200;
  24.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  25.     USART_InitStructure.USART_StopBits = USART_StopBits_1;
  26.     USART_InitStructure.USART_Parity = USART_Parity_No;
  27.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  28.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  29.     USART_Init(USART4, &USART_InitStructure);

  30.     USART_ITConfig(USART4, USART_IT_RXNE, ENABLE);  

  31.     /* NVIC configuration */
  32.     /* Enable the USARTx Interrupt */
  33. //    NVIC_InitStructure.NVIC_IRQChannel = USART3_4_IRQn;
  34. //    NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  35. //    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  36. //    NVIC_Init(&NVIC_InitStructure);

  37.     /* Enable USART */
  38.     USART_Cmd(USART4, ENABLE);
  39. }
复制代码
发送数据如下,循环里连续发送0XAA。
        while(1)
        {
        USART_SendData(USART4,0xAA);
        while (USART_GetFlagStatus(USART4, USART_FLAG_TC) == RESET);
        
        USART_SendData(USART3,0xAA);
        while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);  
        }       
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
5条回答
belongfs
1楼-- · 2019-12-12 23:59
有没有人能解惑一下?
belongfs
2楼-- · 2019-12-13 03:28
解决了。RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);应该改成RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
蛋定
3楼-- · 2019-12-13 07:28
多谢分享解决办法
sunplus
4楼-- · 2019-12-13 09:09
之前遇到过这个问题,也是他们没在一个总线上
n0831
5楼-- · 2019-12-13 14:03
 精彩回答 2  元偷偷看……

一周热门 更多>