cancel
Showing results for 
Search instead for 
Did you mean: 

HX711 Load cell with STM32 Doubt.

Killstreet30
Associate III

Hi,

I'm trying to interface the HX711 load cell with an STM32 MCU. The resultant values are varying by a huge margin after every interval so please help. I'm, uploading my code

So PA6 - GPIO Input - DOUT pin

PB9 - GPIO Output - Pulldown - SCLK Pin

#include "load.h"

uint32_t val = 0;

uint8_t dout;

extern TIM_HandleTypeDef htim1;

void delay_us (uint16_t us)

 {

  __HAL_TIM_SET_COUNTER(&htim1,0);

  while (__HAL_TIM_GET_COUNTER(&htim1) < us);

 }

void load_init(){

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);

}

uint32_t load_value(){

for (uint8_t i = 0; i < 24; i++){

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);

delay_us(1);

dout = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6);

val = val << 1;

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);

delay_us(1);

if(dout){

val++;

}

}

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);

val = val ^ 0X800000;

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);

return val;

}

In my main function:

HAL_TIM_Base_Start(&htim1);

 while (1)

 {

 while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6) == GPIO_PIN_RESET){

 res = load_value();// final result of load cell

 HAL_Delay(1000);

  /* USER CODE END WHILE */

 }

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

Thanks!

5 REPLIES 5
Uwe Bonnes
Principal III

The HX711 resets if clock is high for > 50 us. Probably some interrupt task triggers that condition, so your measurement get wrong results for time to time. Use some hardware mean (timer, uart) to produce the waveform or disable interrupts when high is output.

Doesn't the delay_us function take care of this? Cause I have set the delay to 1us and after that it toggles right?

No, simply guarantees a minimum delay time, if the MCU does a lot of other stuff under interrupt or call-backs, it could be doing that work too,and consuming more/unbounded time.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Oh okay yeah I do have two interrupts set up and what you said surely makes sense, I'm just wondering how I'm going to tackle that cause both my interrupts aren't triggered as of yet.

surrvesh
Associate II

hey im making connection of stm32 to load cell using hx711 , i am not able to run this code for a while . can you connect through linkedin and help me out . This is my id : surrvesh