MikroC and HX771 issue with varying output

Status
Not open for further replies.

lerameur

Junior Member level 1
Joined
Oct 19, 2006
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,454
Hello,

I am using the PIC18F46K22 with MikroC language to program and read a HX711 IC.
I am not getting a steady output.
I added a loop (10x) to get an average, but still its fluctuating between 8453470 and 8427890.
Any help would be appreciated.

C:
sbit ADSK at RD2_bit;
sbit ADDO at RD3_bit;

sbit ADSK_Direction at TRISD.B2;
sbit ADDO_Direction at TRISD.B3;

sbit LCD_RS at LATA5_bit;
sbit LCD_EN at LATA4_bit;
sbit LCD_D4 at LATA0_bit;
sbit LCD_D5 at LATA1_bit;
sbit LCD_D6 at LATA2_bit;
sbit LCD_D7 at LATA3_bit;

sbit LCD_RS_Direction at TRISA5_bit;
sbit LCD_EN_Direction at TRISA4_bit;
sbit LCD_D4_Direction at TRISA0_bit;
sbit LCD_D5_Direction at TRISA1_bit;
sbit LCD_D6_Direction at TRISA2_bit;
sbit LCD_D7_Direction at TRISA3_bit;

char txt;
char txt2;
int i, j;
unsigned long data_average;
unsigned long count;


void main(void) {
    C1ON_bit = 0;                                               // Disable comparators
    C2ON_bit = 0;

    ADCON1 |= 0x0F;
    ADCON1 = 0b00000110;                                        // all ADC ports digital

    SLRCON = 0;                      // Configure all PORTS at the standard Slew Rate
  ////////////////////
VREFCON1=0;                // vref off
VREFCON2=0;                // vref off
    PORTD = 0b00000000;
    INTCON  = 0b00000000;   // disable all interrupts   ...INT0IE: RB0/INT External Interrupt Enable bit
    ANSELD = 0b11000000;    // Configure all PORTS at the standard Slew Rate
    LCD_Init();
    LCD_Cmd(_LCD_CURSOR_OFF);
    LCD_Cmd(_LCD_CLEAR);

 
 while(1)  {
  data_average = 0;
 
 for(j = 0; j < 10; j++) {

       // data_average = data_average +  data_average_new ;
            TRISD = 0b10110010; //0 = Output,
             ADDO = 1;
             ADSK = 0;
            TRISD = 0b10111010;  //0 = Output, 1 = Input  RD3

                while(ADDO )  ;
                   //wait until ADDO is zero
                count = 0;
                          for(i = 0; i < 15; i++) {
                              Delay_us(3);
                              ADSK = 1;
                              Delay_us(3);
                              ADSK = 0;
                               if(ADDO=1) count = count +1 ;
                               if(ADDO=0) count = count ;  // dont really need this line

                                count = count << 1  ;
                          }    // end for loop

                           ADSK = 1;
                          count = count ^ 0x800000;
                           ADSK = 0;

                           if (count > 0xFFFFFF)
                          {
                                  LCD_Cmd(_LCD_CURSOR_OFF);
                                  LCD_Cmd(_LCD_CLEAR);
                              lcd_out(1, 1, "Out of range");
                               LCD_Out(2,4,txt);
                               Delay_ms(200);
                               }

        //  }  // end while loop
     data_average = data_average + count;

              
   }  // end for loop  j
  
 //IntToStr(j, txt2);
    data_average = data_average / 10;
            LongToStr(data_average, txt);

            LCD_Cmd(_LCD_CLEAR);
               lcd_out(1, 1, "For loop:");
                  // LCD_Out(1,11,txt2);
                    LCD_Out(2,1,txt);
               Delay_ms(600);

               }
}      // end main
 

Hello!

Without any further information, it's not easy to troubleshoot.
Something that could make it easier would be to publish at least part of your design,
so that we don't have to search for documentation, for instance to know what a HX711 is.
And a sketch showing how you wired that chip would make it even easier.
So basically it's a 24 bit ADC.
- Is it a module you bought or did you make the circuit yourself?
- Are you properly using the differential outputs?
- What about the power supply, is it stable enough? As you are using a 24 bit ADC, you have
to be extremely careful with any detail in order to get a fair accuracy. By taking 10 values,
you can indeed reduce the noise by a factor of 3 (roughly), but before looking at signal processing
improvements, you have to first understand why it's so inaccurate. Right now, the ratio between
8453470 and 8427890 is 1.003, so you are a little above 8 bits.
That said, you will never get 24 bits accuracy. If you can get 16 bits out of it, that will already
be a performance.

Dora
 

Hi,

I fully agree.
..additionally we need to know what's your input signal. Voltage, impedance, where it comes from...


Klaus
 

hello,

I have tested the hardware with an arduino board and its code, and works fine.
So now I am hooking the hardware to a PIC chip and I am not getting the expected value.
My help I need is concerning the programming. I was able to advance a bit and added a bubble sort and filter out the some ramdon values. I will post the code below.
The code seems to work better if I add a delay in the bubble sort in which I used to LCDout the value to see what I was getting. When I commented it out the LCDout part, I was getting variable results.....
Also I was taking 15 bit at first, but after reading the datasheet, I am back to using 24 bit because the 25th bit clock puts the data back to high and I am getting better results this way.. I am getting better results now even without the LCDout in the bubble function..
Still fluctuating a bit , but I think I will be able to correct it if I take a larger sampling
C:
//Good code but output is somewhat random, need to remove the MIN and MAX

sbit ADSK at RD2_bit;
sbit ADDO at RD3_bit;

sbit ADSK_Direction at TRISD.B2;
sbit ADDO_Direction at TRISD.B3;

sbit LCD_RS at LATA5_bit;
sbit LCD_EN at LATA4_bit;
sbit LCD_D4 at LATA0_bit;
sbit LCD_D5 at LATA1_bit;
sbit LCD_D6 at LATA2_bit;
sbit LCD_D7 at LATA3_bit;

sbit LCD_RS_Direction at TRISA5_bit;
sbit LCD_EN_Direction at TRISA4_bit;
sbit LCD_D4_Direction at TRISA0_bit;
sbit LCD_D5_Direction at TRISA1_bit;
sbit LCD_D6_Direction at TRISA2_bit;
sbit LCD_D7_Direction at TRISA3_bit;

char *txt;
char *txt2;
int i, j;
 long int data_average;
 long int count;
 long int array[25] ;
long int temp;
    
void bubble_sort()  {

/*for(i = 0; i<25; i++) {
      LCD_Init();
    Lcd_Cmd(_LCD_CURSOR_OFF);
            Lcd_Cmd(_LCD_CLEAR);
              LongToStr(array[i], txt);
               IntToStr(i, txt2);
                    lcd_out(1, 1, "step 1");
                     Lcd_Out(1,5,txt);
                  lcd_out(2, 3, txt2);
               Delay_ms(300);
   }*/
  
for(i = 0; i<25; i++) {
   for(j = i+1; j<25; j++)
   {
      if(array[j] < array[i]) {
         temp = array[i];
         array[i] = array[j];
         array[j] = temp;
      }
   }

}
/*for(i = 0; i<25; i++) {
      LCD_Init();
    Lcd_Cmd(_LCD_CURSOR_OFF);
            Lcd_Cmd(_LCD_CLEAR);
              LongToStr(array[i], txt);
              IntToStr(i, txt2);
                 lcd_out(1, 1, "Step 2");
                  lcd_out(1, 3, txt2);
                   Lcd_Out(2,1,txt);

               Delay_ms(300);
             }*/

}      // end bubble sort

void main(void) {
C1ON_bit = 0;                                               // Disable comparators
    C2ON_bit = 0;

    ADCON1 |= 0x0F;
    ADCON1 = 0b00000110;                                        // all ADC ports digital*/

  //   SLRCON = 0;                      // Configure all PORTS at the standard Slew Rate
  ////////////////////
VREFCON1=0;                // vref off
VREFCON2=0;                // vref off
    PORTD = 0b00000000;
   // INTCON  = 0b00000000;   // disable all interrupts   ...INT0IE: RB0/INT External Interrupt Enable bit
    ANSELD = 0b11000000;    // Configure all PORTS at the standard Slew Rate
    LCD_Init();
    LCD_Cmd(_LCD_CURSOR_OFF);
    LCD_Cmd(_LCD_CLEAR);


 while(1)  {
  data_average = 0;

 for(j = 0; j < 25; j++) {

            TRISD = 0b10110010; //0 = Output,
             ADDO = 1;
             ADSK = 0;
            TRISD = 0b10111010;  //0 = Output, 1 = Input  RD3

                while(ADDO )  ;
                   //wait until ADDO is zero
                count = 0;
                          for(i = 0; i < 24; i++) {
                              Delay_us(9);
                              ADSK = 1;
                              Delay_us(9);
                              ADSK = 0;
                               if(ADDO=1) count = count +1 ;
                               if(ADDO=0) count = count ;  // dont really need this line

                                count = count << 1  ;

                          }    // end for loop

                           ADSK = 1;
                          count = count ^ 0x800000;
                           ADSK = 0;
                           array[j] = count;
                          
                           if (count > 0xFFFFFF)
                          {
                                  LCD_Cmd(_LCD_CURSOR_OFF);
                                  LCD_Cmd(_LCD_CLEAR);
                              lcd_out(1, 1, "Out of range");
                               LCD_Out(2,1,txt);
                               Delay_ms(200);
                                   }

                            data_average = count - 8453990;
                          //  data_average = count /3;

               // data_average = data_average / 10;
/*LCD_Cmd(_LCD_CURSOR_OFF);
                                  LCD_Cmd(_LCD_CLEAR);
                                  IntToStr(j, txt);
                                  LongToStr(array[j], txt);
                              lcd_out(1, 1, "init array");
                              lcd_out(2, 1, txt);
                               Delay_ms(300);*/

          }  // end for loop  j
          
          //  Call array_sort function to sort the 25 values. Keep the middle 15 values only
                              /*LCD_Cmd(_LCD_CURSOR_OFF);
                                  LCD_Cmd(_LCD_CLEAR);
                              lcd_out(1, 1, "bubble_sort");
                               Delay_ms(500);*/

               bubble_sort();   // Sorting from ascending order take middle 13 samples
                 temp =0;
                
                 for(j = 9; j < 17; j++) {
                
                   temp = temp + array[j];
                        }  // end for loop for sampling average
                   /* 8453990 - no weight
                      8454048 - 20g      diff
                      8454138 - 50g
                      8388662 - 70g
                  */
                  temp = temp /8 ;
                    LCD_Cmd(_LCD_CURSOR_OFF);
                                  LCD_Cmd(_LCD_CLEAR);
                                   LongToStr(temp, txt);
                              lcd_out(1, 1, "Final value");
                              lcd_out(2, 1, txt);
                               Delay_ms(2120);
             }  // end infinit while loop
            
            
}      // end main
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…