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

hardware

HX711 internal block diagram
insert image description here

In protues, as shown in the figure below,
insert image description here
the pin description is as follows:
insert image description here

insert image description here

analog input

The Channel A analog differential input can be interfaced directly to the differential output of a bridge sensor. Since the output signal of the bridge sensor is relatively small, in order to make full use of the input dynamic range of the A/D converter, the programmable gain of this channel is relatively large, which is 128 or 64. These gains correspond to a full-scale differential input voltage of ±20mV or ±40mV, respectively. Channel B is a fixed gain of 32, corresponding to a full-scale differential input voltage of ±80mV. Channel B is used to detect system parameters including battery.

Power supply

The digital power supply (DVDD) should use the same digital power supply as the MCU chip. The regulator circuit in the HX711 chip can provide analog power to the A/D converter and external sensors at the same time. The supply voltage (VSUP) of the regulated power supply can be the same as that of the digital power supply (DVDD). The output voltage value (VAVDD) of the regulated power supply is determined by the external voltage divider resistors R1, R2 and the output reference voltage VBG of the chip (Figure 1), VAVDD=VBG(R1+R2)/R2. This output voltage should be chosen to be at least 100mV lower than the input voltage (VSUP) of the regulated power supply. If you do not use the voltage regulator circuit in the chip, the pin VSUP and the pin AVDD should be connected, and connected to a low-noise analog power supply with a voltage of 2.6-5.5V. There is no need to connect an external capacitor to the pin VBG, the pin VFB should be grounded, and the pin BASE is not connected.

clock selection

If the pin XI is grounded, HX711 will automatically choose to use the internal clock oscillator, and automatically close the external clock input and the related circuits of the crystal oscillator. In this case, typical output data rates are 10Hz or 80Hz. If an accurate output data rate is required, an external input clock can be connected to the XI pin through a 20pF DC blocking capacitor, or a crystal oscillator can be connected to the XI and XO pins. In this case, the on-chip clock oscillator circuit is automatically turned off, and a crystal oscillator clock or an external input clock circuit is used. At this point, if the crystal oscillator frequency is 11.0592MHz, the output data rate is exactly 10Hz or 80Hz. The output data rate scales up or down with the crystal frequency in the above relationship. When using an external input clock, the external clock signal does not necessarily need to be a square wave. The clock signal on the crystal oscillator output pin of the MCU chip can be connected to the XI pin through a 20pF DC blocking capacitor as an external clock input. The amplitude of the external clock input signal can be as low as 150mV.

Serial communication

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.

Reset and power down

When the chip is powered on, the power-on automatic reset circuit in the chip will automatically reset the chip. The pin PD_SCK input is used to control the power off of HX711. When PD_SCK is low level, the chip is in normal working state.

If PD_SCK changes from low level to high level and remains at high level for more than 60μs, HX711 will enter the power-off state

If the on-chip regulated power supply circuit is used, when the power is turned off, the external sensor and the on-chip A/D converter will be powered off at the same time. When PD_SCK returns to low level again, the chip will automatically reset and enter normal working state. After the chip enters normal working state from reset or power-off state, channel A and gain 128 will be automatically selected as the input channel and gain of the first A/D conversion. The subsequent input channel and gain selection are determined by the number of pulses of PD_SCK, see the section on serial communication. After the chip enters the normal working state from the reset or power-off state, 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.

PCB design of relevant parts of HX711

PCB board reference design circuit diagram of relevant parts of HX711
insert image description here

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/130945606