stm32 485 的只能发送数据,不能接收数据

这是我用的原点的485例程,我只能实现485的发送,实现不了数据的接收发送,这是为什么呢

img


#include "stm32f4xx.h"
#include "usart.h"
#include "delay.h"
#include "RC485.h"

int main(void)
{
    uint8_t BUF[2]={'a','b'};
  uint8_t len;
    uint8_t buf[100];
     RS485_Init();
    //printf("%d",01);
    RS485_Send_Data(BUF,2);
  while(1)
        {
      RS485_Receive_Data(buf, &len);
            RS485_Send_Data(buf,len);
    }
}
#include  "RC485.h"
#include "delay.h"
uint8_t RS485_RX_CNT=0;
uint8_t RS485_RX_BUF[64];

void RS485_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOG,ENABLE); 
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
    
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); //GPIOA2复用为USART2
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); //GPIOA3复用为USART2
    
    
    //USART2    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; //GPIOA2与GPIOA3
    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(GPIOA,&GPIO_InitStructure); //初始化PA2,PA3
    
    
    //PG8推挽输出,485模式控制  
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //GPIOG8
    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(GPIOG,&GPIO_InitStructure); //初始化PG8
    
    //USART2 初始化设置
    USART_InitStructure.USART_BaudRate = 9600;//波特率设置
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
    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;    //收发模式
    USART_Init(USART2, &USART_InitStructure); //初始化串口2
    
    USART_Cmd(USART2, ENABLE);  //使能串口 2
    
    USART_ClearFlag(USART2, USART_FLAG_TC);
        
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启接受中断
  
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    //Usart2 NVIC 配置
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
    NVIC_InitStructure.NVIC_IRQChannelSubPriority =2;        //子优先级2
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;            //IRQ通道使能
    NVIC_Init(&NVIC_InitStructure);    //根据指定的参数初始化VIC寄存器、
    
    RS485_TX_EN=0;        
}

void USART2_IRQHandler(void)
{
    u8 res;        
    if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)//接收到数据
    {         
      res =USART_ReceiveData(USART2);//;读取接收到的数据USART2->DR
        if(RS485_RX_CNT<64)
        {
            RS485_RX_BUF[RS485_RX_CNT]=res;        //记录接收到的值
            RS485_RX_CNT++;                        //接收数据增加1 
        } 
    }                                               
}



void RS485_Send_Data(uint8_t *buf,uint8_t len)
{
    u8 t;
    RS485_TX_EN=1; 
    for(t=0;t<len;t++) 
    {
    while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET); 
    USART_SendData(USART2,buf[t]); 
    }
    while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET); 
    RS485_RX_CNT=0;
    RS485_TX_EN=0; 
}

void RS485_Receive_Data(u8 *buf,u8 *len)
{
    u8 rxlen=RS485_RX_CNT;
    u8 i=0;
    *len=0;                //默认为0
    delay_ms(10);        //等待10ms,连续超过10ms没有接收到一个数据,则认为接收结束
    if(rxlen==RS485_RX_CNT&&rxlen)//接收到了数据,且接收完成了
    {
        for(i=0;i<rxlen;i++)
        {
            buf[i]=RS485_RX_BUF[i];    
        }        
        *len=RS485_RX_CNT;    //记录本次数据长度
        RS485_RX_CNT=0;        //清零
    }
}








【以下回答由 GPT 生成】

首先,我建议您检查以下几个方面来解决无法接收数据的问题:

  1. 硬件连接: 确保您的485模块正确连接到STM32的串口引脚,并且使能引脚正确配置。可以参考官方的手册或者原厂的文档来获取正确的引脚配置和连接方式。

  2. UART初始化: 确保您的UART(串口)已经正确初始化并且使能。可能需要在您的代码中添加UART初始化函数,确保配置正确并打开UART的中断接收功能。以下是一个UART初始化的示例代码,假设您使用的是USART1:

void UART_Init(void)
{
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);

    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;
    USART_Init(USART1, &USART_InitStructure);

    USART_Cmd(USART1, ENABLE);
}

您需要根据您的实际情况修改波特率和引脚配置。

  1. 中断接收: 确保您的接收函数正确并且使能了中断接收功能。根据您使用的库文件和485模块的工作方式,可以使用USART的接收中断来处理接收数据。以下是一个基本的接收中断处理函数的示例代码:
uint8_t receivedData;
uint8_t dataReceived = 0;

void USART1_IRQHandler(void)
{
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {
        receivedData = USART_ReceiveData(USART1);
        dataReceived = 1; // 标记接收到数据
    }
}

这个中断处理函数将在接收到数据时被调用,并将接收到的数据保存到一个变量中。

  1. 接收数据: 在主循环中,您可以根据dataReceived变量的值来处理接收到的数据。以下是修改后的主循环代码:
while(1)
{
    if(dataReceived)
    {
        RS485_Send_Data(&receivedData, 1); // 发送刚刚接收到的数据
        dataReceived = 0; // 重置标志位
    }
}

这样就可以实现将接收到的数据发送回去。

请注意,以上代码仅仅是一个参考示例,实际情况可能会根据您使用的库文件和硬件配置有所不同。如果问题仍然存在,我建议您仔细阅读官方提供的文档和例程,或者咨询原厂的技术支持部门,以获得更准确的解决方案。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

没有用过这段代码。
但是RS485一般都是单工模式,不能同时收发的。