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

求解答!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日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效