请教高手,为什么我用STM32F4 串口发送数据,管脚无波形,仿真时看到DR一直无数据,发送标志位一直为0,跪求高手解释!!配置如下:

2019-07-21 05:25发布

void USART2_Parameter_Configuration(u16 baudrate,u16 USART_StopBits,u16 USART_Parity)
{
 u16 temp_wordlength;     USART_InitTypeDef   USART_InitStructure;
    USART_ClockInitTypeDef  USART_ClockInitStructure;
  NVIC_InitTypeDef   NVIC_InitStructure;
 GPIO_InitTypeDef   GPIO_InitStructure;
 
    RCC_APB2PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);    
   GPIO_PinAFConfig(GPIOA, GPIO_PinSource3,GPIO_AF_USART2);  //复合管脚 
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource2,GPIO_AF_USART2);  //复合管脚
 //USART2 CAM_RX
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
 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);
 
 //USART2 CAM_TX
 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);    if(USART_Parity)
 {
  temp_wordlength=USART_WordLength_9b;
 }
 else
 {
  temp_wordlength=USART_WordLength_8b;
 }
 USART_InitStructure.USART_BaudRate = baudrate;
 USART_InitStructure.USART_WordLength = temp_wordlength;
 USART_InitStructure.USART_StopBits =USART_StopBits;  USART_InitStructure.USART_Parity =USART_Parity;
 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
 /* Configure USART2 */  
 USART_Init(USART2,&USART_InitStructure); //    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_ClockStructInit(&USART_ClockInitStructure);
    USART_ClockInit(USART2,&USART_ClockInitStructure);  
//中断
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
   NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 7; 
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
 
  /* Enable USART3 Receive and Transmit interrupts */
  //USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  //USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
       /* Enable the USART2 */
    USART_Cmd(USART2, ENABLE);  
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
5条回答
jxa19890929
1楼-- · 2019-07-21 06:53
是USART
正点原子
2楼-- · 2019-07-21 12:50
下载骤雨龙元的代码参考下吧.
jxa19890929
3楼-- · 2019-07-21 17:26
回复【3楼】正点原子:
---------------------------------
下了 感觉不是自己代码的问题,仿真看到BRR寄存器也为0,不知道怎么回事!原子哥有没有什么经验,感觉在计算波特率的时候有问题,算出的结果为0.
正点原子
4楼-- · 2019-07-21 23:02
我无经验.
chinafox
5楼-- · 2019-07-22 00:43
 精彩回答 2  元偷偷看……

一周热门 更多>