m0_56354867 2021-11-10 16:45 采纳率: 100%
浏览 143
已结题

求解答!STM32连接了RS485接口,与温湿度传感器无法通信

各个部分都配置好了,但是还没有给传感器供电串口就已经返回乱码,看了好几遍代码也没有找到问题所在,求解答。

**rs486.c**
#include "bsp_rs485.h"
#include "bsp_SysTick.h"

//接收缓存区     
unsigned char RS485_Receive_BUF[64];      //接收缓冲,最大64个字节.
//接收到的数据长度
unsigned char RS485_Receive_CNT=0; 

void USART1_IRQHandler(void)
{
    unsigned char ch;
    while(USART_GetITStatus(USART1, USART_IT_RXNE)==SET)
    {
        ch=USART_ReceiveData(USART1);
        if(RS485_Receive_CNT<64)
        {
            RS485_Receive_BUF[RS485_Receive_CNT]=ch;
            RS485_Receive_CNT++;
        }
    }
    
}

void RS485_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOB, ENABLE);
    
    //RS485接收发送使能,不需接线,只需要电平信号
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;    
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;    //TX
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;    //RX
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);    
        
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1,ENABLE);//复位串口2
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1,DISABLE);//停止复位
 
    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);
    
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; 
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);    
    
    USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);    

    USART_Cmd(USART1, ENABLE);
    
    //默认设置为接收模式,低电平
    RS485_Receive;
}

void RS485_SendData(unsigned char *buf,unsigned char len)
{
    unsigned char t;
    RS485_Transfer;
    Delay_us(10000);
    for(t=0;t<len;t++)
    {
        while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);//确保发送数据寄存器为空,SET为空
        USART_SendData(USART1,buf[t]);
    }
    Delay_us(10000);
    while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
    RS485_Receive_CNT=0;
    RS485_Receive;
}

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

**rs485.h**
#ifndef __BSP_RS485_H__
#define __BSP_RS485_H__

#include <stm32f10x.h>
#include <stdio.h>

void RS485_Configuration(void);
void RS485_SendData(unsigned char *buf,unsigned char len);
void RS485_ReceiveData(unsigned char *buf,unsigned char *len);
    
#define RS485_Receive   GPIO_ResetBits(GPIOB,GPIO_Pin_0)
#define RS485_Transfer  GPIO_SetBits(GPIOB,GPIO_Pin_0)


#endif



**main.c**
#include "bsp_SysTick.h"
#include "Led_key.h"
#include "bsp_exti.h"
#include "bsp_tim2.h"
#include "bsp_uart.h"
#include "bsp_rs485.h"
#include "modbus_rtu.h"

void RS485_GetData_Temperature(void);

unsigned char Getdata[8]={0x01,0x03,0x00,0x00,0x00,0x01,0x84,0x0A}; 
unsigned char RS485Buf[16];
unsigned char len;
unsigned char address1=0x01;

int main(void)
{    
    RS485_Configuration();
    while(1)    
    {
        RS485_GetData_Temperature();
        Delay_us(1000000);
    }
}

void RS485_GetData_Temperature()
{
    unsigned char TemperatureH;
    unsigned char TemperatureL;
    float Temperature;
    RS485_SendData(Getdata,8);
    RS485_ReceiveData(RS485Buf,&len);
    if(len)
    {
        if(RS485Buf[0]==address1)
        {
            if(RS485Buf[1]==0x03&&RS485Buf[2]==0x02) 
            {
                if(RS485Buf[6]==(CRC16_ccitt(RS485Buf,5)>>8)&&RS485Buf[5]==(CRC16_ccitt(RS485Buf,5)&0X00FF))
                {
                    TemperatureH=RS485Buf[3];
                    TemperatureL=RS485Buf[4];
                    Temperature=(TemperatureH<<8 | TemperatureL);
                    printf("Temperature = %f\r\n",Temperature/10);
                }
            }
        }
    }
}




  • 写回答

1条回答 默认 最新

  • waterfxw 2021-11-11 15:45
    关注

    系统时钟初始化了???

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月23日
  • 已采纳回答 3月15日
  • 创建了问题 11月10日

悬赏问题

  • ¥15 u盘问题:盘符不显示 无媒体
  • ¥50 R语言读取nc按月均值转为tif
  • ¥30 智能车串级pid调参
  • ¥15 visual studio code翻译老是错误
  • ¥20 卫星测高数据的高程转换
  • ¥15 爬取招聘网站数据信息
  • ¥15 安装完tensorflow,import tensorflow as tf后报错,如何解决?
  • ¥15 ultralytics库导出onnx模型,模型失去预测能力
  • ¥15 linux下点对点协议连接2个USB串口的硬件流量控制问题
  • ¥15 SQL数据自动生成问题