View previous topic :: View next topic |
Author |
Message |
odemmo
Joined: 26 Jun 2007 Posts: 19
|
Posted: Tue Jan 28, 2020 1:40 pm Post subject: HX711 |
|
|
Does anyone have a code to read data from HX711, I just need to read the weight from the load cell and write them to serial, I have searched a lot on the internet without finding anything, please.
(BASCOM-AVR version : 2.0.8.2 ) |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5921 Location: Holland
|
Posted: Tue Jan 28, 2020 2:36 pm Post subject: |
|
|
the data sheet is reasonable clear.
you can use SHIFTIN or a simple BASIC for next.
The number of pulses selects which channel, and the gain.
I have no code but it looks simple to me from a quick peek in the datasheet. _________________ Mark |
|
Back to top |
|
|
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
|
Back to top |
|
|
odemmo
Joined: 26 Jun 2007 Posts: 19
|
Posted: Thu Jan 30, 2020 9:52 am Post subject: |
|
|
thanks, this solves my "problem", just lots of stripping before i'm done here. |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5921 Location: Holland
|
Posted: Thu Jan 30, 2020 2:26 pm Post subject: |
|
|
well done O-Family ! _________________ Mark |
|
Back to top |
|
|
odemmo
Joined: 26 Jun 2007 Posts: 19
|
Posted: Thu Jan 30, 2020 8:11 pm Post subject: |
|
|
so far this work for me
$regfile = "m16def.dat"
$crystal = 3686400
$baud = 9600
$hwstack = 64
$swstack = 10
$framesize = 24
Hx711_clk Alias Portd.2
Hx711_dat Alias Pind.3
Dim Hx711ad As Long
Dim Hx711avg As Long
Dim Hx711count As Byte
Dim Average(16) As Long
Dim Avgpoi As Byte
Dim Avgsum As Long
Dim Zeroadj As Long
Dim Stablecount As Byte
Dim Lastvalue As Long
Dim Sensorcal As Long
'
Dim Temp1 As Byte
Dim Temp2 As Byte
Dim Templ1 As Long
Dim Tempstr As String * 40
'
Dim Dummy As Eram Long
Dim Eepprgid As Eram Long
Dim Eepsensorcal As Eram Long
Config Hx711_clk = Output
Reset Hx711_clk
Config Hx711_dat = Input
If Eepprgid <> &H31375848 Then
Eepprgid = &H31375848
Eepsensorcal = 30065
End If
Sensorcal = Eepsensorcal
For Temp1 = 1 To 20
Do
Loop Until Hx711_dat = 0
'
Gosub Hx711get
Gosub Hx711movavg
Locate 2 , 1
Print Hex(hx711avg)
Next Temp1
Zeroadj = 0 - Hx711avg
Stablecount = 0
Lastvalue = 0
Temp2 = 0
Main:
If Hx711_dat = 0 Then
Gosub Hx711get
Gosub Hx711movavg
Templ1 = Hx711avg + Zeroadj
Templ1 = Templ1 * 100
Templ1 = Templ1 / Sensorcal
'
Temp2 = Temp2 + 1
If Temp2 > 10 Then
Temp2 = 0
End If
waitms 100
Tempstr = Str(templ1)
If Templ1 < 0 Then
print Format(tempstr , " 0")
If Temp2 = 0 Then
Print Format(tempstr , " 0")
End If
Else
print Format(tempstr , " 0")
If Temp2 = 0 Then
Print Format(tempstr , " 0")
End If
End If
If Templ1 = Lastvalue Then
Stablecount = Stablecount + 1
If Stablecount > 5 Then
Stablecount = 5
End If
Else
Lastvalue = Templ1
Stablecount = 0
End If
End If
Goto Main
Hx711get:
Hx711ad = 0
For Hx711count = 23 To 0 Step -1
Set Hx711_clk
Waitus 1
Hx711ad.hx711count = Hx711_dat
Reset Hx711_clk
Waitus 1
Next Hx711count
Set Hx711_clk
Waitus 1
Reset Hx711_clk
Waitus 1
'
If Hx711ad.23 = 1 Then
Hx711ad = Hx711ad Or &HFF000000
End If
Return
Hx711movavg:
Avgsum = Avgsum - Average(avgpoi)
Average(avgpoi) = Hx711ad
Avgsum = Avgsum + Hx711ad
'
Avgpoi = Avgpoi + 1
If Avgpoi > 16 Then
Avgpoi = 1
End If
'
Hx711avg = Avgsum
Shift Hx711avg , Right , 4 , Signed
Return
eid |
|
Back to top |
|
|
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
Posted: Fri Jan 31, 2020 2:31 am Post subject: |
|
|
I'm glad I could help you!
Calibration and zero adjustment are required depending on the type of load cell, so please devise it. |
|
Back to top |
|
|
odemmo
Joined: 26 Jun 2007 Posts: 19
|
Posted: Sat Feb 01, 2020 10:51 am Post subject: |
|
|
O-Family wrote: | I'm glad I could help you!
Calibration and zero adjustment are required depending on the type of load cell, so please devise it. |
Thanks, the app in my Mac take care of calibration and zero adjustment but I will look at it later. |
|
Back to top |
|
|
odemmo
Joined: 26 Jun 2007 Posts: 19
|
Posted: Thu Nov 26, 2020 10:31 am Post subject: |
|
|
Next, how can I read data from 4 pcs. hx711 with 1 Atmega 8? |
|
Back to top |
|
|
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
Posted: Fri Nov 27, 2020 1:29 pm Post subject: |
|
|
All you have to do is assign [PD_SCK] and [DOUT] for 4 circuits to different ports and execute a program to extract A/D data for each port. |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sat Nov 28, 2020 7:34 am Post subject: |
|
|
Hello. Im also using HX711 in some of my projects but I cant wait for the measure (SERIALS/DOS,ETH/temperatures etc.). So I handle it similarly like DS18B20.
I cut this code from one of bigger projects.
Because HX711 is temperature sensitive then one TARE is not enough. I add simple Autotare that walk thru chip is gettin warm from rest of electronic.
Maybe this can be handy for someone.
Code: | $regfile = "m2560def.dat" '8K SRAM | 4K EEPROM
$crystal = 16000000
$hwstack = 256
$swstack = 128
$framesize = 256
$baud = 115200
Config Submode = New
'*****************************************************************
'* PROGRAM CONSTANTS/SETTINGS *
'*****************************************************************
' HX711
Const Number_of_calibrations = 5
Const Number_of_readings = 5
'***********************************************
'* ALPHANUMERIC LCD *
'***********************************************
Config Lcdpin = Pin , Db4 = Portc.3 , Db5 = Portc.5 , Db6 = Portc.7 , Db7 = Porta.6 , E = Portl.5 , Rs = Portl.1 ' , Wr = Porta.1
Config Lcd = 20x4
Initlcd : Cursor Off , Noblink : Cls
'*****************************************************************
'* TIMER2 FOR TIME BASE *
'*****************************************************************
Config Timer2 = Timer , Prescale = 1024 , Compare_a = Disconnect , Compare_b = Disconnect , Clear_timer = 1
Compare2a = 155 '10ms @16MHz/1024
Dim 100ms As Byte
'*****************************************************************
'* VARIABLES *
'*****************************************************************
'multipurpose
Dim N As Byte , Helpstr As String * 20 , Helpb As Byte , Helpl As Long , Tempb As Byte , Helps As Single
Dim Helpw As Word
'HX711
Dim Hx711 As Long , Gramm As Single , Gramy As Long , Hx711_state As Byte , Prev_cnt As Byte
Dim Finalstr As String * 20 , C_niuton_str As String * 20 , Niutony As Single , Prev_hx As Long
Dim Niuton_str As String * 20 , Number_of_reads As Byte , Offset As Long , Temp_sum As Long
'*****************************************************************
'* HX711 *
'*****************************************************************
'When on CLK pin state is HI then after 600us chip goes to sleep mode
'It wake up after first LO state on the CLK
'Then HX711 starts to measure and set its DATA pin HI
'When measure is ready then chip reset DATA pin into LO and we can shiftin data
'Data is 24 bits long, but additional pulse on the CLK we set config for new measure
'Thats we shiftin 25 bits and then cutof that unwanted bit in the data.
Config Portf.6 = Input : Datapin Alias Pinf.6 : Set Datapin
Config Portf.7 = Output : Clockpin Alias Portf.7 ': Set Clockpin
'-Function reads 25 bits, then shift last one and return 24 bits-
Function Read_raw()as Long
Local Watch As Word : Watch = 62767 'if chip is not present
Local Scale As Long
Do
Decr Watch
If Datapin = 0 Then Watch = 0
Loop Until Watch = 0
Shiftin Datapin , Clockpin , Scale , 1 , 25 '24for data + 1clk for 128gain canal A
Shift Scale , Right , 1 , Signed
'Debug Scale
Read_raw = Scale
End Function
Do
'--[ TIMER2 10ms ]----------------------------
If Tifr2.ocf2a = 1 Then
Tifr2.ocf2a = 1
'every10ms
'##################### HX711 ###############
'--[HX711 data rate is set into 10Hz (100ms)]--
'Reset Arduino_led 'reset 10ms blink about new measure
Select Case Hx711_state
Case 0 'only after boot
Locate 4 , 1 : Lcd "Calib "
Number_of_reads = Number_of_calibrations
Incr Hx711_state
Case 1 '--[calibrating 5x100ms]--
'Debug "Cal " ;
Temp_sum = Temp_sum + Read_raw()
Decr Number_of_reads
If Number_of_reads = 0 Then
Temp_sum = Temp_sum / Number_of_calibrations
Offset = Temp_sum
Temp_sum = 0
Incr Hx711_state
Locate 4 , 1 : Lcd Spc(9)
'Set Arduino_led
Else
Locate 4 , 7 : Lcd Number_of_reads ; " "
End If
Case 2 '--[normal work after calibrating 5x100ms]--
'Debug "Raw " ;
Temp_sum = Temp_sum + Read_raw()
Incr Number_of_reads
If Number_of_reads = Number_of_readings Then
Number_of_reads = 0
Temp_sum = Temp_sum / Number_of_readings
Hx711 = Temp_sum - Offset
Temp_sum = 0
Shift Hx711 , Right , 12 , Signed 'scale down 24->12 bits (for MCP7426)
'Gramm = Hx711 * 0.002326 'tensometr 5kg BARTek
Gramm = Hx711 * 0.09045 'bigger tensometr
Gramy = Gramm 'Single to Long casting, in dkg
If Gramy < 0 Then Gramy = 0 'never below zero
Helpl = Gramy 'copy
Helpstr = Str(helpl)
Helpstr = Format(helpstr , "0.0") + "kg"
Helpb = Len(helpstr)
If Helpb < 9 Then
Helpb = 9 - Helpb
Finalstr = Space(helpb)
Else
Finalstr = ""
End If
Finalstr = Finalstr + Helpstr
Finalstr = Right(finalstr , 9)
Locate 3 , 1 : Lcd Finalstr
'---[CALCULATE FORCE]----
'1kg=9,80665 N 100g = 0,980665
Helps = Gramy * 9.80665
Niutony = Helps / 10 'dKg->Kg
C_niuton_str = Fusing(niutony , "#.#")
Niuton_str = C_niuton_str + "Nm"
Helpb = Len(niuton_str)
If Helpb < 9 Then
Helpb = 9 - Helpb
Finalstr = Space(helpb)
Else
Finalstr = ""
End If
Finalstr = Finalstr + Niuton_str
Finalstr = Right(finalstr , 9)
Locate 4 , 1 : Lcd Finalstr
'Set Arduino_led 'blink about new measure
'-----[ AUTOTARE ]-----
Select Case Gramy
Case -5 To 5
If Prev_hx = Gramy Then
If Prev_cnt < 5 Then
Incr Prev_cnt
Else
Prev_cnt = 0
Shift Prev_hx , Left , 12 , Signed
Offset = Offset + Prev_hx
'Debug "Cor " ; Prev_hx 'correction= (how many offset is changed)
End If
Else
Prev_cnt = 0
End If
Prev_hx = Gramy 'always remember prev
Case Else
Prev_cnt = 0
End Select
'-------------[AUTOTARE END]
End If
End Select
'--[ HX711 End ]--
End If
Loop
|
|
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5921 Location: Holland
|
Posted: Mon Nov 30, 2020 3:41 pm Post subject: |
|
|
thanks for sharing. indeed good practice to calibrate. _________________ Mark |
|
Back to top |
|
|
odemmo
Joined: 26 Jun 2007 Posts: 19
|
Posted: Thu Jan 14, 2021 1:36 pm Post subject: |
|
|
O-Family wrote: | All you have to do is assign [PD_SCK] and [DOUT] for 4 circuits to different ports and execute a program to extract A/D data for each port. |
This became a little too difficult for an amateur like me, how can I get someone to make the code for me, it is important to me since there is no finished equipment for this. What I need is to connect 4 pcs. HX711 to a microcontroller which then reads in the data in weight1, weight2, weight3, weight4 and then prints as,
Print weight1; weight2; weight3; weight4
Can I pay anything for it, Paypal?
Oddvar |
|
Back to top |
|
|
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
Posted: Thu Jan 14, 2021 2:15 pm Post subject: |
|
|
You can create four types of ports for the program you copied and worked on, and call each of them.
Code: | Hx711_clk Alias Portd.2
Hx711_dat Alias Pind.3
'
' ******************************************************************************
' * Subroutine to retrieve A/D data from channel A of HX711 with a gain of 128 * (Hx711ad = 2's complement format 24bit)
' ******************************************************************************
'
Hx711get:
Hx711ad = 0
For Hx711count = 23 To 0 Step -1 'Extract 24-bit data.
Set Hx711_clk 'Set the [PD_SCK] pin of the HX711 to [H].
Waitus 1 'Period of [H]. (T3 = 1uS)
Hx711ad.hx711count = Hx711_dat 'Place the data of the [DOUT] pin of HX711 in each bit.
Reset Hx711_clk 'Set the [PD_SCK] pin of the HX711 to [L].
Waitus 1 'Period of [L]. (T4 = 1uS)
Next Hx711count
'
' * Send the 25th clock pulse (set channel A and gain to 128 for the next conversion) *
'
Set Hx711_clk 'Set the [PD_SCK] pin of the HX711 to [H].
Waitus 1 'Period of [H]. (T3 = 1uS)
Reset Hx711_clk 'Set the [PD_SCK] pin of the HX711 to [L].
Waitus 1 'Period of [L]. (T4 = 1uS)
'
If Hx711ad.23 = 1 Then 'Is the 24-bit value of A/D a negative value?
Hx711ad = Hx711ad Or &HFF000000 'Set to a negative value of 32 bits.
End If
Return |
Assign 4 types of ports directly to 4 types of subroutines as shown below.
Code: | Hx711get1:
Hx711ad = 0
For Hx711count = 23 To 0 Step -1 'Extract 24-bit data.
Set Portd.2 'Set the [PD_SCK] pin of the HX711 to [H].
Waitus 1 'Period of [H]. (T3 = 1uS)
Hx711ad.hx711count = Pind.3 'Place the data of the [DOUT] pin of HX711 in each bit.
Reset Portd.2 'Set the [PD_SCK] pin of the HX711 to [L].
Waitus 1 'Period of [L]. (T4 = 1uS)
Next Hx711count
'
' * Send the 25th clock pulse (set channel A and gain to 128 for the next conversion) *
'
Set Portd.2 'Set the [PD_SCK] pin of the HX711 to [H].
Waitus 1 'Period of [H]. (T3 = 1uS)
Reset Portd.2 'Set the [PD_SCK] pin of the HX711 to [L].
Waitus 1 'Period of [L]. (T4 = 1uS)
'
If Hx711ad.23 = 1 Then 'Is the 24-bit value of A/D a negative value?
Hx711ad = Hx711ad Or &HFF000000 'Set to a negative value of 32 bits.
End If
Return
'---------------------------------------------------------------
Hx711get2:
Hx711ad = 0
For Hx711count = 23 To 0 Step -1 'Extract 24-bit data.
Set Portd.4 'Set the [PD_SCK] pin of the HX711 to [H].
Waitus 1 'Period of [H]. (T3 = 1uS)
Hx711ad.hx711count = Pind.5 'Place the data of the [DOUT] pin of HX711 in each bit.
Reset Portd.4 'Set the [PD_SCK] pin of the HX711 to [L].
Waitus 1 'Period of [L]. (T4 = 1uS)
Next Hx711count
'
' * Send the 25th clock pulse (set channel A and gain to 128 for the next conversion) *
'
Set Portd.4 'Set the [PD_SCK] pin of the HX711 to [H].
Waitus 1 'Period of [H]. (T3 = 1uS)
Reset Portd.4 'Set the [PD_SCK] pin of the HX711 to [L].
Waitus 1 'Period of [L]. (T4 = 1uS)
'
If Hx711ad.23 = 1 Then 'Is the 24-bit value of A/D a negative value?
Hx711ad = Hx711ad Or &HFF000000 'Set to a negative value of 32 bits.
End If
Return
'---------------------------------------------------------------
Hx711get3:
'---------------------------------------------------------------
Hx711get4: |
If your technology makes it difficult to create a complete program, please contact me directly on the bulletin board on my site. (English is also acceptable) |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|