RS485接收数据不正常

2019-08-14 06:21发布

我的主程序较短时一切功能正常,但随着代码的增加出现了数据接收错误和接收不到数据。不明白什么原因,求指教。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
4条回答
xx3360765
2019-08-14 17:15
这是我的USART.C文件////////////
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_usart.h"
#include "misc.h"
#include "stdio.h"
#include "usart.h"

#if 1
#pragma import(__use_no_semihosting)            
//±ê×¼¿aDèòaμÄÖ§3Öoˉêy                 
struct __FILE
{
        int handle;

};

FILE __stdout;      
//¶¨òå_sys_exit()òÔ±üÃaê1óðëÖ÷»úģ꽠  
_sys_exit(int x)
{
        x = x;
}
//Öض¨òåfputcoˉêy
int fputc(int ch, FILE *f)
{      
        while((USART1->SR&0X40)==0);//Ñ-»··¢Ëí,Ö±μ½·¢Ëííê±Ï   
    USART1->DR = (u8) ch;      
        return ch;
}
#endif

void USART1_Initial(void)
{
        GPIO_InitTypeDef GPIO_InitStructure2;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);//ê1ÄüPA,USART1¶Ë¿úê±Öó

        /*--------------485¿ØÖƶË3õê¼»ˉ----------------*/  
        GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_11 ;  
  GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz; //50Mê±ÖóËù¶è
  GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_Out_PP; //íÆíìêä3ö
  GPIO_Init(GPIOA, &GPIO_InitStructure2);  
  
         
  GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_9;          //485_TXD
  GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;   
  GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF_PP;  //¸′óÃíÆíìêä3ö  
  GPIO_Init(GPIOA, &GPIO_InitStructure2);  
  
  GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_10;           //485_RXD  
  GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_IN_FLOATING; //¸¡¿Õêäèë
  GPIO_Init(GPIOA, &GPIO_InitStructure2);
       
       
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//ÇàÕ¼óÅÏ輶3
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;                //×óóÅÏ輶3
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //IRQí¨μàê1Äü
        NVIC_Init(&NVIC_InitStructure);        //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
       
       
        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_Tx | USART_Mode_Rx;  
  USART_Init(USART1, &USART_InitStructure);
        //USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿aÆô′®¿ú½óêüÖD¶Ï
        USART_ITConfig(USART1, USART_IT_PE, ENABLE);
  USART_ITConfig(USART1, USART_IT_ERR, ENABLE);       
        USART_Cmd(USART1, ENABLE);
}

一周热门 更多>