HX711 Lib Port

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
bcclsn
Posts: 6
Joined: Fri Oct 13, 2017 3:59 pm
Has thanked: 2 times

HX711 Lib Port

Postby bcclsn » Sun Dec 20, 2020 11:12 am

hi,
I'm trying to make a library for the HX711 sensor ((https://cdn.sparkfun.com/datasheets/Sen ... nglish.pdf).)
I took a cue from github's stm32 library (HAL):
- https://github.com/freakone/HX711
- https://github.com/nimaltd/HX711
and rewrote the code for chibios ... but I can't read the data :(

I've only one load cell and I've connected as indicate in the following picture

Image

this is my read function... I read always 0 (return 0 caused by the timeout)

Code: Select all

uint32_t HX711_Value( void ) {
    uint32_t data = 0;
    uint8_t  cont = 0;

    while( (data = palReadLine( LINE_DATA )) == PAL_HIGH ) {
        chThdSleepMilliseconds( 5 );
        cont++;

        if( cont < TIMEOUT ) {
            return 0;
        }
    }

    for( uint8_t i=0 ; i<24 ; i++ ) {
        palSetLine( LINE_SCK );
        chThdSleepMicroseconds( 1 );

        data = data << 1;

        palClearLine( LINE_DATA );
        chThdSleepMicroseconds( 1 );

        if( (data = palReadLine( LINE_DATA )) == PAL_HIGH ) {
            data++;
        }
    }
   
    palSetLine( LINE_SCK );
    chThdSleepMicroseconds( 1 );
   
    data = data ^ 0x800000;
   
    palClearLine( LINE_DATA );
    chThdSleepMicroseconds( 1 );

    return data;
}


LINE_DATA is

Code: Select all

palSetLineMode( LINE_DATA, PAL_MODE_INPUT_PULLUP );   
palSetLine( LINE_DATA );


LINE_SCK is

Code: Select all

palSetLineMode( LINE_SCK, PAL_MODE_OUTPUT_PUSHPULL );
palClearLine( LINE_SCK );


I want to use PAL_STM32_OSPEED_LOWEST for both the pin but I don't know how declare it in addition to INPUT_PULLUP and OUTPUT_PUSHPULL... I'll try with && but it doesn't work.

so, please, can you give me some advice?

regards,
alex

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: HX711 Lib Port

Postby Giovanni » Sun Dec 20, 2020 11:33 am

Hi,

Speed is only for output pins, you need to use (PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_LOWEST).

giovanni

bcclsn
Posts: 6
Joined: Fri Oct 13, 2017 3:59 pm
Has thanked: 2 times

Re: HX711 Lib Port

Postby bcclsn » Sun Dec 20, 2020 2:24 pm

ahh sorry! :o

I understood that the F103C8T6 does not support that mode, so I started trying on a F401RE-NUCLEO64.
I always end up in the return 0 loop, as if the sensor was never ready for data transfer ... I don't know why, unfortunately

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: HX711 Lib Port

Postby Giovanni » Sun Dec 20, 2020 3:43 pm

How is LINE_DATA defined? if you look at demos there is a input check on the board button, you need to do exactly like that.

Giovanni

bcclsn
Posts: 6
Joined: Fri Oct 13, 2017 3:59 pm
Has thanked: 2 times

Re: HX711 Lib Port

Postby bcclsn » Sun Dec 20, 2020 4:12 pm

it is defined as

Code: Select all

palSetLineMode( LINE_DATA, PAL_MODE_INPUT_PULLUP );   
palSetLine( LINE_DATA );


which button? I think I did not understand

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: HX711 Lib Port

Postby Giovanni » Sun Dec 20, 2020 7:12 pm

Giovanni wrote:How is LINE_DATA defined?


Focus on this part.

Giovanni

bcclsn
Posts: 6
Joined: Fri Oct 13, 2017 3:59 pm
Has thanked: 2 times

Re: HX711 Lib Port

Postby bcclsn » Sun Dec 20, 2020 8:05 pm

ops sorry, both SCK and DATA are defined as

Code: Select all

 
PAL_LINE(GPIOB, xU)

where x is 3 for SCK and 4 for DATA pin. I've copied the define from board.h

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: HX711 Lib Port

Postby Giovanni » Sun Dec 20, 2020 8:25 pm

Are you sure you are not reading zero because the input pin is actually at zero?

Giovanni

bcclsn
Posts: 6
Joined: Fri Oct 13, 2017 3:59 pm
Has thanked: 2 times

Re: HX711 Lib Port

Postby bcclsn » Sun Dec 20, 2020 9:06 pm

data stays at zero because I get stuck in

Code: Select all

while ((data = palReadLine (LINE_DATA)) == PAL_HIGH)

so date initialized to zero is incremented to 1 from the previous reading and then 0 is returned because I enter in the while loop.

the problem is that from the datasheet I read that "When output data is not ready for retrieval, digital output pin DOUT is high."
so I get zero because the data is never ready... but why?!
I think it is a code problem because I have tested the same load cell and the same hx711 with an arduino nano ( sorry :roll: :lol: ) and i am sure it works

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: HX711 Lib Port

Postby Giovanni » Mon Dec 21, 2020 9:02 am

I don't know that device sorry.

I would start by attaching an oscilloscope or digital analyzer to data and clock lines and see what is going on there.

Giovanni


Return to “ChibiOS/HAL”

Who is online

Users browsing this forum: No registered users and 14 guests