Hx711 with pic16f877A

MrChips

Joined Oct 2, 2009
34,828
On mikroC not a specific file . It's like the code C in the post #1 . Please are you ok with my code ?
I don't usually spend time trying to trouble-shoot user's code. This is something that you have to learn to do yourself.
Try to start simple and test one portion at a time.

C:
#define _XTAL_FREQ 20000000
#define DT_PIN RB1_bit
#define SCK_PIN RB0_bit
#define DT_TRIS TRISB1_bit
#define SCK_TRIS TRISB0_bit

long offset=0;
float scale =1;
float known_weight=1000;
void init_hx711(void);
long read_hx711(void);
void tare(void);
void calibrate(float known_weight);
float get_weight(void);

void main() 
{
     UART1_Init(9600);
     delay_ms(100);
     DT_TRIS=1;
     SCK_TRIS=0;
     init_hx711();
     calibrate(known_weight);
     tare();
     //calibrate(float known_weight);

while (1)
  {
  float weight = get_weight();

  char weight_str[10];
  char LuRaw[10];
  UART1_Write_Text("Raw value:  ");
  FloatToStr(read_hx711(), LuRaw);
  UART1_Write_Text("  g\r\n");
   UART1_Write_Text("calibration");
  UART1_Write_Text("Poids:  ");
  FloatToStr(weight, weight_str);
  UART1_Write_Text(" g\r\n");
  delay_ms(1000);
  }  
}

void init_hx711(void) 
{
     SCK_PIN=0;
}
long read_hx711(void) 
{      int i;
     long count =0;
     while (DT_PIN);
     for(i =0; i<24; i++)
     {  SCK_PIN=1;
        delay_us(1);
        count=count <<1;
        SCK_PIN=0;
        delay_us(1);
        if(DT_PIN) count++;
       }
     SCK_PIN =1;
     delay_us(1);
     SCK_PIN=0;
     delay_us(1);
     count^=0x800000;
     return count;
}
void tare (void) 
{
     long sum=0;
     int i;
     for(i =0;i<10;i++) 
     {
             sum+=read_hx711();
             delay_ms(100);
     }   
}

void calibrate( float known_weight){
     long raw_value= read_hx711()-offset;
     scale= raw_value/known_weight;
     }
float get_weight(void)
{
      long raw_value= read_hx711() - offset;
      return raw_value/scale;
 }
 

Thread Starter

Michel09

Joined May 23, 2024
20
I don't usually spend time trying to trouble-shoot user's code. This is something that you have to learn to do yourself.
Try to start simple and test one portion at a time.

C:
#define _XTAL_FREQ 20000000
#define DT_PIN RB1_bit
#define SCK_PIN RB0_bit
#define DT_TRIS TRISB1_bit
#define SCK_TRIS TRISB0_bit

long offset=0;
float scale =1;
float known_weight=1000;
void init_hx711(void);
long read_hx711(void);
void tare(void);
void calibrate(float known_weight);
float get_weight(void);

void main()
{
     UART1_Init(9600);
     delay_ms(100);
     DT_TRIS=1;
     SCK_TRIS=0;
     init_hx711();
     calibrate(known_weight);
     tare();
     //calibrate(float known_weight);

while (1)
  {
  float weight = get_weight();

  char weight_str[10];
  char LuRaw[10];
  UART1_Write_Text("Raw value:  ");
  FloatToStr(read_hx711(), LuRaw);
  UART1_Write_Text("  g\r\n");
   UART1_Write_Text("calibration");
  UART1_Write_Text("Poids:  ");
  FloatToStr(weight, weight_str);
  UART1_Write_Text(" g\r\n");
  delay_ms(1000);
  } 
}

void init_hx711(void)
{
     SCK_PIN=0;
}
long read_hx711(void)
{      int i;
     long count =0;
     while (DT_PIN);
     for(i =0; i<24; i++)
     {  SCK_PIN=1;
        delay_us(1);
        count=count <<1;
        SCK_PIN=0;
        delay_us(1);
        if(DT_PIN) count++;
       }
     SCK_PIN =1;
     delay_us(1);
     SCK_PIN=0;
     delay_us(1);
     count^=0x800000;
     return count;
}
void tare (void)
{
     long sum=0;
     int i;
     for(i =0;i<10;i++)
     {
             sum+=read_hx711();
             delay_ms(100);
     }  
}

void calibrate( float known_weight){
     long raw_value= read_hx711()-offset;
     scale= raw_value/known_weight;
     }
float get_weight(void)
{
      long raw_value= read_hx711() - offset;
      return raw_value/scale;
}
I completely agree with you. and your approach I did it but so far no results
 

geekoftheweek

Joined Oct 6, 2013
1,429
It didn't register yesterday, but

Code:
FloatToStr(read_hx711(), LuRaw);
Is not going to return a valid number. From what I could find on my quick look FloatToStr() expects an IEE754 compliant floating point number which is not the same as a 24 bit number. You need a bit more math to make a floating point number from the result.

The function also does not in anyway print the result of the function to UART. You are missing that step entirely. Add something like

Code:
LongToStr(read_hx711(), LuRaw);
UART1_Write_Text(LuRaw);
Other than that your code seams right. The scope images are questionable, but I am wondering if you have the timing set correctly. Maybe a shorter window or faster sample rate will produce something that looks like what you expect. If not then the delay_ms() is not working correctly. Unfortunately my understanding of C with PICs is pretty weak so I am not sure where to go with that. My language of choice with PICs is assembly.
 

Thread Starter

Michel09

Joined May 23, 2024
20
It didn't register yesterday, but

Code:
FloatToStr(read_hx711(), LuRaw);
Is not going to return a valid number. From what I could find on my quick look FloatToStr() expects an IEE754 compliant floating point number which is not the same as a 24 bit number. You need a bit more math to make a floating point number from the result.

The function also does not in anyway print the result of the function to UART. You are missing that step entirely. Add something like

Code:
LongToStr(read_hx711(), LuRaw);
UART1_Write_Text(LuRaw);
Other than that your code seams right. The scope images are questionable, but I am wondering if you have the timing set correctly. Maybe a shorter window or faster sample rate will produce something that looks like what you expect. If not then the delay_ms() is not working correctly. Unfortunately my understanding of C with PICs is pretty weak so I am not sure where to go with that. My language of choice with PICs is assembly.
thank you for your reply
 
Top