Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 08:10 18 Sep 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : C Help

Author Message
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 09:39am 24 Sep 2014
Copy link to clipboard 
Print this post

I am working a little project that uses a HX711 to read a load cell
The manufacture has provided a reference driver written in C

Could one of the C guru's be so kind to translate the code below into Micromite basic


sbit ADDO = P1^5;
sbit ADSK = P0^0;
unsigned long ReadCount(void){
unsigned long Count;
unsigned char i;
ADDO=1;
ADSK=0;
Count=0;
while(ADDO);
for (i=0;i<24;i++){
ADSK=1;
Count=Count<<1;
ADSK=0;
if(ADDO) Count++;
}
ADSK=1;
Count=Count^0x800000;
ADSK=0;
return(Count);
}


From the above I am sure it reads the 24bit ADC result and stores it in Count.

There a quite a few examples using the Arduino if it helps here is a link to one.
HX711
The Arduino code is complete not just reading the ADC result


Any help here would be greatly appreciated

Jman
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4071
Posted: 10:16am 24 Sep 2014
Copy link to clipboard 
Print this post

hmm...

That's not ordinary C.

P1^5 would be "unlikely" but P0^0 is downright not meaningful.

So, I guess either typos or a funny non-standard compiler. Go for the latter and they're really something like Port1 bit 5 and Port0 bit 0.

So it's pulsing an output pin and seeing what another does.

And that's consistent with the C++ version here

Look at its read routine, where it clocks 24 bits in.

Hope that gets you or someone else started...

JohnEdited by JohnS 2014-09-25
 
vasi

Guru

Joined: 23/03/2007
Location: Romania
Posts: 1697
Posted: 11:43am 24 Sep 2014
Copy link to clipboard 
Print this post

"<<1" means shift to left once
"Count++" means "Count = Count + 1" after the conditional testing.
"for (i=0;i<24;i++)" means "FOR i = 0 to 23 step 1"
"^" means "Binary XOR Operator"
_________
Edit: I suspect is about mikroC for AVR? P0 and P1 may be reference to different ports... It is used in assembler for AVRs.Edited by vasi 2014-09-25
Hobbit name: Togo Toadfoot of Frogmorton
Elvish name: Mablung Miriel
Beyound Arduino Lang
 
G8JCF

Guru

Joined: 15/05/2014
Location: United Kingdom
Posts: 676
Posted: 11:51am 24 Sep 2014
Copy link to clipboard 
Print this post

Taking some guesses, here's a stab which may prompt you or someone with knowledge of the HX117 ! This is a mechanical type translation, eg it's a bit ,like how Google would translate German into English, understandable but not v hight quality, but usually enough to then tidy it up.

Peter



'Whichever pin is the input from the HX711
'sbit ADDO = P1^5;
ADDO=5
SETPIN ADDO,DIN

'Whichever pin is the Output clock to the HX711
'sbit ADSK = P0^0;
ADSK=0
SETPIN ADSK,DOUT

'unsigned long ReadCount(void){
FUNCTION ReadCount()

'unsigned long Count;
LOCAL Count

'unsigned char i;
LOCAL I

'ADDO=1;
'No idea

'ADSK=0;
PIN(ADSK)=0

'Count=0;
Count=0

'while(ADDO);
DO
WHILE(PIN(ADDO)<>0))

'for (i=0;i<24;i++){
FOR I=0 TO 23

'ADSK=1;
PIN(ADSK)=1

'Count=Count<<1;
Count=Count*2

'ADSK=0;
PIN(ADSK)=0

'if(ADDO) Count++;
IF PIN(ADDO)<>0 THEN Count=Count+1

'}
NEXT I

'ADSK=1;
SETPIN(ADSK)=1

'Count=Count^0x800000;
'This next line is really problematic I think
Count = Count XOR 8388608

'ADSK=0;
SETPIN(ADSK)=0

'return(Count);
ReadCount=Count
'}
END FUNCTION

The only Konstant is Change
 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 02:20pm 24 Sep 2014
Copy link to clipboard 
Print this post

Thanks for the reply's

I now have the 24bit number spread over three variable's
Weight(0) to Weight(2) with Weight(0) as the MSB

So the next trick would to turn these into a human readable format
the 24bit number is in the 2's complement format

So how to convert the 24bit ADC reading into weight ?
The load cell is a 20KG device and as a 1mV/V rating
The DAC is full scale at +-20mv with a gain of 128
The DAC full scale has an output of &H7FFFFF and a min &H800000

Regards
Jman






 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 09:27am 25 Sep 2014
Copy link to clipboard 
Print this post

Ok
So we are getting somewhere now.
I now have a reliable reading stored in a single variable

Could somebody please explain the following two lines of C


void set_scale(float scale = 1.f);


void HX711::set_scale(float scale){
SCALE = scale;
}

Many thanks

Jman
 
vasi

Guru

Joined: 23/03/2007
Location: Romania
Posts: 1697
Posted: 10:33am 25 Sep 2014
Copy link to clipboard 
Print this post

@Jman, probably SCALE is a global variable initialized with scale parameter value.
You can omit "HX711::" part, or name the prefixed functions like this "HX711_".
By example, HX711_set_scale .

SCALE and scale are different variables in C language.

void HX711::set_scale(float scale)

void is the value type returned by the set_scale function which is member of HX711 class. Because the returned type is void, then this function it does not return any value, so, in BASIC this will be a procedure or a subroutine. If the type of returned value would have been different than void (so it could be int, float, char, double, etc.), then that would have been a function in BASIC language.

scale parameter is a floating point type variable.Edited by vasi 2014-09-26
Hobbit name: Togo Toadfoot of Frogmorton
Elvish name: Mablung Miriel
Beyound Arduino Lang
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4071
Posted: 10:41am 25 Sep 2014
Copy link to clipboard 
Print this post

Look at the C++ code mentioned before. See the .h file as it has SCALE etc.

Also, the
= 1.f
means default to 1.

John
 
vasi

Guru

Joined: 23/03/2007
Location: Romania
Posts: 1697
Posted: 11:03am 25 Sep 2014
Copy link to clipboard 
Print this post

I hate the C/C++ written like that... is too cryptic. I know that many like it that way, but it can also be written in a more elegant manner. And I'm not pointing only at that way of "default-ing" variables or parameters...
Anyway, that is still readable. It will be harder for Jman at the object initialization part. But maybe not.

__________
Edit: BTW, ignore the first line, you can go without it.Edited by vasi 2014-09-26
Hobbit name: Togo Toadfoot of Frogmorton
Elvish name: Mablung Miriel
Beyound Arduino Lang
 
vasi

Guru

Joined: 23/03/2007
Location: Romania
Posts: 1697
Posted: 02:08pm 25 Sep 2014
Copy link to clipboard 
Print this post

Jman, is relatively simple to port a C++ code where you have a defined class and the code is not obfuscated.

A class (or object) is a collection of variables and functions. When you break the class down to simple functions, the variables inside become global variables (ignore the classification in public and private categories if there is the case, it does not make much sense in a BASIC program). Among the specific functions is there also a init function (subroutine) which it initializes the variables and maybe pins and registers of the microcontroller (if required). That init function must be called first, then you can use the other functions as you need in your program.

As I said, the first C line you speak of, it can be ignored. The second C line is a subroutine definition. Maybe is good to rename the global variable "SCALE" into something like "GSCALE" (from GlobalSCALE) and let the parameter's name the same, "scale", to avoid conflicts in BASIC.

Not looked into the original code but these applies to any C++ Arduino library, where classes are defined/used.
____
Forgot to say that there may be functions with the same name, but with different number of parameters or different types of parameters. In that case, you have to rename those functions and pay attention where and which is used in the the other functions of the class or in the final application, to make the correct replacements.

By example, you can have:
void class_name::same_function_name(param1, param2)
void class_name::same_function_name(param1)
void class_name::same_function_name(param1, param2, param3)

And you rename them like this in your BASIC library (well, I don't know MMBasic so I will rename them like in C):
void class_name_same_function_name_01(param1, param2)
void class_name_same_function_name_02(param1)
void class_name_same_function_name_03(param1, param2, param3)

There the types of parameters are not mentioned, to keep the presentation simple. Edited by vasi 2014-09-27
Hobbit name: Togo Toadfoot of Frogmorton
Elvish name: Mablung Miriel
Beyound Arduino Lang
 
jwettroth

Regular Member

Joined: 02/08/2011
Location: United States
Posts: 79
Posted: 10:09am 25 Sep 2015
Copy link to clipboard 
Print this post

I know this is an old thread but I recently was able to talk to the HX711 with just plain basic- no C required. It's very solid and doesn't violate and timing.

If you look at the datasheet, you'll see that that the HX711 outputs a bit of data each time there is a rising edge on SCLK followed by a falling edge that occurs within 50 uS. The data persists until the next valid clock pulse. It isn't necessary to read the data line while SCK is high as above. Also, if you use a setpin high followed by a set pin low, its still too slow.

The trick is to use the PULSE command on the clock line. In your loop above, you PULSE the clock for something <50 uS (I use 25 uS) and then read the data line at your leisure and loop til done.


I hope this helps someone still.

Bought an old weigh scale at yard scale with a broken display, popped a uMite and little LCD in it and have a nice little scale that I can customize.

Regards All,

John Wettroth
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025