Hx710b: code explained
Communication with Hx710 always initiated by a microcontroller or MASTER, the SLAVE Hx710 only sends STATUS and DATA. If the microcontroller holds the Control line high for more than 60us the Hx710 goes to sleep (low power standby) and the Hx710 holds the Data line high indicating that Data is NOT available. Both Control and Data are high during sleep. When the microcontroller switches the CLK or control line to low the Hx710 wakes up in default mode and reads its amplified inputs and performs a conversion; this takes ~0.4s.
Control example: Saving only 16 bits out of 24.
Change the CLK or control pin to High.
If Hx710 changes the state of data line to Low, Nano should be ready to receive data.
23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
\---------------------/ \---------------------/ \------------------/
bits 16-23 (8 bits) bits 8-15 (8 bits) bits 0-7 (8 bits)
In this 3 bytes protocol (MSB-Middle-LSB), only MSB and Middle saved.
After reaching the 24 Data bits end, the microcontroller must send 1, 2 or 3 Control bits to Hx710.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
loop1:
- read constantly the state of pin D4.
- when D4=0, get out of the loop
- flag_loop = 0
loop2:
If tick > 23 //End of data receiving. Nothing is saved to string now. The bits 16-23 (8 bits) ignored.
- flag_loop = 1 (get out of this loop)
- D3=1
- D3=0
- D3=1
- D3=0
- D3=1
- D3=0
If tick > 7 //Note: Only ‘0’ is added to the end of string here.
- D3=1
- delay 10uS
- D3=0
- delay 10uS
- PrStr16 = PrStr16 + "0"
Else //if tick < 7
//Note: My program receive 8 bits (0 to 7) and save it to 16-bit string.
//
- D3=1
- delay 10uS
- D3=0
- delay 10uS
- save value from input D4 to d_pin (bool)
- save/convert bool value to string tmp_str // [2]
- save tmp_str value to PrStr16[17] ( PrStr16= PrStr16 + .TmpStr1)
tick = tick +1
End of loop
Now.
Convert 16 bit string value to 16 bit integer.
If you now the length of the string, you can read it bit by bit and save it to integer.
Example of integer: 01010101 01010011 (16 bit)
Convert the integer to float and save it.
Divide float by 256.
Convert the float back to integer.
Show/send the value to User.
Please have a look at the programming platform Flowcode ver. 10
It is free for Arduino Nano.
https://www.flowcode.co.uk/