Skip to main content
Visitor II
June 5, 2019
Question

trying to interface with an HX711 load cell amp

  • June 5, 2019
  • 15 replies
  • 12218 views

The HX711 requires the MCU to send an exact number of clock pulses to capture a 24 bit value, then the clock signal must remain low until the HX711 indicates its ready to transmit again, using the data line. I tried to use a GPIO pin to generate the clock signal manually with pin_set and pin_reset calls, which works in that it creates a clock signal at the frequency of the APB, but the amplitude is only 100 mv. When I plug the clock signal into the HX711 the signal becomes low level noise. Here is the pin config:

 /*Configure GPIO pin : SCLK_Pin */
 GPIO_InitStruct.Pin = SCLK_Pin;
 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 GPIO_InitStruct.Pull = GPIO_PULLUP;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 HAL_GPIO_Init(SCLK_GPIO_Port, &GPIO_InitStruct);

and here is the clock gen code:

	// read 24 data bits
	for (i = 0; i < 24; i++) {
	 HAL_GPIO_WritePin (SCLK_GPIO_Port, SCLK_Pin, GPIO_PIN_SET);
	 buffer <<= 1;
	 HAL_GPIO_WritePin (SCLK_GPIO_Port, SCLK_Pin, GPIO_PIN_RESET);
	 buffer += HAL_GPIO_ReadPin (SDATA_GPIO_Port, SDATA_Pin);
	}

I should be getting a 3.3V clock signal. Any suggestions?

    This topic has been closed for replies.

    15 replies

    GrantWAuthor
    Visitor II
    February 23, 2020

    The following code works. Make sure that the buffer (and returned value) are defined uint32_t. The line "buffer ^= 0x800000;" is essential to avoid the huge #'s starting with 8.

    uint32_t CB_SPI_Receive (void) {

    uint32_t buffer = 0U;

    int i, j, k = 1;

    // wait for SDATA to go lo

    while (HAL_GPIO_ReadPin (SDATA_GPIO_Port, SDATA_Pin)) ;

    // wait 300 ns

    for (j = 0; j < 5; j++) k += 19;

    // read 24 data bits

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

      HAL_GPIO_WritePin (SCLK_GPIO_Port, SCLK_Pin, GPIO_PIN_SET);

      buffer <<= 1;

      HAL_GPIO_WritePin (SCLK_GPIO_Port, SCLK_Pin, GPIO_PIN_RESET);

      buffer += HAL_GPIO_ReadPin (SDATA_GPIO_Port, SDATA_Pin);

    }

    // generate one or 3 more clock pulse to set gain for next sample and set bit 23 before returning

    buffer ^= 0x800000;

    // for (i = 0; i < 2; i++) {

    HAL_GPIO_WritePin (SCLK_GPIO_Port, SCLK_Pin, GPIO_PIN_SET);

    HAL_GPIO_WritePin (SCLK_GPIO_Port, SCLK_Pin, GPIO_PIN_RESET);

    // }

    return buffer;

    }

    Graduate II
    February 23, 2020

    Remember that SCK high longer than 50 us resets the chip. So block interrupts before you set SCK high and unblock after you have reset SCK.

    Visitor II
    February 24, 2020

    HI BROS,

    I finally did it.Thank you for your help.

    It works properly.

    CODE:

    #include "main.h"

    #include "stm32f4xx_hal.h"

    void SystemClock_Config(void);

    static void MX_GPIO_Init(void);

    uint8_t value=0;

    uint8_t i = 0;

    int32_t adc_value = 0;

    #define pause for(int n=0;n<100;n++){};

    int main(void)

    {

     HAL_Init();

     SystemClock_Config();

     MX_GPIO_Init();

     while (1)

     {

     while (HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_15));

     for(i=0;i<25;i++)

     {

     HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_SET);

     //pause

     adc_value <<= 1;

     if(HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_15))adc_value++;

     HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_RESET);

     //pause

     };

    for (int i = 0; i < 128; i++) {

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_SET);

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13,GPIO_PIN_RESET);

    }

    //adc_value = adc_value ^ 0x800000;

      while(adc_value>=255)adc_value>>=8;

      value=adc_value;

      adc_value=0;

     }

     }

    void SystemClock_Config(void)

    {

     RCC_OscInitTypeDef RCC_OscInitStruct = {0};

     RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

     /** Configure the main internal regulator output voltage

     */

     __HAL_RCC_PWR_CLK_ENABLE();

     __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

     /** Initializes the CPU, AHB and APB busses clocks

     */

     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

     RCC_OscInitStruct.HSIState = RCC_HSI_ON;

     RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

     RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

     RCC_OscInitStruct.PLL.PLLM = 8;

     RCC_OscInitStruct.PLL.PLLN = 192;

     RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

     RCC_OscInitStruct.PLL.PLLQ = 7;

     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

     {

    Error_Handler();

     }

     /** Initializes the CPU, AHB and APB busses clocks

     */

     RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

     |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;

     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)

     {

    Error_Handler();

     }

    }

    static void MX_GPIO_Init(void)

    {

     GPIO_InitTypeDef GPIO_InitStruct = {0};

     /* GPIO Ports Clock Enable */

     __HAL_RCC_GPIOB_CLK_ENABLE();

     __HAL_RCC_GPIOD_CLK_ENABLE();

     /*Configure GPIO pin Output Level */

     HAL_GPIO_WritePin(GPIOD, LD4_Pin|LD3_Pin|LD5_Pin, GPIO_PIN_RESET);

     /*Configure GPIO pins : LD4_Pin LD3_Pin LD5_Pin */

     GPIO_InitStruct.Pin = GPIO_PIN_13;

     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

     GPIO_InitStruct.Pull = GPIO_PULLUP ;

     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

     HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

     /*Configure GPIO pin : PD15 */

     GPIO_InitStruct.Pin = GPIO_PIN_15;

     GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

     GPIO_InitStruct.Pull = GPIO_PULLUP ;

     HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

    }

    void Error_Handler(void)

    {

    }

    #ifdef USE_FULL_ASSERT

    /**

     * @brief Reports the name of the source file and the source line number

     *   where the assert_param error has occurred.

     * @param file: pointer to the source file name

     * @param line: assert_param error line source number

     * @retval None

     */

    void assert_failed(uint8_t *file, uint32_t line)

    {

     /* USER CODE BEGIN 6 */

     /* User can add his own implementation to report the file name and line number,

    tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

     /* USER CODE END 6 */

    }

    #endif /* USE_FULL_ASSERT */

    /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

    Visitor II
    February 24, 2020

    Congrats mate, thnaks for sharing ☺�?​

    Visitor II
    February 1, 2021

    hey were you able to solve the 8404695 value issue ?

    Visitor II
    February 25, 2020

    Hi Bro's,

    The weight values ​​are read properly, but how can I calibrate? Do you have any idea about this?

    GrantWAuthor
    Visitor II
    February 26, 2020

    That you will have to solve on your own. Hint: its simple math and known weights.

    Visitor II
    November 3, 2020

    Hi everybody... I have the STM32F103C8T6 "bluepill".. i want to conect whit the HX711 using arduino IDE, but doesnt work, someone did this project?