Electronic module|Pressure sensor module HX711---C51&&STM32 drive

Electronic Module|Pressure Sensor Module HX711---Hardware Introduction and C51&&STM32 Driver

physical photos

insert image description here

Module Introduction

HX711 is a 24-bit A/D converter chip specially designed for high-precision load cells. Compared with other chips of the same type, this chip integrates the peripheral circuits required by other chips of the same type, including regulated power supply, on-chip clock oscillator, etc., and has the advantages of high integration, fast response, and strong anti-interference. The cost of the whole machine of the electronic scale is reduced, and the performance and reliability of the whole machine are improved. The interface and programming of the chip and the back-end MCU chip are very simple, all control signals are driven by pins, and there is no need to program the registers inside the chip. The input selector switch can select channel A or channel B arbitrarily, and connect with its internal low-noise programmable amplifier. Channel A has a programmable gain of 128 or 64, corresponding to a full-scale differential input signal amplitude of ±20mV or ±40mV, respectively. Channel B is a fixed gain of 32 for system parameter detection. The regulated power supply provided in the chip can directly provide power to the external sensor and the A/D converter in the chip, and there is no need for another analog power supply on the system board. The on-chip clock oscillator does not require any external components. The power-on automatic reset function simplifies the initialization process of power-on.

Module Features

  • Two Selectable Differential Inputs
  • On-chip low-noise programmable amplifier with selectable gains of 64 and 128
  • On-chip voltage regulation circuit can directly provide power to external sensors and on-chip A/D converter
  • The on-chip clock oscillator does not require any external components, and an external crystal or clock can also be used if necessary
  • Power-on automatic reset circuit
  • Simple digital control and serial communication: all controls are input by pins, and the on-chip registers do not need to be programmed
  • Selectable 10Hz or 80Hz Output Data Rate
  • Simultaneous suppression of 50Hz and 60Hz power interference
  • Power consumption (including regulated power supply circuit): typical operating current: <1.7mA, power-off current: <1μA
  • Operating voltage range: 2.6 ~ 5.5V
  • Operating temperature range: -20 ~ +85°C
  • 16-pin SOP-16 package

software driver

The serial port communication line is composed of pins PD_SCK and DOUT, which are used to output data, select input channel and gain. When the data output pin DOUT is at a high level, it indicates that the A/D converter is not ready to output data, and the serial port clock input signal PD_SCK should be at a low level. When DOUT changes from high level to low level, PD_SCK should input 25 to 27 different clock pulses. The rising edge of the first clock pulse will read the highest bit (MSB) of the output 24-bit data until the 24th clock pulse is completed, and the 24-bit output data will be output bit by bit from the highest bit to the lowest bit. The 25th to 27th clock pulses are used to select the input channel and gain of the next A/D conversion.
insert image description here
The number of input clock pulses of PD_SCK should not be less than 25 or more than 27, otherwise it will cause serial communication errors. When the input channel or gain of the A/D converter is changed, the A/D converter needs 4 data output cycles to be stable. DOUT will change from high level to low level after 4 data output cycles, and output valid data.

Data output, input channel and gain selection timing diagram
insert image description here

Through the measured AD value, the principle of conversion to gravity is as follows :

Assuming that the gravity is A Kg, (x<5Kg), the measured AD value is the
output of the y sensor, and the voltage sent to the AD module is A Kg * 4.3mV / 5Kg = 0.86A mV
after 128 times the gain is 128 * 0.86 A = 110.08AmV
converted to 24bit digital signal is 110.08A mV * 224 / 4.3V = 429496.7296A,
so y = 429496.7296A
, so A = y / 429496.7296
, so the calculation formula in the program is obtained

Weight_Shiwu = (unsigned long)((float)Weight_Shiwu/429.5)

C51 software code

sbit ADDO = P1^5;
sbit ADSK = P0^0;
unsigned long ReadCount(void)
{
    
    
unsigned long Count;
unsigned char i;
ADSK=0;
//使能AD(PD_SCK 置低)
Count=0;
while(ADDO);
//AD转换未结束则等待,否则开始读取
for (i=0;i<24;i++)
{
    
    
ADSK=1;
//PD_SCK 置高(发送脉冲)
Count=Count<<1; //下降沿来时变量Count左移一位,右侧补零
ADSK=0;
//PD_SCK 置低
if(ADDO) Count++;
}
ADSK=1;
Count=Count^0x800000;//第25个脉冲下降沿来时,转换数据
ADSK=0;
return(Count);
}

STM32 software code




#define  DWT_CYCCNT  *(volatile unsigned int *)0xE0001004
#define  DWT_CR      *(volatile unsigned int *)0xE0001000
#define  DEM_CR      *(volatile unsigned int *)0xE000EDFC
#define  DBGMCU_CR   *(volatile unsigned int *)0xE0042004

#define  DEM_CR_TRCENA               (1 << 24)
#define  DWT_CR_CYCCNTENA            (1 <<  0)

#define		WEIGHT_CAP_NUM							1		//重量采集数量 滤波使用

uint8_t		ucWeightCapCompleteFlag= 0;     //重量采集完成标志
uint8_t		ucWeightCapCount 			 = 0;			//重量采集计数
int32_t   ilWeightRawDataAddToal = 0;			//重量AD数据累计
int16_t		iWeightRawData = 0;							//重量AD数据


/*
*********************************************************************************************************
*	函 数 名: bsp_InitDWT
*	功能说明: 初始化DWT. 
*********************************************************************************************************
*/
void bsp_InitDWT(void)
{
    
    
	DEM_CR         |= (unsigned int)DEM_CR_TRCENA;   /* Enable Cortex-M4's DWT CYCCNT reg.  */
	DWT_CYCCNT      = (unsigned int)0u;
	DWT_CR         |= (unsigned int)DWT_CR_CYCCNTENA;
}

/*
*********************************************************************************************************
*	函 数 名: DWT_DelayUS
*	功能说明: 这里的延时采用CPU的内部计数实现,32位计数器
*             	OSSchedLock(&err);
*				bsp_DelayUS(5);
*				OSSchedUnlock(&err); 根据实际情况看看是否需要加调度锁或选择关中断
*	形    参: _ulDelayTime  延迟长度,单位1 us
*********************************************************************************************************
*/
void DWT_DelayUS(uint32_t _ulDelayTime)
{
    
    
  uint32_t tCnt, tDelayCnt;
	uint32_t tStart;
		
	tStart = DWT_CYCCNT;                                     /* 刚进入时的计数器值 */
	tCnt = 0;
	tDelayCnt = _ulDelayTime * (SystemCoreClock / 1000000);	 /* 需要的节拍数 */ 		      

	while(tCnt < tDelayCnt)
	{
    
    
		tCnt = DWT_CYCCNT - tStart; /* 求减过程中,如果发生第一次32位计数器重新计数,依然可以正确计算 */	
	}
}


/*
*********************************************************************************************************
*	函 数 名: HX711_GPIOInit
*	功能说明: 重量芯片HX711 GPIO初始化
*********************************************************************************************************
*/
void  HX711_GPIOInit(void)
{
    
    

	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);	//使能PORTA

 	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;				      //PA7 推挽输出 
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		  //推挽输出
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;				      //PA6 推挽输出 
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 		  	//上拉输入
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
}


/*
*********************************************************************************************************
*	函 数 名: HX711_Init
*	功能说明: 重量芯片HX711初始化
*********************************************************************************************************
*/

void HX711_Init(void)
{
    
    
	CH376_SPI_SCS = 1;					//CH376 片选失能	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, DISABLE);	
	SPI_Cmd(SPI1, DISABLE);			//禁止SPI
	HX711_GPIOInit();						//初始化IO
}


/*
*********************************************************************************************************
*	函 数 名: Task_WeightCap
*	功能说明: 重量采集任务
*********************************************************************************************************
*/
void Task_WeightCap(void)
{
    
    
	uint32_t	count = 0;
	int16_t		raw_data = 0;
	uint8_t		i;	
	HX711_Init();						//HX711重新初始化
	
	
	ADSK = 0;
	count= 0;
	while(ADDO);
	
	for(i=0; i<24; i++)
	{
    
    	
		ADSK = 1;
		DWT_DelayUS(1);
		count = count << 1;
		ADSK = 0;
		DWT_DelayUS(1);

		if(ADDO) 
		{
    
    
			count++;		
		}
	}
	
	//第25个时钟信号
	ADSK=1;
	DWT_DelayUS(1);
	ADSK=0;
	DWT_DelayUS(1);

	raw_data = count >> 8;
	
	ilWeightRawDataAddToal = raw_data + ilWeightRawDataAddToal;
	ucWeightCapCount++;
	
	if(ucWeightCapCount == WEIGHT_CAP_NUM)
	{
    
    
		ucWeightCapCount 				= 0;
		iWeightRawData = ilWeightRawDataAddToal / WEIGHT_CAP_NUM;
		ilWeightRawDataAddToal  = 0;
		ucWeightCapCompleteFlag = 1;
	}

}

/*
*********************************************************************************************************
*	函 数 名: GetWeightRawData
*	功能说明: 得到重量的原始值 
*	形    参: iRawData 原始值
*	返 回 值: 1 已经得到 0 没有得到
*********************************************************************************************************
*/
uint8_t  GetWeightRawData(int16_t *iRawData)
{
    
    
		if(ucWeightCapCompleteFlag == 1)
		{
    
    
			*iRawData = iWeightRawData;
			ucWeightCapCompleteFlag = 0;
			return 1;
		}
		
		return 0;
}

Guess you like

Origin blog.csdn.net/qq_32761549/article/details/130981839