stm32f439 uart5的问题

2019-07-20 19:49发布

                                     用原子的F407案例uart2改为uart5   发现有usart和uart的区别  一直无法收发 如何解决?      


      void UART5_IRQHandler(void)
{
        u8 res;            
        if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)//接收到数据
        {                
          res =USART_ReceiveData(UART5);//;读取接收到的数据USART2->DR
                if(RS485_RX_CNT1<64)
                {
                        RS485_RX_BUF1[RS485_RX_CNT1]=res;                //记录接收到的值
                        RS485_RX_CNT1++;                                                //接收数据增加1
                }
        }                                                                                           
}
#endif                                                                                 
//初始化IO 串口2
//bound:波特率          
void RS485_Init1(u32 bound)
{           
       
    GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure1;
        NVIC_InitTypeDef NVIC_InitStructure;
       
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOD,ENABLE); //使能GPIOC时钟
        RCC_APB2PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);//使能USART5时钟
       
  //串口2引脚复用映射
        GPIO_PinAFConfig(GPIOC,GPIO_PinSource12,GPIO_AF_UART5); //GPIOC12复用为USART5
        GPIO_PinAFConfig(GPIOD,GPIO_PinSource2,GPIO_AF_UART5); //GPIOD2复用为USART5
       
        //USART2   
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;        //速度100MHz
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
        GPIO_Init(GPIOC,&GPIO_InitStructure);
       
       
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;//GPIOC6与GPIOC7
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;        //速度100MHz
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
        GPIO_Init(GPIOD,&GPIO_InitStructure);
       
        //PC8推挽输出,485模式控制  
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //GPIOB13
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//输出
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;        //速度100MHz
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
        GPIO_Init(GPIOB,&GPIO_InitStructure); //初始化PB13


   //USART2 初始化设置
        USART_InitStructure1.USART_BaudRate = bound;//波特率设置
        USART_InitStructure1.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
        USART_InitStructure1.USART_StopBits = USART_StopBits_1;//一个停止位
        USART_InitStructure1.USART_Parity = USART_Parity_No;//无奇偶校验位
        USART_InitStructure1.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
        USART_InitStructure1.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;        //收发模式
    USART_Init(UART5, &USART_InitStructure1); //初始化串口5
       
    USART_Cmd(UART5, ENABLE);  //使能串口 5
       
        USART_ClearFlag(UART5, USART_FLAG_TC);
       
#if EN_USART2_RX1       
        USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);//开启接受中断

        //Usart2 NVIC 配置
    NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
        NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;                //子优先级3
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //IRQ通道使能
        NVIC_Init(&NVIC_InitStructure);        //根据指定的参数初始化VIC寄存器、

#endif       
       
        RS485_TX_EN1=0;                                //默认为接收模式       
}

//RS485发送len个字节.
//buf:发送区首地址
//len:发送的字节数(为了和本代码的接收匹配,这里建议不要超过64个字节)
void RS485_Send_Data1(u8 *buf,u8 len)
{
        u8 t;
        RS485_TX_EN1=1;                        //设置为发送模式
          for(t=0;t<len;t++)                //循环发送数据
        {
    while(USART_GetFlagStatus(UART5,USART_FLAG_TC)==RESET); //等待发送结束               
    USART_SendData(UART5,buf[t]); //发送数据
        }         
        while(USART_GetFlagStatus(UART5,USART_FLAG_TC)==RESET); //等待发送结束               
        RS485_RX_CNT1=0;          
        RS485_TX_EN1=0;                                //设置为接收模式       
}
//RS485查询接收到的数据
//buf:接收缓存首地址
//len:读到的数据长度
void RS485_Receive_Data1(u8 *buf,u8 *len)
{
        u8 rxlen=RS485_RX_CNT1;
        u8 i=0;
        *len=0;                                //默认为0
        delay_ms(10);                //等待10ms,连续超过10ms没有接收到一个数据,则认为接收结束
        if(rxlen==RS485_RX_CNT1&&rxlen)//接收到了数据,且接收完成了
        {
                for(i=0;i<rxlen;i++)
                {
                        buf[i]=RS485_RX_BUF1[i];       
                }               
                *len=RS485_RX_CNT1;        //记录本次数据长度
                RS485_RX_CNT1=0;                //清零
        }
}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。