Skip to main content
Killstreet30
Associate III
March 7, 2023
Question

HX711 Load cell with STM32 Doubt.

  • March 7, 2023
  • 2 replies
  • 5766 views

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!

This topic has been closed for replies.

2 replies

Uwe Bonnes
Chief
March 7, 2023

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.

Killstreet30
Associate III
March 7, 2023

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

Tesla DeLorean
Guru
March 7, 2023

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 VenmoUp vote any posts that you find helpful, it shows what's working..
Associate
March 19, 2024

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