Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

HX711
Goto page Previous  1, 2
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
odemmo

Bascom Member



Joined: 26 Jun 2007
Posts: 19

norway.gif
PostPosted: Thu Jan 21, 2021 12:59 pm    Post subject: Reply with quote

O-Family wrote:
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)



Thanks, but where / how to call up these subroutines?

Oddvar
Back to top
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1019
Location: France

france.gif
PostPosted: Thu Jan 21, 2021 3:10 pm    Post subject: Reply with quote

hi,
do you know what is a sub ?
see the help for Gosub
I prefer use call subxxxxx() or using function
that is the fondamental of basic language.

I did an help about starting with Basic langage in french, if you need it let me your mail
JP

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
odemmo

Bascom Member



Joined: 26 Jun 2007
Posts: 19

norway.gif
PostPosted: Thu Jan 21, 2021 7:33 pm    Post subject: Reply with quote

I am aware of Gosub but where in my code should I insert Gosub Hx711get1, Gosub Hx711get2, Gosub Hx711get3 abd Gosub Hx711get4.
I am aware of the principle of Basic since the 80's with CBM64 but Bascom Basic is more advanced Basic than what I learned then.
I really need this gadget that reads all 4 weights one by one.

I speak only Norwegian and google English Smile
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5319
Location: Holland

blank.gif
PostPosted: Thu Jan 21, 2021 8:32 pm    Post subject: Reply with quote

there are multiple solutions.
the most simple would be when you use a full port for example PORTB.
Say portb.0 and portb.1 for the first device, portb.2 and portb.3 for the next, etc.

code becomes :

Code:
 For Hx711count = 23 To 0 Step -1                         'Extract 24-bit data.
      Set PORTB.0   'clock of first device
      SET PORTB.2  'clock of second device
      SET PORTB.4
      SET PORTB.6  
      Waitus 1                                              'Period of [H]. (T3 = 1uS)
      Hx711ad1.hx711count = PINB.1
      Hx711ad2.hx711count = PINB.3
      Hx711ad3.hx711count = PINB.5
      Hx711ad4.hx711count = PINB.7
      Reset PORTB.0
      Reset PORTB.2
      Reset PORTB.4
      Reset PORTB.6
      Waitus 1                                              'Period of [L]. (T4 = 1uS)
   Next Hx711count
 



this is just a partial sample to show the idea where you perform the operations on 4 devices at once. this also requires 4 variables to contain the data.
you could also make a sub routine where you pass the bit numbers of the port.

and you can simple copy the code and then assign 4 different ports as the author suggested.

_________________
Mark
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5319
Location: Holland

blank.gif
PostPosted: Thu Jan 21, 2021 8:35 pm    Post subject: Reply with quote

- you should start with the code you have for 1 device.
- then show us which pins you have for the other 3 devices.

_________________
Mark
Back to top
View user's profile Visit poster's website
odemmo

Bascom Member



Joined: 26 Jun 2007
Posts: 19

norway.gif
PostPosted: Thu Jan 21, 2021 11:56 pm    Post subject: Reply with quote

I use this code when I have only one HX711

$regfile = "m8def.dat"

$crystal = 3686400

$baud = 9600

$hwstack = 64
$swstack = 10
$framesize = 24

Hx711_clk Alias Portb.0
Hx711_dat Alias Pinb.1

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

end




but now I want to use there more HX711 on port b2 to port b7

Thanks
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 257
Location: Japan

japan.gif
PostPosted: Fri Jan 22, 2021 12:11 am    Post subject: Reply with quote

If the questioner has a high level of programming, we can also suggest simplifying the program.
For example, the [PD_SCK] pin is shared by all four ports and connected to one port, and only the [DOUT] pin is received individually by the four ports.
However, since many modifications of the provided test program are required, I proposed a method to make the four easiest subroutines by copying.

Even so, the questioner often doesn't understand the BASIC language.
You'll have to learn a little more and then ask questions or ask for all the programming.

It also does not provide any hardware information such as the power supply voltage (AVDD) for the HX711 load cell or the characteristics of the load cell used.
Back to top
View user's profile Visit poster's website
odemmo

Bascom Member



Joined: 26 Jun 2007
Posts: 19

norway.gif
PostPosted: Fri Jan 22, 2021 12:29 am    Post subject: Reply with quote

Thanks.
I understand simple basic programming but this is too complicated for an old man like me and therefore I wants lots of help, help with the whole program is of course invaluable help and thanks.
To learn this I think will be very time consuming for a hobby project.
Thanks
Back to top
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 257
Location: Japan

japan.gif
PostPosted: Fri Jan 22, 2021 1:27 am    Post subject: Reply with quote

Yes, I think it takes some effort.

The coefficient calculation of the program changes depending on the power supply voltage (AVDD) for the load cell of the HX711 and the characteristics (resistance value and output voltage) of the load cell.
Also, for each of the four load cells, error adjustment and zero adjustment are required, so simply speaking, the program is quadrupled.
It corresponds to the idea of measuring with four separate AVR circuits and summarizing the four results obtained.

Simply asking only the subroutine part of a program can be difficult to solve if you don't know how the entire program works.
It may take some time, but do your best!
Back to top
View user's profile Visit poster's website
odemmo

Bascom Member



Joined: 26 Jun 2007
Posts: 19

norway.gif
PostPosted: Wed Jan 27, 2021 1:09 am    Post subject: Reply with quote

Finally I got it, tips from you and lots of copying and pasting, I am not a trained programmer but have tried and failed since CBM64 in my youth but this type of programming is very complicated, but I got it.

$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

end
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5319
Location: Holland

blank.gif
PostPosted: Wed Jan 27, 2021 10:15 am    Post subject: Reply with quote

hi

you should use the CODE tags when posting code like this :

Code:
$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
   Bitwait Hx711_dat , Reset                                'instead of do loop
   Gosub Hx711get
   Gosub Hx711movavg
   Locate 2 , 1
   Print Hex(hx711avg)
Next Temp1
Zeroadj = 0 - Hx711avg
Stablecount = 0
Lastvalue = 0
Temp2 = 0


'Main:
Do
   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
Loop

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
End


Also use the Proper Indent feature so you get automatic indention.
In your code i replaced main: goto main with a Do Loop
And the do loop until with a BITWAIT which is the equivalent.

Your code still uses 1 sensor but when it works as you want it is indeed a matter of copy & paste.
One important thing however : it is always a good idea to invest some time in what the end goal is. It is like making an IKEA furniture : best read all the pages before you start.

Now you have something working you can probably also better understand the commands better and you are able to use one of the methods to enhance with multiple sensors.
- simple copy & rename
- do all the 4 sensors at the same time
- make a sub routine and pass info like the bit number.

for example you have 2 bits you use. these you could pass :

Code:
declare function Something(byval sensorpin0 as byte, byval sensorpin1 as byte) as word 'or what ever you return

'call it like this
myword1=Something(0,1)
myword2=Something(2,3)


and in the sub you have a reference to the pin number :
function Something(byval sensorpin0 as byte, byval sensorpin1 as byte) as word 'or what ever you return
   portd.sensorpin0 = 0 'for example
end function
 

you best make a small example without the other code and look in the simulator how it will work.

_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
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