一、HX711模块介绍

HX711模块是一种专门用于称重传感器的放大器模块。它的主要功能是将测得的微小电压信号放大到可以被微控制器读取的范围。
HX711模块通常配合称重传感器一起使用,例如压力传感器、负载细胞等。它采用24位的模数转换器(ADC)来精确测量传感器的电压变化。
HX711模块具有一对不平衡式差分输入端,可以通过将感应器连接到这对输入端来测量称重传感器的变化。它还包含一个可变增益放大器,可以根据需要调整放大倍数,以适应不同的测量范围。
该模块通过I2C或SPI接口与微控制器通信,可以实现数据传输和命令控制。通过读取HX711模块输出的测量值,可以获取准确的称重数据。
HX711模块通常被广泛应用于电子秤、压力传感器、称重传感器等领域,用于测量和计量各类物体的重量。它具有高精度、低功耗、稳定性好等特点,适用于各种工业和消费类电子设备。

二、HX711模块原理图

stm32驱动HX711称重传感器 c++代码分享_c++

三、C++源码

hx711.hpp

/************************************************* 
* @copyright: 
* @author:Xupeng
* @date:2022-11-03
* @description: 
**************************************************/  
#ifndef _HX711_HPP_
#define _HX711_HPP_

#include "main_value.h"

class Hx711
{
public:
    Hx711(char *name,int sckPin = -1,int sdaPin = -1);
    ~Hx711();
	uint32_t adcRead();
    uint32_t getFurWeight();
    uint32_t getWeight(uint32_t furWeight);	               //称重函数
	void setCalibrationValue(float val);
private:
    int sckPin = -1;
    int sdaPin = -1;
	const static uint8_t NAME_MAX_NUM = 20;
	char name[NAME_MAX_NUM];
	float	calibrationValue = 384.7;       //标定系数,根据自己的传感器调节
};
#endif

hx711.cpp

/************************************************* 
* @copyright: 
* @author:Xupeng
* @date:2022-11-03
* @description: 
**************************************************/  
#include "hx711.hpp"


#define DBG_TAG "hx711"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
/************************************************* 
* @function:Hx711::Hx711(char *name,int sckPin, int sdaPin)       
* @description:HX711构造函数
* @calls:          
* @input:                    
* @return:       
* @others:       
*************************************************/ 
Hx711::Hx711(char *name,int sckPin, int sdaPin)
    :sckPin(sckPin)
    ,sdaPin(sdaPin)
{
	memset(this->name,0x00,this->NAME_MAX_NUM);
	strncpy(this->name,name,this->NAME_MAX_NUM);
	
	if(sckPin == -1 || sdaPin == -1)
	{
		LOG_E("pin value invald!");
		return;
	}
	
	rt_pin_mode(this->sckPin,PIN_MODE_OUTPUT);
	rt_pin_mode(this->sdaPin,PIN_MODE_INPUT);

}
/************************************************* 
* @function:Hx711::~Hx711()    
* @description:HX711析构函数
* @calls:          
* @input:                    
* @return:       
* @others:       
*************************************************/ 
Hx711::~Hx711()
{
}
/************************************************* 
* @function:Hx711::~Hx711()    
* @description:HX711析构函数
* @calls:          
* @input:                    
* @return:       
* @others:       
*************************************************/ 
uint32_t Hx711::adcRead()
{
	unsigned long count; 
	unsigned char i; 
	unsigned char over = 10;

  	rt_pin_write(this->sckPin,PIN_LOW);
  	count=0; 
  	while(rt_pin_read(this->sdaPin) && over >0)
	{
		over --;
		rt_thread_mdelay(2);
	};
	rt_hw_us_delay(2);
  	for(i=0;i<24;i++)
	{ 
	  	rt_pin_write(this->sckPin,PIN_HIGH);
	  	count=count<<1; 
		rt_hw_us_delay(2);
		rt_pin_write(this->sckPin,PIN_LOW);
	  	if(rt_pin_read(this->sdaPin))
			count++; 
		rt_hw_us_delay(2);
	} 
 	rt_pin_write(this->sckPin,PIN_HIGH);
    count=count^0x800000;//第25个脉冲下降沿来时,转换数据
	rt_hw_us_delay(2);
	rt_pin_write(this->sckPin,PIN_LOW);
	return(count);
}
/************************************************* 
* @function:uint32_t Hx711::getFurWeight()
* @description:获取毛皮重量
* @calls:          
* @input:                    
* @return:       
* @others:       
*************************************************/ 
uint32_t Hx711::getFurWeight()
{
	const uint8_t MEDIAN_LEN=5,MEDIAN=3;              //中值滤波参数,冒泡排序法
	uint32_t   buffer[MEDIAN_LEN];                //储存数组
	uint32_t readData,furWeight,num;           				
	uint8_t i,medleng = 0;
	for(i=0;i<MEDIAN_LEN;i++)
	{
		readData=this->adcRead();	        	   
		if(medleng == 0)                           //第1个元素,直接放入,不需要排序
		{ 
			buffer[0] = readData;
			medleng = 1; 
		}
		else                            	           //中值滤波,取中间值,冒泡排序法
		{  
			for(i = 0; i < medleng; i ++)  
			{
				if( buffer[i] > readData) 	          
				{ 
					num = readData; readData = buffer[i]; buffer[i] = num;
				}
			}
			buffer[medleng] = readData;          	
			medleng++;
		}		
		if(medleng >= MEDIAN_LEN) 		           
		{
			readData = buffer[MEDIAN];	            //取排序好的中间值,
			medleng = 0; 
		}
	}
	furWeight=readData;      
	return furWeight;
}
/************************************************* 
* @function:uint32_t Hx711::getWeight(uint32_t furWeight)     
* @description:获取重量
* @calls:          
* @input:                    
* @return:       
* @others:       
*************************************************/ 
uint32_t Hx711::getWeight(uint32_t furWeight)
{
	uint32_t readWeight;
	uint32_t weight;
	
	readWeight = this->adcRead();		            //HX711数据采集函数
	if(readWeight>furWeight)
	{
		//readWeight=this->adcRead();			                  //重新采集HX711数据					 														   
		weight=(uint32_t)(((float)(readWeight-furWeight))/this->calibrationValue);//修改标定系数,根据传感器不同
	}
	else				
		weight=0;
	return weight;
}
/************************************************* 
* @function:void Hx711::setCalibrationValue(float val)
* @description:设定标定参数
* @calls:          
* @input:                    
* @return:       
* @others:       
*************************************************/ 
void Hx711::setCalibrationValue(float val)
{
	this->calibrationValue = val;
}