Electrical – How read count of 24 bit ADC HX711

adc

Here I have two options but both are giving me a problem.

op_1) make my own C function to read count which is similar to Arduino shiftIn() function but I don't know how to do it …
Actully Arduino have ready made library for HX711 24bit ADC and into that library I get following function which return me adc_count

long read(void) 
{
    // wait for the chip to become ready
    while (!is_ready()) 
        {
        // Will do nothing on Arduino but prevent resets of ESP8266 (Watchdog Issue)
        yield();
    }

    unsigned long value = 0;
    uint8_t data[3] = { 0 };
    uint8_t filler = 0x00;

    // pulse the clock pin 24 times to read the data
    data[2] = shiftIn(DOUT, PD_SCK, MSBFIRST);
    data[1] = shiftIn(DOUT, PD_SCK, MSBFIRST);
    data[0] = shiftIn(DOUT, PD_SCK, MSBFIRST);

    // set the channel and the gain factor for the next reading using the clock pin
    for (unsigned int i = 0; i < GAIN; i++)
    {
        digitalWrite(PD_SCK, HIGH);
        digitalWrite(PD_SCK, LOW);
    }

    // Replicate the most significant bit to pad out a 32-bit signed integer
    if (data[2] & 0x80) 
    {
        filler = 0xFF;
    } else 
    {
        filler = 0x00;
    }

    // Construct a 32-bit signed integer
    value = ( static_cast<unsigned long>(filler) << 24
            | static_cast<unsigned long>(data[2]) << 16
            | static_cast<unsigned long>(data[1]) << 8
            | static_cast<unsigned long>(data[0]) );

    return static_cast<long>(value);
}

Here in this function they have used the shiftIn() function.
How to implement this using C language ?

op_2) I have written my own read() function which return ADC count but I get garbage value ….

unsigned long read(void)
{
    unsigned long count,dout;
    uint8_t i;  
    count = 0;
    dout = 0;
    
    if(isready() == 1)
    {
        GPIO_ResetBits(GPIOC,PD_SCK);
    }
    else
    {
        for(i=0;i<24;i++)
        {
            GPIO_SetBits(GPIOC,PD_SCK);
            delay(1);
            GPIO_ResetBits(GPIOC,PD_SCK);
            dout = GPIO_ReadInputDataBit(GPIOC,DOUT);
            count = (count | dout) << 1;
      delay(1);         
        }
   }
    GPIO_SetBits(GPIOC,PD_SCK);
    GPIO_ResetBits(GPIOC,PD_SCK);
    count = count >> 1;
    return count;
}

Best Answer

Arduino shiftin() is easy to port so that seems the simplest solution.

I posted a set of hx711 code here https://dannyelectronics.wordpress.com/2017/06/29/hx711-code-available/

It has links to the original work I did with the chip as well as downable code. You should be able to get it going on any chip easily.