aptx48699 发表于 2018-6-25 11:10:42

求助!!!STM32L151C8T6,串口2和3都是只能发送,不能接收


void Uart_2_int()
{

    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;

                               
    RCC_APB2PeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);    //使能USART2,GPIOA时钟//USART2_TX   GPIOA.2
       
       
       
          USART_DeInit(USART2);//复位串口1
       
       

       
       
       //USART1_TX   PA.2
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;       
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    //USART1_RX          PA.3   GPIO_PuPd_NOPULL
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;


    GPIO_Init(GPIOA, &GPIO_InitStructure);


    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_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

                GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
                GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
               
    USART_Init(USART2, &USART_InitStructure); //初始化串口
       

    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断
                USART_Cmd(USART2, ENABLE);                  //使能串口
                USART_ClearITPendingBit(USART2, USART_IT_TC);//清除串口TC位
               


}


void USART2_IRQHandler(void)                  //串口1中断服务程序
{


        if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
        {
          uart_2=USART_ReceiveData(USART2);
                uart_2_count++;
                serial2_sned_buff("123",3);
                uart_2_count=0;
               

        }       

}







huangyanga 发表于 2018-6-25 11:54:41

1、先排除线路有没有接通。看现象描述像是串口接受线断了
2、确定复用时钟不用打开?

wudianjun2001 发表于 2018-6-25 13:21:49

用cube生成了例程对照看看

す疯Ⅱ恒す 发表于 2018-6-25 13:32:29

中断源不用初始化一下的么?

与龙共舞 发表于 2018-6-25 16:07:37

        //USART1_TX   
        GPIO_InitStructure.GPIO_Pin = usart.GPIOTx_Pin;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;        //复用推挽输出
        GPIO_Init(usart.GPIOTx, &GPIO_InitStructure);

        //USART1_RX          
        GPIO_InitStructure.GPIO_Pin = usart.GPIORx_Pin;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
        GPIO_Init(usart.GPIORx, &GPIO_InitStructure);

TX RX 两次初始化 貌似你忘记一句话呀
页: [1]
查看完整版本: 求助!!!STM32L151C8T6,串口2和3都是只能发送,不能接收